Introduction: Touchless Sensor Controlled Robot
This project is a simple robot controlled from a PC by either a keyboard or a Leap Motion touchless sensor. The robot itself runs on a PICAXE 20M2 chip, a microcontroller which is programmed using a form of BASIC. I chose the PICAXE chip because it was inexpensive (about $5) and easy to pick up the language. Unfortunately, the PICAXE does not come pre-mounted on a board like an Arduino, so some breadboard magic is required.
NOTE: The Python module used to control this robot, PySerial, is not supported by Mac computers, so if you're using a Mac, you won't be able to use my robot controller. You can still follow the PICAXE instructions and build the robot, but you'll have to find a different method to control it.
Step 1: Hardware Needed
To run the PICAXE you will need:
- (1) PICAXE 20M2, which can be bought at picaxe.com
- (1) serial programming cable, which can be bought at http://www.picaxe.com/Hardware/Cables/PICAXE-USB-...
- (1) 3.5mm socket, which can also be bought from the PICAXE website.
- (1) 10K resistor
- (1) 22K resistor
- (1) 480 Ohm resistor
- (1) LED
- (1) 5V power regulator IC (I used an LM7805)
- (2) 100uF capacitors
- (2) 0.1uF capacitors
- (1) power switch
- (1) 9V battery with wires to plug into a breadboard
- Some wire
- (1) breadboard (I used a half-size breadboard)
- (1) computer to program the PICAXE from
To allow the PICAXE to control a robot you will need:
- (1) L293D dual H-bridge motor driver IC
- (2) DC motors, preferably with a gearbox to slow them down
- (2) wheels that can be attached to aforementioned DC motors
- (4) alligator jumper wires
- (1) robot body (I used a 4-3/4" x 4-3/4" piece of plexiglass)
- Some more wire
- (1) motor power battery with wires to plug into a breadboard, between 4.5V and 36V (this will be the power source for the motors)
- Scotch tape
Step 2: Software Needed
Obviously the PICAXE won't just magically run on its own (but that would be awesome), it needs to be programmed somehow. Fortunately, the PICAXE company has a programming environment which allows easy programming of the PICAXE chip.
To program the PICAXE you will need:
- PICAXE Editor 6 (Windows)
- AXEpad (Windows/Mac/Linux)
- AXE027 USB cable drivers (not needed for Linux)
To program the controllers used in this project, you will need:
- The latest version of Python 2.7
- The latest version of PySerial for Python 2.7 (does not support Mac)
- The latest version of Pygame for Python 2.7
To control the robot with a Leap Motion sensor, you will need:
- Leap Motion SDK from developer.leapmotion.com/ (this requires you to sign up for free as a developer)
Step 3: Prepare the Download Socket
In order to program the PICAXE chip, it will need to be connected to a computer. This is where the serial programming cable comes in. The cable connects to the PICAXE via a 3.5 mm jack, the same type as most audio devices use. The programming cable is NOT intended to be plugged into an audio device; NEVER use the cable for anything other than programming or communicating with a PICAXE.
The 3.5 mm socket that the programming cable plugs into can be fairly difficult to keep in a breadboard. For this reason, I soldered some wires onto the terminals of my socket. This allowed me to have a more stable connection to the PICAXE, and also move the socket into the perfect position. Looking at the images above, we see that, from the bottom, the left set of pins are the incoming data, the right set of pins are GND, and the top pin is outgoing data. I color-coded my wires, with green for incoming data, blue for GND, and yellow for outgoing data.
When soldering wires onto the pins, make sure that the pins grouped with each other are both soldered to the wire, i.e. the left two pins should both be soldered to the green wire, and the right two pins should both be soldered to the blue wire.
Step 4: Breadboard the PICAXE
A picture is worth a thousand words. Check the pictures above for step-by-step instructions on adding 5V regulated power to your breadboard and building the basic PICAXE circuit. Each image is numbered, and has a corresponding explanation below. Images labeled with "power" show the 5V setup, while images labeled with "PICAXE" pertain to the PICAXE circuit setup.
You can also use the Fritzing image to breadboard your PICAXE, but you may need to check the list below for clarification in some places.
Setting up the 5V power supply:
- Add the LM7805 power regulator at the top of the breadboard, with the aluminum piece facing right. Add wires between row 3 left and left VCC bus, between row 2 left and row 2 right, between row 2 right and row 4 right, and between row 4 right and right GND bus.
- Add the power switch between row 1 left and row 1 right. My switch had a few extra pins, you can ignore them.
- Connect the VCC buses together, and the GND buses together. Now we have power and GND all along your breadboard!
- Add one 100uF capacitor between row 1 left and row 2 left, with the negative side on row 2 left. Add the other 100uF capacitor between the left VCC bus and left GND bus, with the negative side on the left GND bus.
- Repeat with the 0.1uF capacitors.
Adding the PICAXE 20M2:
- Add the 10K resistor between left GND bus and row 4 left.
- Add the 22K resistor between row 4 left and row 8 left.
- Add the PICAXE 20M2, with pin 1 (top left) on row 7 left.
- Add wires between VCC and row 7 left, and between GND and row 7 right.
- Add the download socket, with the green wire (data from computer) on row 4 left, the yellow wire (data from PICAXE) on row 8 right, and the blue wire (GND) on GND.
- Add the 480 Ohm resistor between row 11 left and row 17 left. Also add the LED between row 17 left and GND.
- Add your 9V battery, with V+ going to row 1 right, and V- going to row 2 right.
At this point we should have a complete PICAXE circuit. Check over the wiring, and make sure everything is where it should be.
Step 5: Install the PICAXE Software
Now that we have a PICAXE chip wired up, it's time to program it, so jump over to the PICAXE website and download the drivers for the programming cable. Once that's done, follow these instructions to properly install the cable's drivers. If all goes well, when the cable is plugged in, it will be assigned a serial port ID number. Write down this ID number, as you will need it to verify that your PICAXE editor is recognizing the cable properly, and for configuring the robot control program.
The next software requirement is to download and install your editor: PICAXE Editor 6 for Windows, and AXEpad for Windows/Mac/Linux. Once we have an editor, PICAXE programming should be only a few keystrokes away!
NOTE: This guide assumes you are running Windows 7 and using Picaxe Editor 6. If you are not using Windows 7 or Picaxe Editor 6, my code should still work, but the programming environment will look different.
Step 6: Program the PICAXE: Blink Program
At this point we have the software and the hardware... but we don't know whether the PICAXE works or not! The simple solution is to write a "blink" program that turns an LED on and off. If you like programming, the PICAXE website has plenty of documentation for you to write your own blink program. If you just want to run a robot, download the attached file and open it with your PICAXE editor. Then plug in the programming cable.
If you are using PICAXE Editor 6, look to the left side of the GUI, and under "COM Port" click on "Refresh COM ports". If your programming cable's ID appears in the box, then you are almost ready to program the PICAXE. Plug the 3.5mm jack end of the programming cable into the 3.5mm socket in the breadboard. Then under "PICAXE Type", change the PICAXE name to match that of your chip, in our case the 20M2.
Almost there! The final step toward programming your PICAXE is to click the big "Program" button at the top of the GUI, then click the PICAXE power switch on. If all goes well, the PICAXE should download the program and start blinking its LED. Congratulations, you've just programmed a PICAXE chip!
Step 7: Add the Motor Controller Circuit
Now that we know the PICAXE is working as expected, its time to add the motor controller chip. Just like before, follow the images above, and use the list below for details.
You can also refer back to step 4 for the Fritzing drawing.
Adding the motor controller:
- Add the L293D dual H-bridge, with pin 1 on row 18 left.
- Add wires between row 18 left and VCC, between row 18 right and VCC, and between row 25 right and VCC.
- Add wires between row 21 left and row 22 left, and row 21 right and row 22 right.
- Add wires between row 22 left and GND, and row 22 right and GND.
- Add a wire between row 26 left and row 21 left.
- Add wires between row 24 left and row 14 left, and row 19 left and row 16 left.
- Add wires between row 24 right and row 12 left, and row 21 right and row 15 left.
- Add your motor power battery, with V+ going to row 25 left, and V- going to row 26 left.
At this point the L293D can be controlled by the PICAXE. Check the wiring and make sure everything is in its proper place.
Step 8: Build the Robot Body
Now we have a robot controller, but no robot to control! Let's fix that. Grab your robot body, and attach your motors to it, in the back. I got my plexiglass and motors from a kit, and the suggested method of attachment was 2-part epoxy. I, unfortunately, don't have any epoxy, so I just used super-awesome-hi-tech Scotch tape. I also drilled two holes in the front of the robot, ran small bolts through the holes, and added a nut on either side of the plexiglass to hold the bolt in place. The size of the holes and type of nut depend on what bolt is used. Really, any small bolt will work fine.
Now attach your wheels to your motors, and your robot body is done.
Step 9: Mount the Electronics
We're almost done with the hardware; the next step is to add the breadboard and batteries to the robot body. I used Scotch tape to attach the breadboard and batteries to the robot, but you can use something stronger if you'd like. Keep in mind that you'll probably want to remove the breadboard at some point, so epoxy is probably not the best choice of fastening medium.
Once your electrical has been attached to the robot body, take four wires and insert them at the following four points:
- Row 20 left
- Row 23 left
- Row 20 right
- Row 23 right
Then take the alligator jumper wires and, for each of them, clip one end to one of the four wires you just placed. The other ends of these jumper wires will connect to the wires of your motors like so:
- The row 20 left jumper connects to the left motor's V-
- The row 23 left jumper connects to the left motor's V+
- The row 20 right jumper connects to the right motor's V+
- And the row 23 right jumper connects to the right motor's V-
Tape down the jumper wires to prevent them from interfering with the wheels, and you're done with the hardware!
NOTE: Your motors, gearboxes, and wheels may differ from mine, so your motors may need to be connected differently. If you come to the end of this project, and find your motors running backward, simply swap which jumper connects to which motor wire, remembering that the left jumpers go to the left motor; similarly so for the right.
Step 10: Program the PICAXE: Motor Controller Program
Now that the robot's hardware is complete, it's time to program the PICAXE with its final script. This one's a little more complicated than the previous "blink" program. It uses the serial port, which brings up another interesting reason why I chose the PICAXE. I originally tried this robot with an Arduino, but the Arduino couldn't keep up with the serial data, and would lose data, causing the robot to respond sluggishly to my commands. I found that the PICAXE had almost no data loss, making it a much more reliable option for my robot.
Anyway, download the attached file, and, just like last time, program the PICAXE with the new set of instructions. If you're interested, you can take a peek at the code to see how it works.
Once the PICAXE has finished downloading the new program, it should blink its LED once, letting us know that it's operational. At this point, you can turn off the robot and set it aside for now.
Step 11: Install Python 2.7 and PySerial
Because everyone wants a snake inside their computer...
The robot you've created is cool, but it can't be controlled on its own. For that, we need a little help, in the form of a PC, and a control program I've written. This control program uses Python 2.7 to run, so first check and see if you have Python installed. The simplest way to do this is to open a command terminal and type 'python' into the prompt, without the quotes. If you have python installed, a short message will display, and then your prompt will change to a '>>>' shape. If you do not have Python installed, an error message will appear. If you get an error message, download and install the latest version of Python 2.7 from python.org.
Once you have Python installed, go to pygame.org and install the latest version of Pygame for Python 2.7.
Finally, go to sourceforge.net/projects/pyserial/ and download the latest version of PySerial for Python 2.7.
You should now have all the software you need to run your robot!
Step 12: Customize the Code
Even though I've pre-written a robot controller script, you will still have to make some slight edits to the code. Download the attached .py file, then open it with either a Python compiler, like IDLE, or just with a basic text editor, like Notepad. The program is by default set for my serial port number, so if you happen to have the same port number as me, you can skip this next step. However, there is a 99.6% chance you do not have the same serial port number as me, so you will probably need to change some things in the code. The above image has a screenshot of the code, and directions on how to change the serial port settings to match your operating system.
For those who like a text-based explanation, change the portNumber number from '4' to the serial port number you got in step 5. Simple.
Next, figure out if you're running Linux or Windows. If you're running Windows, you're done, and the program should work. If you're running Linux, there are two sets of commented lines below the portNumber line. Remove the '#' from the beginning of the last line in the first set, and add a '#' at the beginning of the last line in the second set. At this point, you should be done as well.
Finally, plug in your serial download cable (but don't connect it to the robot yet) and make sure the program doesn't throw any strange errors (which it shouldn't). If there are no errors, then you should be ready to roll!
Step 13: It Works!
At this point you are ready to run your new robot! Make sure the robot and robot controller program are both off. Next, plug the serial programming cable into both the computer and the robot, and turn the robot on. Finally, open the robot control program, and wait for a black frame to appear. Once this appears, press spacebar, and watch...
If all went well, your robot should have turned left, then driven forward. If it did, then it is receiving data from the PC and you can now try driving it around using the keyboard. Use 'W' and 'S' to drive the left motor forward and backward, and use 'I' and 'K' to drive the right motor forward and backward. Press spacebar to enter a simple autonomous mode, which you've just seen.
NOTE: If you find one wheel or the other is driving backwards from what you expect, refer back to step 9 of this guide for motor troubleshooting.
Step 14: Or Maybe It Doesn't Work...
So the robot didn't work properly, eh? Here are some tips to help you with your troubleshooting.
- Is the PICAXE properly programmed? If the PICAXE still has the 'blink' program running, it won't be very effective as a robot brain.
- Do you have fresh batteries? Bad batteries will stop any robot cold. You can either replace the batteries, or check the ones you're using with a multimeter.
- Are there any loose connections or short-circuits? Check your wiring and circuitry to make sure nothing has come loose.
- Does the Python script run properly? If you made a mistake while editing the script, nothing will work properly. Download a fresh copy and re-edit it, its a simple process.
- Is the proper version of Python installed? If you have Python 3, PySerial will not work.
- Are you running a Mac? PySerial, an important component of this project, will not work on a Mac.
Still not working? Message me with your problem, or leave a comment, and I'll see what I can come up with. Keep in mind that there may be some things I can't fix.
Step 15: Leap Motion Interface
Now we come to the icing on the cake: touchless control of the robot! The next few steps are dedicated to interfacing our simple robot with a sophisticated touchless sensor.
I learned most of this stuff about the Leap Motion and Python from Coding Basics, a YouTube user who has really good walkthroughs on many different coding subjects, including getting Leap Motion to work with a Raspberry Pi. Credit where credit is due!
To make your robot go touchless you will need:
- 1 Leap Motion sensor
- 1 copy of the Leap Motion-compatible Python controller, which just happens to be attached to this step.
If you have your Leap Motion controller, plug it into a different USB port than the one used by your PICAXE programming cable, and make sure the necessary drivers install properly. Once that's done, download the .py file attached to this step, and save it in its own folder somewhere. Finally, sign up as a Leap Motion developer and download the SDK, available for Windows, Linux, and Mac.
NOTE: Remember, PySerial is not compatible with Mac, so you will not be able to use my robot control script if you are running a Mac. However, the Leap Motion and Leap SDK are compatible with Mac, so you can still use a Leap Motion if you so desire.
Step 16: Setting Up the Controller Files
The Leap SDK is not an installer, its simply a collection of files needed to make the Leap Motion compatible with custom scripts. These files differ based on operating system, so figure out which operating system you use, then copy the files listed under your operating system:
Once you have copies of the necessary files, move the copies into the same folder as the .py robot controller. At this point you should have all the files needed to properly run the Python script.
Step 17: Final Steps
We now have the Leap Motion, the proper Leap SDK files, and a robot to control. Only one step remains before we start moving the robot with touchless control, and that is to open the leapPicaxe.py robot controller, and edit the serial port settings.
Edit the controller settings just like you did last time: change the portNumber to your serial port ID number, and swap the commented lines if you're using Linux. Once that's done, save the file, and plug in your PICAXE programming cable and your Leap Motion. Run the file, and if no error messages appear, then you should be ready to run a robot!
Step 18: Drive the Robot!
Now that our file is working as expected, its time to drive the robot. Plug the USB end of the PICAXE programming cable into your PC, and plug the 3.5mm jack end into the robot. Next plug the Leap Motion sensor into the computer. Turn the robot on, and run the Python robot controller. If all goes well, you should be able to tilt your hands backward and forward to run the wheels on the robot.
The controls for this version of the robot controller are very simple. Tilt your left hand forward or backward to drive the robot's left wheel forward or backward. Tilt your right hand forward or backward to drive the robot's right wheel forward or backward. Level out either hand to stop its respective robot wheel.
Congratulations! You have successfully built a PICAXE-driven robot, controllable by simple hand gestures. Give yourself a pat on the back, and go click that 'I Made It!' button with pride.
I appreciate any comments and suggestions you may have; I will also try to help you if you have any issues with your robot, just leave a comment or send me a message and I'll see what I can do.
hammad25 made it!
We have a be nice policy.
Please be positive and constructive.