How to build a robot: Robot with a brain! Part I

Part I tutorial shows you:

  • Common terms you may encounter
  • How to use servos (continuous rotation and standard)
  • How to use an Analog Infra-Red Ranging System
  • How to use a Passive Infra-Red sensor
  • How to make your own NiCd/NiMh battery charger (at your on risk)
  • Pictures of how I assembled my robot
  • Sample code, schematics, PCBs

Bill of Materials (8-22-2008)

When I was growing up, I always wanted a robot. I don’t know why I wanted a robot, but I believe my inner-being knew that one day I was going to build my own robot. It doesn’t have to do anything helpful. It doesn’t have to serve a purpose. I just want a robot.

Now I am older. I can have my robot. It doesn’t do much, but it is a start to a platform that can be expanded upon. Building robots can be a great learning experience if you are new to microcontrollers. You will learn about many peripherals and sensors that can be used to ‘feel’ the world.

For those of you who are new to the microcontroller concept I have some information for you. The simplest and least expensive way, in my own qualified opinion, is to start with the Picaxe family of microcontrollers. Buy a protoboard and the USB download cable at hvwtech.com. The USB cable is a one-time buy for $22 USD. You will use this cable for all of your projects. For about $22 USD, the ‘Axe Stack 28 offers you a 28 pin microcontroller that can store up to 1000 lines of code, be programmed thousands of times, and it has multiple I/O and ADCs, which you will learn about with this robot tutorial. I’ve never used the BASIC STAMP, but I believe that the Picaxe chips are much cheaper to use in the long run over the B.S. (The best starter kit for Picaxe can be found here for about $70 USD. It is worth it.) Beginners to microcontrollers can learn some of the basics of what a microcontroller is for less than $50usd.

Let’s start with some basic terms:

  • I/O – Input and output.
    Inputs are digital sensors that are in one of two states, high or low. High is +5 volts and low is zero volts. A microcontroller uses inputs for external information. The most common form of input is through a switch. When the switch is open the input pin is usually grounded; no current flow. When the switch is closed current flows, and the input pin is pulled high to +5 volts.
    Outputs are similar to inputs except that they send information to the outside world. Again, they can either be high or low. The most common outputs are used to light LEDs. Outputs can also be used to drive motors, servos, and also communicate with other peripherals (See communication in action as you build a digital clock).
  • ADC – Analog to Digital Converter
    ADC is another type of input used by some microcontrollers. These are special because they can be used to detect variations in voltage. A common type of input used for ADCs are potentiometers.
  • BASIC- BASIC is the programming language that is used to program microcontrollers. It is the easiest language to learn to get you started on making your electronics projects have brains. To program in BASIC, you must have a compiler, which may come included with certain kits. (Other languages that are used to program microcontrollers are C and Assembly)
  • Compiler- Compilers are used to convert your programming into something a microcontroller can understand. Microcontrollers use machine language, which is just a bunch of digital 1s and 0s, highs and lows. If we were to try and write a program of just ‘digital’ language, it would take us a long time to make our microcontrollers do stuff. Once you have compiled your program you need to download it to your microcontroller.
  • Programmer- This is what you use to actually program your microcontroller. The programmer downloads your compiled program to the IC.
  • IDE – Integrated Development Environment
    IDEs are where you write your program, debug your program, test your program, perhaps compile your program, and download your program to your microcontroller. The only IDE that I use for microcontrollers, thus far, is the IDE provided by Picaxe. It is free to use. Find the program at their site, and download it if you are interested. Picaxe

Step-by-Step Tutorial

