Introduction: ImpBot: a Pan-Tilt Electric Imp Robot

About: Developer at Electric Imp
With controlBox, you've got a great basic input device pushing data to the Electric Imp planner (and therefore to the internet at large, via the HTTP Out node), but what about output devices? Controlling lights is a good trick, as we saw yesterday, but it's not enough to start your quest for global domination. No, as the Instructables Robot is well aware, for all your real conquering needs, you need robots.




Building an Electric Imp robot is quick and easy. Check out this instructable to build your own.

Step 1: Building ImpBot: What You'll Need

Building an imp-based robot is particularly easy for a couple of reasons:
  1. The Electric Imp API for configuring and controlling Pulse-Width Modulation (PWM) is quite straightforward and intuitive
  2. Many servos, like the A0090 micro servo in the pan/tilt mechanism I used here, run great on a 4.5V to 6V supply - which means you can power your electric imp breakout board and servos from the same small power supply
    • A note on this: you may have less luck if you attempt to run the imp and servos off of a USB power supply. While the supply voltage is just fine, a USB power supply may not provide the necessary current, depending on what you're using. 
  3. You don't need any additional components: power, an imp, and a servo, and you're all wired up.
  4. Everything is really small!
Here's a full inventory of what I used:
  1. An imp (rather obviously)
  2. An electric imp breakout board (I used an "April" dev board from electric imp, but the Sparkfun breakout is identical)
  3. Two A0090 micro servos
  4. A pan/tilt bracket
  5. A small piece of stripboard for connecting things cleanly
  6. A pair of CR123 batteries
  7. A pair of CR123 battery clips
  8. Some headers and heat shrink tubing to keep things neat
  9. A box I had handy. Something 4" square by 1.5" deep or more should be just fine for you. You'll want to cut some holes in it, so get one made out of something reasonably easy to cut.
  10. A SPDT (Selector) Switch, which I wired up to select between USB and battery power. Since I don't have USB power connected, it serves as a power switch
And of course, some tools were required:
  1. A soldering iron and some solder
  2. Heat gun for shrinking heat shrink
  3. A big, heavy pair of scissors for cutting up the stripboard. If you're going to saw this stuff (you probably don't want to), wear a respirator. FR4 dust is toxic.
  4. A pair of angle cutters.
  5. Some double-sided foam tape for securing stuff to the bottom of the box
  6. Drill 
  7. Small adjustable wrench
  8. Dremel - great for cutting a hole for the pan servo in the top of the box
Alright! That's the works. Check out the next step for the schematic and let's get started.

Step 2: Making a Compact 6V Supply From Two CR123 Battery Clips

A pair of CR123 batteries is perfect for powering the imp breakout board. Not that this is the only battery solution that works - there are plenty of ways to power up your board. This solution is is just particularly compact and convenient. Since this is the technique I used for this project, now is a good time to take a look at how to make a compact 6V supply for your imp out of a pair of CR123 battery clips.
  1. Take a good look at your clips and note which side is marked as positive and negative. Most of these clips are mechanically functional in either direction, but the markings are helpful if you use them, so go ahead and take note of that now. 
  2. We're going to solder the positive tab on the bottom of one clip to the negative tab on the bottom of the other. The tabs on the side that we DON'T solder together must not touch, however, or they'll short your batteries out. Let's go ahead and cut them off now.
    1. Orient your battery clips as shown
    2. Cut the positive tab off the bottom of one clip
    3. Cut the negative tab off the bottom of the other
  3. With the appropriate tabs cut, we need to solder the remaining positive tab to the remaining negative tab. Before we do that, though, we need to put a piece of foam tape between the two clips to hold them together. Tape together as shown.
  4. NOW go ahead and solder the remaining two tabs together as shown.
  5. Add wire leads to connect your battery pack to your imp breakout board. For convenience, we can now think of your battery pack as one big battery (with two CR123s inside it). The "positive tab" henceforth refers to the positive side that IS NOT soldered to the negative side of the other battery. The same goes for the negative side - we mean the negative terminal of the battery clip that IS NOT soldered to another battery clip.
    1. Solder a short (1" to 1.5") piece of red wire to the positive side of your battery pack.
    2. Solder a slightly longer (1.5" to 2") piece of black wire to the negative side of your battery pack.
  6. Put a piece of foam tape on the bottom of  your breakout board and use it to secure your breakout board to the top of your battery pack. 
  7. Connect the positive (red wire) side of your battery pack to the "P+" terminal on your breakout board.
  8. Connect the negative (black wire) side of your battery pack to the "P-" terminal on your breakout board.
