Robot Plant


posted Jan 28, 2011, 11:10 AM by Eric McKinley

The code below is mostly the hello world code from the first Make It Last series modified slightly. 


// Include files
// These lines allow us to use code routines (libraries) from other files,
// so that we don't have to write everything by ourselves.

#include <p18lf25k22.h>        // This file includes definitions for all of the
                            // registers on our chip
// Define statements
#define GREEN_LED PORTAbits.RA0
#define SOLENOID PORTAbits.RA1

// Pragma statements
// These lines tell the microcontroller what configuration to use when it
// when it turns on. The most important part for now is to tell the
// microcontroller what to use for a clock input.

// Use the internal oscillator as a clock
#pragma config FOSC = INTIO67

// Configure it to run at 16 MHz
#pragma config PLLCFG = OFF

// Make sure that the internal watchdog timer is off. The watchdog is a
// a special feature of the processor, that runs separately from the main
// program and can be used to clea
#pragma config WDTEN = OFF

// Function declarations
// Declare any user functions that you want to use here
void main (void);
void setup(void);
void loop(void);

// Function definitions
// Define any user functions that you want to use here

// The main() function is where the program 'starts' when the microcontroller
// is turned on. For this project, we will use it to call setup() and loop()
// functions, then use those similar to an Arduino sketch.
void main (void)
    // Call the setup function once to put everything in order

    // Then call the loop() function over and over
    while (1)