Pick and choose what to use!

  1. The first step before you robot shop is to consider what you would like your robot to do. Then do some research to find parts that will accomplish the task.
  2. Next, Give your ‘bot a name.
  3. Buy parts. You can use my list as a starting point. I bought the stuff, but may not use it. Be sure to know what you are buying. If you do not know something, Google about it. (Example – check forward current and voltage for LEDs, whether parts are DIP or SOIC, 3mm or 5mm, etc.)
  4. First, install some wheels. You can use a motor or servo. I am going to use a servo since it works all by itself without an H-bridge. I cut holes in the box and secured the servos with #4 screws. Your best bet is to mount your motors near your expected center of gravity of the ‘bot to ensure easy turning. I did not do this and I wish I had.
    CutoutFittedTesting
  5. Servos are special motors. They come from Parallax.com in two types: Standard and Continuous. Standard servos are mainly used for steering, while continuous rotation servos are used for movement. One last thing, standard servos can be modified to be continuous, but this is not for beginners or for me, yet.
    Next, read the manual on your continuous rotation servos. They will have a procedure to center them. (Watch your servo code, sometimes the code to go forward for the left will not be the same as the right. You’ll see. Experiment with a simple servo program.)
    Connect each servo to its own 7805 voltage regulator. They can draw a low of current and make regulators get hot. Also, be sure your ‘Axe has its own regulator. Connect the white wire of the servo to a 330 ohm resistor and the other end of the 300 ohm resistor to the output pin of the Picaxe. Then connect the black wire to the ground of one 7805 regulator. Connect the red wire to +5 volts of the 7805. Once connected you can write your code. Check the set point of the servos with the following code:

    MAIN:
    PULSOUT SERVOPIN,150 ‘SERVOPIN IS THE OUTPUT PIN NUMBER. 150 IS 1.5 MILLISECONDS
    PAUSE 20
    GOTO MAIN
    Start with your left servo. If your servo is spinning then you need to adjust the set point pot. Now swap the servo connected to the pin and set that one.
    SYMBOL SERVOLEFT = 0 ‘LEFT SERVO IS CONNECTED TO OUTPUT 0
    SYMBOL SERVORIGHT = 1 ‘RIGHT SERVO IS CONNECTED TO OUTPUT 1
    SYMBOL COUNTER = B0 ‘DECLARES THE VARIABLE B0 AS THE COUNTER
    MAIN:
    ‘SERVOS SPIN ONE WAY
    FOR COUNTER = 1 TO 100
    PULSOUT SERVOLEFT,180
    PULSOUT SERVORIGHT,120
    PAUSE 20
    NEXT COUNTER
    ‘SERVOS SPIN THE OTHER WAY
    FOR COUNTER = 1 TO 100
    PULSOUT SERVOLEFT,120
    PULSOUT SERVORIGHT,180
    PAUSE 20
    NEXT COUNTER
    GOTO MAIN
  6. The next step is to mount our turret to the ‘bot. The turret will be the range finding system. This will give our ‘bot the power to avoid some obstacles. It is best to use a standard servo for this. Continuous rotation servos don’t always return back to the center. The turret will face forward while driving forward, and if the distance measured is within collision range, the ‘bot will stop and then look left and right to find the best route of travel.
    SYMBOL UPPERSERVO = 2 ‘PIN THAT SERVO IS ON
    SYMBOL COUNTER = B0 ‘COUNTER
    SYMBOL PULSES = B1
    LET PULSES = 75 ‘NUMBER OF STEPS TO TURN
    MAIN:
    GOSUB RIGHT ‘ROTATES RIGHT
    PAUSE 500
    GOSUB TOCENTER ‘RETURN TO CENTER
    PAUSE 500
    GOSUB LEFT ‘ROTATES LEFT
    PAUSE 500
    GOSUB TOCENTER ‘RETURN TO CENTER
    PAUSE 500
    GOTO MAIN
    TOCENTER:
    FOR COUNTER = 1 TO PULSES
    PULSOUT UPPERSERVO,150 ‘CENTERS STANDARD SERVO
    PAUSE 20
    NEXT COUNTER
    RETURN
    RIGHT:
    FOR COUNTER = 1 TO PULSES
    PULSOUT UPPERSERVO,75 ‘ROTATES TO RIGHT ON STANDARD SERVO
    PAUSE 20
    NEXT COUNTER
    RETURN
    LEFT:
    FOR COUNTER = 1 TO PULSES
    PULSOUT UPPERSERVO,225 ‘ROTATES TO LEFT ON STANDARD SERVO
    PAUSE 20
    NEXT COUNTER
    RETURN

    The turret servo shares power with the microcontroller and other low current devices since it is only used intermittently.
  7. Now let’s look at the AIRRS. It is easy to set up as you only connect 3 wires, red to +5 volts, black to ground, and blue to an ADC pin on your microcontroller. We will test for certain distances to see what happens using the DEBUG command. Copy and paste this code into the Programming Editor and download it to your microcontroller. Be sure to change the ADC input to the one you are using.
    MAIN:
    READADC 1,B0 ‘READADC PIN#,BYTE
    DEBUG B0 ‘SEND INFORMATION TO COMPUTER TO VIEW
    PAUSE 1000
    GOTO MAIN
  8. You’ll notice that the far objects result in a low ADC value and close objects result in a high value. I was able to get my ADC value to 143. Object avoidance is best around 70. An ADC value of 70 is about 8 inches from the sensor.
  9. Mount a solderless breadboard inside your ‘bot. Use double-sided tape to stick it to the inside.
  10. I have been buying parts as I run into problems. Here is one problem that you can learn from. I want my servos and sensors to be easily disconnected from the breadboard. The solution is to buy a 14″ LCD Extension cable from Parallax.com (p/n 805-00012) and cut it in half. Now you can splice the wires to two different sensors that do not have connectors. Then just use the 3 pin headers to make the connections to the breadboards.
  11. Let’s try another sensor. Take the PIR motion detector. It has 3 connections that need to be made: ground, 5 volts, and signal. The signal wire goes to an input pin or an ADC pin. I chose to use an ADC pin since it is easy to get a reading. You could also use an input and use an ‘if…then’ statement. I just wanted to make sure that it worked, and it is easier to debug using the ADC.
    MAIN:
    READADC 1, B0
    DEBUG B0
    GOTO MAIN

  12. The next step is to make or buy a rechargeable battery and charger. The safest thing to do is to buy this, but the die-hard hobbyist is going to want to make one. !!!     IT’S DANGEROUS! IT COULD BURN A HOUSE DOWN! IT COULD BURN ME! IT DID BURN ME! I offer no warranties for damage or injury as a result of making or charging batteries. Make at your own risk.    !!! With all seriousness, working with batteries can be dangerous. The batteries that I used are 3300 milliamp-hour NiMh sub-C cells. I used 6 in series to get an average 7.2 volts. If they are shorted together they make fire hot enough to melt the solder holding the wires to the tabs. I already did it so I know that it can happen. Carelessness is something that should not be exercised when handling batteries like these. If you are comfortable with soldering batteries in series then give it a shot. Oh, and buy batteries from eBay. They are much cheaper. The next step is a charging circuit. I will have a post with some fast-charger circuits, but for now just stick with simplicity and safety, somewhat. You’ll need a few things to charge a 7.2 volt pack: 9v wall-wart rated at least 800mA, 1N4001 diode, 120ohm resistor 1/4w, 5ohm resistor 5 or 10w (Yes, 5 or 10 WATTS!!! See why at the end of this paragraph.), small LED, multimeter with at least 1Amp current rating and 25 volts. OPTIONAL: Laser thermometer to check temperature of big 5ohm resistor. You can make this on a breadboard, but I actually reverse engineered this from a cordless drill so I already had a PCB made. I also had a 600mA meter that I was given so I put it in a Shack box. Why do I need a 5 watt resistor? They cost $2.50! Can’t I just use a little one? The answer is no! Power is dissipated in the form of heat. Most circuits you build will not generate much heat. A simple LED, 350 ohm Resistor and 9V battery circuit only dissipates a max of .140 watts, which a 1/4 watt [.250] resistor is perfect for.
    {I=E/R,      P=IE,      P=E²/R}
    (The LED drops some voltage (E). I used a 2 volt LED for this problem so E=9-2)
    P=7²/350      P=49/350      P=.140Watts
    This charging circuit is much different. As a worst case scenario you have 600mA indicated on your amp meter, which is a lot considering 300mA is sufficient for charging in 11 hours for the batteries I am using. C/10 is what the charge rate is defined as. 3300mAhours/10 hours = 330mA. Anything faster than that should be charged with digital circuitry.
    P=IE     P=.600*9   P=5.4Watts  The resistor gets really hot.
    P=.300*9   P=2.7Watts  The resistor still gets hot.
    Slow Charger Schematic
    Slow Charger PCB
    FrontBack
  13. After making your battery pack and charger you can install it somewhere. I chose to mount another box to the back of my ‘bot. You can do whatever you want according to your design. I am attaching a model airplane tailwheel to mine so that it can caster on it. You could use casters from a local hardware store, but they are usually made poorly. The ones that I used caused my ‘bot to drive in circles. If you choose to mount your battery in your main chassis, then you can also mount your casters to the main chassis. Keep in mind that it’s your ‘bot so you can design it any way you wish.

    The tailwheel pictured here is not a good choice. It does not return to dead center which causes the ‘bot to drive in arcs. There will be more on my choice of tailwheel in part II of the robot tutorials.
  14. TIME TO PLAY - By this time you have enough code and assembly to run your ‘bot. Your ‘bot should be able to drive in multiple directions, and should be able to avoid couches using the AIRRS. You’ll find that is doesn’t do a good job about avoiding small objects around the wheels. There are many ways to fix this. You could use momentary contact switches, sonar, infrared, or AIRRS mounted to the front of the ‘bot to detect objects. A simple program snippet for avoiding obstacles using simple momentary contact switches:
  15. MAIN:
    //other code here
    IF PINX = 1 OR IF PIN Y = 1 THEN GOSUB OBSTACLEAVOID
    //more code here
    GOTO MAIN
    OBSTACLEAVOID:
    //code to go backwards? spin around? fly?
    RETURN
This entry was posted in Electronics and tagged , , , , , , by robbie. Bookmark the permalink.

About robbie

I am an electronics enthusiest and a ham radio operator (W1RCP). I like to play with electronics. It's fun and educational. I looked forward to working in the engineering field in the future. I have a BS in Electronics Engineering Technology from DeVry University. I also have an Associate's degree in Marketing Management from Moultrie Tech, and a diploma in Electronics from MTC.

Comments are closed.