You're all set! You can test this out now by popping in a couple of CR123 batteries, connecting the power select terminal on your breakout board to the "BAT" terminal next to it, and popping an imp card into your breakout board. You should see the imp light up and try to get online. When you've got that working, you're ready to build your robot - head to the next step! 

Step 3: ImpBot: Schematic and Getting Started

After getting started with this project, I discovered Fritzing, an excellent tool for designing DIY projects that has most of Sparkfun's catalog already included. We're working now on getting the Electric Imp breakout board symbol added to the Fritzing library, but it's not there yet, so bear with me.

For tips and tricks on soldering to plated holes on your breakout board, male pin headers, and solder lugs, check out the controlBox instructable. To keep things moving, I'm not going to go through the whole soldering procedure here in the same depth. 

The servos already come with female socket headers, so we can avoid soldering them completely by just putting some male pin headers down on the perfboard. I also added a pair of wire leads to my breakout board for Vin and GND, which I wired to male pins. I put a four-pin female socket down on the perfboard (I only needed two pins, but four-pin sockets were the smallest I had). 

I also happened to have male pin headers on my breakout board already, so I was able to use test leads with single-pin female sockets on both ends to connect the PWM signals on the breakout board to the appropriate pin on my perfboard that way.

Without belaboring the point further: you can connect the lines however you prefer. The easiest way, if you're starting with bare boards, is probably just to run a wire from your breakout board to the perf board for each of:
  • Power (Vin, 6V)
  • Ground
  • Pin 1 (the PWM signal for the pan motor)
  • Pin 2 (the PWM signal for the tilt motor)
Assemble the motor circuitry:
  1. Solder two three-pin male headers to your perfboard
  2. Use small pieces of wire to connect the middle pin of each to the 6V supply from your breakout board
  3. Use the same technique to connect the right pin of each three-pin header to ground on your breakout board.
  4. You can run the PWM signal for each motor directly to the remaining pin on each header from the breakout board.  

That's the functional part of this project! If you want a power switch for your robot, you can add that too:
  1. Solder a 4" to 6" piece of wire to the center pin of the three-pin header used to select USB or Battery power on your imp breakout board. If there is no header on your breakout board, you can solder this wire right to the plated hole in the center.
  2. Solder another two 4" to 6" pieces of wire to the USB and BAT connections
  3. Connect these three wires to your toggle switch:
    1. The wire from the center pin (or center hole) should be connected to the center pin on the toggle switch
    2. The remaining two wires go to the two outer pins on the switch - which side is which does not matter.
    3. As always, I recommend using heat shrink on these connections to keep things neat.
Check it out! A robot! Alright, time to put this project into the enclosure.

Step 4: Into the Box

Time for some judicious drill-and-dremel work; a robot that can't stand up by itself isn't exactly on the path to global domination. Give your robot some dignity and get a decent box.

Naturally, I recommend you check your servo's dimensions and measure out the cuts you're going to make accordingly, but I've provided a quick diagram of the dimensions I used for my A0090 servo. The dimensions shown are measured values from the body of the servo itself; I cut wide of these. In addition to making a cutout for my servo's body, I made a notch for the cable to the tilt servo. I cut the notch big enough that some additional wiring will fit comfortably; the goal is for the robot to be able to hold something like a light or a camera or a marshmallow-roasting stick or whatever the case may be. 