// This function is called once, when the microcontroller is turned on.
void setup(void) {
    unsigned int count;

    // Configure the oscillator to use a 16 MHz input.
    OSCCONbits.IRCF = 111;

    // Set pins 0-3 to be outputs, and 4-7 to be inputs, then set them all to 0.
    // Cleared bits are outputs.
    TRISA = 0b11110000;
    PORTA = 0x0;
    // Flash the green led to indicate bootup.
    GREEN_LED = 1;
    for( count = 0; count < 64000; count++) {}
    GREEN_LED = 0;

// This function is called repeatedly
void loop(void) {
    // Make a variable for the delay routine
    unsigned int count;

    if(PROXIMITY) {
        // Indicate there is a fly.  Useful for debugging.  Ha.  Ha.  Ha.
        GREEN_LED = 1;
        for( count = 0; count < 32000; count++) {}
        GREEN_LED = 0;

        // Trigger the solenoid, wait for it to close.
        SOLENOID = 1;
        for( count = 0; count < 64000; count++) {}
        for( count = 0; count < 64000; count++) {}

        // Release the solenoid, and wait for it to open. 
        //If we don't wait here, the proximity sensor would see the flytrap mouth and trigger again. 
        SOLENOID = 0;
        for( count = 0; count < 64000; count++) {}
        for( count = 0; count < 64000; count++) {}

Making Custom Springs

posted Jan 28, 2011, 10:24 AM by Eric McKinley

While my hardware store has an impressive selection of replacement springs available, they don't have much in the way of miniature springs.  Using a method adapted from almico123 I was able to get just the spring I wanted.  Below is a photo-journal of my method.  Watch the video for a great way to do it.

The basic idea is to wrap the music wire around a post. 

Because I didn't have any brass tubing handy, I made a block from scrap HDPE with the right sized hole in it.  Then, near that hole, I predrilled a small hole at an angle for the screw.  The screw pokes out a tiny bit from the bottom to catch the music wire.

Here's the HDPE block on the post, and the music wire lined up and ready to be grabbed by the tip of the screw.

The first wind:  The big piece of blue tape holds the other end of the wire stationary.  You have to be careful on the first few passes that the screw tip doesn't grab the other end of the wire as it passes.  I had to lift up on the block a little to "hop" the bottom wire. 

The second and third passes.  Around and around. 

I found I had to go about 160 degrees past where I wanted the spring to open naturally. 

The finished product is very easy to duplicate. 


posted Jan 25, 2011, 2:42 PM by Eric McKinley   [ updated Jan 28, 2011, 11:30 AM ]

Project Description

The Robotic Fly Trap is an electro-mechanical device that attracts, senses, and kills flies.

Schematic and Photos

The prototype flytrap mouth

The base of the flytrap mouth.  The screws on top limit the width of the mouth's opening.  The screw on the side is a setscrew to attach the base to the copper stem.  The springs return the mouth to an open position when the solenoid releases.

Experimenting with making different spring openings.  The springs on the base proved too stiff.

Adding lips to the mouth. 

The lipped mouth, with sensor viewing the killzone.
A closeup of the solenoid attached to the base.  The screw facing the camera is a setscrew to clamp onto the copper stem.  The big bolt is for securing to the base through the drain hole with an eye-bolt for hanging. 

Trying to cram everything on a small enough breadboard to fit into the pot.

The electronics don't quite fit in the pot.

Bill of Materials

  • Hardware
    • DVD case
    • 4" Terra Cotta Pot
    • 16" Copper Tube, 3/8" OD
    • 3/8" HDPE scrap, about 1" by 6"
    • Misc. Small Nuts and Bolts
    • Instrument Wire, .38mm by about 4'
  • Electronics
    • Microchip PIC18LF25K22 - Microcontroller
    • National Semiconductor LM2586 - Flyback Regulator
    • Bosch SFH7741 - Proximity Sensor
    • Guardian T4X12-INT-12DC - Solenoid
    • .47uF Capacitor
    • .1uF Capacitor
    • 100uF Capacitor
    • 680uF Capacitor
    • 2k Resistor
    • 4.7k Resistor
    • 10k Resistor
    • 1k Potentiometer
    • 15uH Coil
    • 1N5820 Diode
    • Random LED for diagnostics
    • Small breadboard and jumpers
    • .1" Male headers x12

Biggest Challenge

So many!  I think the one that had me stumped for the longest was realizing that I needed to calibrate the proximity sensor.  The datasheet gives an arbitrary value that didn't work for me, and I ruined a sensor because of it.  Now I know to try a potentiometer on any pin labeled "prog" or "calibrate" tied to a resistor.
Close runners-up:
  • Getting RB to act as input pins.  Still haven't figured it out.  I used RA 4 as an input instead.
  • Figuring out how to trigger the solenoid without running power to components when not needed.


I think the biggest improvement needed is to utilize a servo and a trigger-action mechanism to pull the mouth shut.  This would remove the need for a voltage booster, and its extra components.  Sensing the fly should also be more reliable.  Maybe a PIR sensor with the range cranked way down?

How This Started

Reading in the Make blog about the series focused on minimizing power requirements for microprocessor circuits piqued my interest because I have been disappointed with the battery life of an Arduino project powered by a 9v battery.  I followed the first newsletters and contest by simply observing.  I decided to overcome my fear of using a bare microcontroller after reading about the second series and having the desire to make a flytrap. 

Without this contest, I wouldn't have the impetus to explore the world of microcontrollers outside the realm of Arduino.  Also, I may not have had the realization that the reason I enjoy electronics so much is because I'm really not good at it, and I have so much to learn.  Thanks to Microchip, Energizer, and Make dudes Matt and Gareth!


posted Dec 14, 2010, 1:09 PM by Eric McKinley   [ updated Dec 22, 2010, 12:06 PM ]

Bosch's  proximity sensor (Näherungssensor in German), the SFH 7741, arrived from Digikey yesterday.  Despite being about 1/4 the size I was expecting, I got it wired up to a ribbon cable and male header pins.  Of note is the ribbon cable I used, which I took from the new type of IDE (parallel ATA) cable that has 80 conductors.  These are much smaller, and actually made soldering easier, as the gauge caused the wires to line up perfectly with the proximity sensor. 

Steps to solder something so tiny:
  1. Get that iron cranked down to about 400 deg F
  2. Separate the wires about 15mm from the end of the cable.
  3. Cut the pin1 wire (red) about 2.5mm shorter than the rest. 
  4. Repeat for wires 3 and 5, so 2, 4, and 6 will reach across the device to pins 6, 5, and 4 respectively.
  5. Strip and tin 1mm of each wire.  Tinning will cause the insulation to roll back a bit, so that's why we only strip 1mm.  I used the 26g slot on my strippers at an angle.
  6. Tin the pads on the device.  Between each tinning, allow the device to cool for at least 15 seconds. 
  7. Fold wires 2, 4, and 6 out of the way for the moment
  8. Tack wires 1, 3, and 5, allowing time for the device to cool between each tack. 
  9. Fold wires 2, 4, and 6 back down so they meet pins 6, 5, and 4 respectively, and tack. 
  10. Remember when soldering the header, that the wires correspond to different pins:
    1. 1
    2. 6
    3. 2
    4. 5
    5. 3
    6. 4
According to the datasheet attached below, pin 1 should not be connected, so I'll just snip it at the sensor.  Pins 2 and 4 should go to ground, 3 is the output, 5 is V+, and 6 is "prog" or what we'll use for adjustment. 

I've wired it up, but am not getting a reading from it.  Not quite sure what resistor value I should try for prog, but I've tried 0, 100, 10k, and no resistor to ground...  Maybe I fried it, as the data sheet specs 250 deg F for soldering...

UPDATE - 12/16/2010
Because I thought I fried it, I swapped it for my spare.  Still nothing but 0v on all pins but Vin.  The datasheet says "please don't use a regulated power supply" but I don't know if that poor translation means it won't give reliable results or if it just won't work.  So, I pull out some batteries, but they'll only net me 3v.  Checking the datasheet says 2.4v min.  Perfect. 

Plug it in... Same thing.  What am I missing? 

UPDATE 12/17/2010
Checking pin 1 on the scope I see a peak of voltage every 79ms.  The datasheet says the refresh time is between 60 and 90 ms, so this is within spec.  So why still no output? 

I wasn't using the 100uF cap recommended in the datasheet, and the device got very hot.  Did I cook it?  Probably.  Digikey, you're welcome.  I needed a motor controller anyway, so I just placed another order.  At $6 each, I've learned my lesson to read the datasheet first....

UPDATE 12/22/2010 - IT WORKS!
Got a new one from digikey and wired it up.  I tried it in the same circuit, with dismaying results.  Then the revelation came to try a potentiometer on the Prog (calibrate) line.  A HA!  Tweaking the pot to 570 ohms gives results! 

To the right is a screen grab of the signal.  The square waves is my finger over the sensor.  Bwahaha!  This is a minor victory in what could have been the death of the project. 

12 volt solenoid in a 5 volt system

posted Dec 13, 2010, 11:34 AM by Eric McKinley   [ updated Jan 25, 2011, 3:09 PM ]

I received my solenoid from today, and was a little dismayed at the size.  It's about the diameter of a Sharpie.  I first tried applying 5 volts to the solenoid.  It did in fact actuate, but with embarrassingly little force.  The 12v power produced a satisfying snap, but I'll still have to fight friction and a spring return.  I think it can work.

So, how to supply 12 volts to my solenoid in a 5 volt system?  There's a lot of info out there for voltage multipliers using an AC to DC process, but to stick directly in the realm of batteries, I'd like to multiply my DC voltage by two.  Assuming I have 6 AA batteries in there, that would give me at most 18 volts (which happens to not fry the solenoid when only applied for a second). 

This may be the answer: uses a 555 as an astable multivibrator.  Not quite sure how that helps us, but I think it's making fake AC power, which we can then rectify to DC power... Maybe I should just put in more batteries...

After reading I've ordered a LM2586 for some voltage doubling.  Should be in today or tomorrow.

UPDATE 12/21:
Got the LM2586 on a breadboard and tested with the solenoid, using diagram 2 on page 10 of the spec sheet.  I subbed the 1N5820 with a random smaller diode, the 15uH coil with a hand-selected coil from an old power supply and the 680uF cap with a 470uF one.  I DO get considerably more pull from the solenoid at the 12v through a 5v regulated supply then just the straight 5v.  Good news. 

The voltage boost circuit

Here's the circuit from the datasheet.  It has an error.  A minor one, but enough to cause me some trouble.
The first time I built the circuit, I made the mistake of making a connection that isn't shown.  The circuit worked.  The next time I breadboarded it, I omitted the connection, as it is omitted on the diagram.  The circuit didn't work.
The mistake is right above the ground symbol.  On this type of drawing, a dot at an intersection represents a connection.  No dot represents a hop.  There should be a dot between ground and the two filtering caps. 

Of course, if I was an EE, I'm pretty sure I would have caught that and thought nothing of it.  Because I'm not, I did not inherently realize that the two electrolytic caps in the circuit are filtering caps, and should be connected between V+ and ground.  Now I know, and I won't make that mistake again.  This reminds me that the reason I enjoy electronics so much is because I'm really not good at it, and I have so much to learn. 

Coil values without measurement

Being quite complicated to actually measure inductance, I decided to reverse-engineer one of the many that can be found on old regulated power supplies. is a tool to calculate how to make an air-core inductor.  I plugged in measurements for a few of the coils, and settled on one that has a ferrite core about 4mm in diameter, is about 20mm long, and has 18g wire.  The calculator says I need 81 turns for an air-core inductor, but I figure this ferrite-core is more efficient, as it only has about 20 turns.  Not quite ball-park, more like I got the right continent.  But it works.

Learning about the Quartz Crystal Microbalance

posted Dec 10, 2010, 2:15 PM by Eric McKinley

Here's what I found interesting and easiest to understand: (from

One can describe the QCM to be an ultra-sensitive mass sensor. The heart of the QCM is the piezoelectric AT-cut quartz crystal sandwiched between a pair of electrodes. When the electrodes are connected to an oscillator and an AC voltage is applied over the electrodes the quart crystal starts to oscillate at its resonance frequency due to the piezoelectric effect (see figure below). This oscillation is generally very stable due to the high quality of the oscillation (high Qfactor).

Quartz Crystal

If a rigid layer is evenly deposited on one or both of the electrodes the resonant frequency will decrease proportionally to the mass of the adsorbed layer...

I really like this approach.  Basically, a fly on the surface will change the resonant frequency of the crystal, which can then be measured.  I see a few downsides:

  • The crystal needs to be oscillated with AC current.  Not impossible, but extra electronics means more battery drain
  • The oscillated frequency is probably of too high a rate to be read by the microcontroller's analog input. 
    • Here's more info, with the possibility of upping the sample rate on the analog pins:
      • Also see page 10 of the attached PDF.  
  • The oscillations may act as a deterrent to the fruitflies?  Hey, they're tiny, so maybe it's possible...
  • Where can I get one? has one on sale for the low, low price of $590. 

Toying With an Optical Mouse Imager

posted Dec 9, 2010, 10:54 AM by Eric McKinley   [ updated Dec 15, 2010, 6:11 PM ]

Inside the Logitech M-SBF90 optical mouse I have is the A2610 optical mouse imager.  I have an idea to use the imager to detect differences in pixel values as a fly lands on the trap mouth.  Dumping the imager data to the microcontroller every second or so and comparing with the previous dump could detect the fly.
Sprite, over at has written some software that can read the image from the chip via serial.

According to page 23 of the data sheet, in the attachments below, each pixel holds a value between 0 and 63.  0 is black and 63 is white.  

The process would look for <=(x) difference in >=(y) pixels, to account for ambient light changing.  x and y will have to be adjusted for optimum results.  To start with, I would try x=10 (approx. a 15% change in darkness) and y=9 (a three-by-three square area). 

Wow, I just sounded a whole lot smarter than I am.  I think a good way to go about learning how to collect this data (because I'm not so hot with arrays or serial communications) is to:
  1. Grab all 324 pixels with the microcontroller (Arduino, as I don't have my programmer yet)
  2. Send pixels to the serial port on my PC to see what the data actually looks like
  3. Practice with a fake fly to note value changes

Data? - Update 12/10

I figured I should first see if I am getting any data out of the SDIO port.  Nope.  Flat line.  I can see the frame rate pin flip-flopping, and watch it drop low when I make the LED come on fully.    Maybe it won't send data without a request first?  Hmm...

Dat-Ah! - Update 12/15

RTFM.  Page 12 states, and I quote:

The synchronous serial port is used to set and read parameters in the ADNS-2610, and also to read out the motion information.

The port is a two wire, half duplex port.  The host microcontroller always initiates communication; the ADNS-2610 never initiates data transfers.

Did you catch that?  So my presumption from a few days ago was right, but I should have read the datasheet first.

Poking around with this lead (ie. googling "arduino optical mouse") lead me to this page: which has a library for reading the mouse info and printing it to the serial port. 

Looking at the library files, I've found some interesting lines of code in OptiMouse.cpp.  Take a look at lines 54 and 74 to start with. 

Off to the forums to ask for help...

Exploring Possible Sensors

posted Dec 8, 2010, 9:38 AM by Eric McKinley   [ updated Dec 22, 2010, 3:12 PM ]

As I mentioned in the first post, my initial thoughts on sensing flies were to explore some common sensors.  However, each has a shortfall:
  • IR interrupt
    • Fly would have to be very (un)lucky to completely occlude the IR emitter from the IR receiver. 
  • Capacitive
    • A fly, I think, would not be a large enough distinction from the noise floor of the capacitive sensor input.
  • Pressure
    • A fruitfly-like threshold on any sort of pressure or weight sensor would definitely be falsely triggered by puffs of air from the nearby kitchen door. 

Here are some less straight-forward, but more likely sensors:

Optical mouse sensor (

To test if this would work, I placed a stationary optical mouse over a stationary white paper.  Then, slid transparency film between the two.  Result: no movement of the mouse on screen.  Good.  Next, I placed a dot from a marker on the transparency, to represent the fly.  As I moved the dot around between the mouse and white background, I was able to get the mouse on screen to jump about 10 pixels.  The area that was affected was relatively small, but could be enlarged by moving the mouse away from the background.  The only problem presented in testing is the mouse disables itself if it is more than 3mm away from the paper. 

Low power proximity sensor (OSRAM SFH 7741-Z)

Operationally identical to the proximity sensor in the iPhone that shuts off the screen when you put the phone to your face.  The trick here is to light up the fly with the "spotlight" that is in the sensor emitter, enough that the detector will see it as a change in proximity of the inside of the trap mouth. 

Quartz Crystal Microbalance

Dave1959 just suggested on the forum that I explore the QCM. 

Noise Floor

On a typical touch sensor input, a human finger is barely enough to raise the input above the inherent noise floor.  Maybe because it is battery powered, the noise floor will be lower?

Help me order some sensors!


posted Dec 7, 2010, 3:18 PM by Eric McKinley

Make magazine, along with Energizer and Microchip Technologies, is holding a robotic plant design contest.  My interest has been piqued.  The first solicitation for ideas: What do you think of when you hear the phrase 'robotic plant?"

Here's what I came up with:

The Fly Trap robot can be housed in a genuine plant pot, and optionally hung upside down to preserve counter space. 
  • Some hardware and a disc can be used to create the container and hanging apparatus
  • Copper tube with coated steel fishing leader wire inside could be used as two conductors needed for the sensing apparatus. 
  • Bait, such as honey, would be placed on the trap's mouth
  • A solenoid attached to a cable would pull the trap's mouth shut
  • The micro controller would sense a fly at the trap's mouth, wait for the fly to get comfortable, and trigger the solenoid before returning to watch mode
The main issue I can foresee with this is sensing the fly.  Some thoughts are:
  • IR interrupt
  • Capacitive
  • Pressure

1-9 of 9