NOTE: I highly recommend drilling the mounting holes for the servo before making the servo body cutout with the dremel. The top of the enclosure is stronger without a big hole in it, and it will make your drill work easier and more accurate. Better still; there's no danger of the drill walking to the edge of the cutout and blowing through the wall between the mounting hole and the cutout if you haven't made the cutout yet. Center-punch your holes before drilling for accuracy and drill to 1/8".

I highly recommend using a drill to drill the four corners of the rectangle prior to cutting along the edges with a dremel. With a cut-off wheel, getting into the corners without overshooting and making unsightly marks is quite difficult. By pre-drilling the corners, you give the dremel end points to cut lines between. This also seems to help with reducing the amount of melted plastic blown back into the cut by the cutoff wheel. Try starting with an 1/8" hole at each corner.

Once you've got the cutout in the enclosure, you can drop the pan servo into the cutout and secure it. You may need to remove the pan/tilt bracket from the top of the pan servo in order to get at the mounting screws. 

While you're working on the enclosure, now's a good time to make a hole for the power switch, if you added one. The switch I used required a 3/16" hole in the enclosure. Center punch, drill with the 1/8" drill first, then finish with the 3/16" drill. If you have a lot of burrs around the hole, you can clean up with sandpaper, an X-acto knife, or (my personal favorite) you can gently deburr the hole with a countersink tool. Remove the hex nut from the switch shaft, feed the switch through the hole, and secure with the nut by gently tightening with a a small adjustable wrench. 

Feed the cable for the tilt servo through the notch in the lid, secure both of the servo headers to the pins on your board inside the case.  Lower the lid onto your enclosure and secure it. 

Alright, let's add a little bit of code to make your robot controllable.

Step 5: The Source Code: Your Robot's Firmware

The last step you'll need to make your impBot work like the one in the video at the beginning of this instructable is just a little bit of of firmware. Fortunately, I've written it for you! I highly recommend you take a quick look at the code; thanks to the way the Electric Imp API is set up, the code is extremely short, readable, and easy to understand. One of the really fun things about programming imps in Squirrel (the language the imps use) is that it's object-oriented. I've shortened the code considerably here by defining a simple class for my servo, then making two separate instances for my pan and tilt motors. It's a pretty powerful setup for a simple embedded application!

To grab the source code, head to https://github.com/tombrew/ei-fw-tom/blob/master/april-dualServo.nut. Copy the code to your clipboard, then head to plan.electricimp.com and sign into planner. Now follow these steps:
  1. Click the "code" tab at the top of the planner window in your browser
  2. Click the big "+" button at the top of the code window
  3. Give your new firmware a name. I used "april.dualServo", as our breakout board design is called "April"
  4. Paste the code from github into the IDE that appears
  5. Click the save button in the top left of the IDE window
  6. Click the "plan" button in the top left to go back to the planner view
There! Now we just need to tell your robot to run this code. Follow these steps:
  1. Blink up your impBot to put it on your wifi network. A blue box will appear in your plan. If you haven't blinked up an imp in your breakout board before, the blue box will say "BLANK", as the imp service does not have any firmware to run on the board until you assign one. 
  2. Click the settings button (a little box with a picture of some sliders on it) in the top-right corner of the blue box representing your impBot
  3. In the popup box that appears, click the drop-down menu to show all the firmware you have available to run on this imp
  4. Select the firmware you created by pasting in code from github
  5. The imp will begin to run your code in just a few seconds. The words "APRIL DUAL SERVO CONTROLLER" will appear in the blue box once the imp is configured.
Voila! You've got an imp-controlled robot, which you can drive from anywhere in the world as long as you have internet. Try hooking up one of the "tick/tock" nodes in the planner and see what happens, or connect your robot to a potentiometer input like I did in the intro video. Try hooking up an HTTP In node to send values to your robot, and you can even write up a quick mobile app to control your robot with your phone or tablet - from anywhere!


Hack It! Contest

Participated in the
Hack It! Contest