Let's Make Robots!

Understanding the Mr. General Robot Kit

Navigates and tracks objects using Infrared
Mr_General_Nano.zip3.21 KB
Mr._General_4.bas17.22 KB
TDA2822m_stereo_power_amp.pdf141.68 KB
LM35_temperature_sensor.pdf303.72 KB
speakjetusermanual.pdf405.9 KB
Tone.zip9.68 KB
Mr__General_ATmega8_Edge_Detect.zip3.23 KB
Mr__General_ATmega8_Object_Detect.zip3.25 KB

Mr. General is based on my original LMR robot Bot 08M which was my attempt at a "Start Here" robot using the picaxe 08M processor. This walkthrough will explain how the sensors work, how the eye works, how the servos work and how the code works.

As of September 2011 we are releasing a new version which includes an ATmega8A, ceramic resonator, USB interface that can plug directly into the breadboard and a greatly improved manual. This interface can be bought seperately and should work with any Arduino clone that does not have an onboard USB interface. I have now updated this tip/walkthrough to include the new wiring diagram for ATmega8A as well as the new sample codes.

Note that there is one version of the code for object detection (your corner sensors aimed outward) and one for edge detection (your corner sensors face downward). In the case of edge detection you may need to use a black marker pen or some of the supplied heatshrink to reduce the sensitivity of the phototransistors (black). The sensitivity will depend a lot on the colour and material of the floor / table that the robot is running on.

Notice in the attachments is a new "Tone" library for the Arduino. This version, thanks to RobotFreak and Brett Hagman works on the ATmega8 as well as the other Arduino boards. The version in Arduino 0022 and earlier does not. Simply delete the old Tone library from the library folder and add this one instead. As later versions of the Arduino IDE have the tone command included you must add "#include <Tone.h>" to your code for this library to be selected.

The sensors:
In late 2008 Frits described a simple sensor using an LDR and one or more LEDs. This is a simple but effective sensor. More importantly he pointed out that your processor should take two readings, One with the LEDs on and another with the LEDs off. Then measure the difference! This will be explored later when we look at the code.

I expanded on this idea using phototransistors instead of LDR's as they are quicker to respond and using them in pairs to track movement. I ended up with this very simple IR motion tracker.

When you look at the corner sensors on Mr. General they are just a simple voltage divider. The principle is that the phototransistor works much like a LDR (light dependand resistor). As more light shines on the transistor it allows more current to flow. The voltage across the fixed resistor changes with the current flowing.

When the sensor is connected to a digital input then it will read as a 1 when the voltage goes above 60% of the CPU voltage and will read as a 0 when it falls below 40%. The actual voltage levels may vary slightly depending on the processor used. When connected to an analog input then the voltage will get higher as the object gets closer. The advantage of using the sensor on an analog input is that your code can then have more than one level. You might have:

  0 - 200       nothing there
  201 - 500    something there but no chance of collision. Curious? have a look.
  501 - 800    try to avoid this
  801 - 1023  better back up or something quick!

The Eye: consist of 4 sensors almost the same as those on the corners of the main PCB. The main difference being that there are two phototransistors in parallel. This doubles the sensitivity. Increasing the brightness of the IR LED could also increase the sensitivity but uses more power and flattens the batteries quicker.

Below is a full schematic of Mr. General using an ATmega8A. A schematic using the Picaxe 28X1 can be found further down. Click on the schematic for a full sized image which you can then save onto your computer.

Servos - how they work:
Mr. General uses two standard micro servos for the pan/tilt assembly that makes his head/neck. This pan/tilt kit was designed right here on LMR. These servos consist of a small motor, a gearbox and a variable resistor connected to the output shaft. A small built in control circuit uses the variable resistor to determine what position the output shaft is in. A control pulse tells the circuit where the shaft should be. The control circuit uses this information to adjust the speed and direction of the servo motor until the position of the output shaft matches the position given by the control pulse.

This pulse is between 1mS and 2mS in width with 1.5mS being the center position of the servo. The pulse repeats every 20mS. Different servo manufacturers have slightly different standards and as such some servos will work with pulse widths outside of the 1-2mS range. Pulses that exceed these limits may force your servo to try and move beyond it's range of travel and possibly damage the servo.

Continuous rotation servos:
Mr. General uses two continuous rotation servos for movement. A continuous rotation servo is similar to a standard servo except that the output shaft can rotate continuously. For this reason there is no variable resistor that monitors the position of the output shaft like there is in a standard servo. The variable resistor is replaced with two fixed resistors that simulate the variable resistor set to the center position.

Now a pulse of 1.5mS will make the motor stop. A pulse greater than 1.5mS will make the output shaft turn clockwise and a pulse less than 1.5mS will rotate the shaft counter clockwise. The closer the pulse width is to 1.5mS the slower the motor will turn. In reality no two servos are the same and as such 1.5mS is only the theoretical center position. Temperature can also affect this center position causing the motors to drift slightly in one direction on a cold morning and the other direction on a hot day.

To connect either type of servo to Mr. General simply plug it into the 3 pin male headers provided with the ground wire (usually black or brown) to the outer edge of the PCB. The 3 pin male headers already have power supplied (4.8V) so only a single wire is needed to connect the signal wire to the processor. All 3 pin male headers have a 3 pin female header connected in parallel so that a jumper wire can be used to connect the servo to the breadboard.

Programming Mr. General with the USB interface:
The USB interface from DAGU plugs directly into the breadboard. Because the sample code uses D0 and D1 for the pan servo and eye IR LEDs these must be temporarily disconnected when programming. Once the program is loaded the USB interface is removed and the pan servo and eye IR LEDs are reconnected.

Pololu may sell this kit with a similar USB interface. Use DTR for your reset. It is a good idea to use a 1K resistor in series with the TX and RX pins to limmit the current

Making a programming cable:
To program the ATmega8A while it is in the breadboard will require a homemade programming cable. There are many USB to serial interfaces available. The photo below shows a typical example of what you might find in an office supply store. Alternatively you could buy a USB/serial breakout board such as the FTDI basic from Sparkfun.

No matter which interface you use your cable will need 4 wires. Gnd, TX, RX and DTR or RTS. Normally I use DTR but if that doesn't work then try RTS. A full list of pinouts for the D9 serial socket can be found here: http://en.wikipedia.org/wiki/Serial_port

In order for the Arduino IDE to reset the ATmega8A prior to programming a 100nF capacitor needs to be in series with the DTR and reset pins. This capacitor allows the DTR pin to briefly trigger a reset allowing the bootloader to accept a new program.

I have shown 1K2 resistors in series with TX and RX. This is a simple precaution as D0 and D1 can be input or output depending on your program. D0 should be disconnected from the eye and D1 disconnected from the servo prior to program upload to ensure a reliable data transfer.

The Code:
The original "Mr. General" kit was designed around the Picaxe 28X1 although many different processors can be used. The program has since been re-written for Arduino and the kit is now normally supplied with the ATmega8A which is equivalent to the old Arduino NG with ATmega8.

If you have an Arduino Nano then this will also plug directly into the breadboard. The Arduino Nano has two extra analog input. This allows the 4 corner sensors to be used as analog sensors.

I have attached sample code for ATmega8A, Arduino Nano and Picaxe28X1. If you are using a Picaxe 28X2 or 40X2 then you might be able to use the X2 conversion wizzard to convert the program.

Arduino users should note that there is a seperate tab called "IO_pins.h" where all the IO pins are defined. This tab is effectively your wiring diagram as it tells you which pin of the processor connects to what piece of hardware on the PCB.

Regardless of which processor you use the structure of the code is very simple.

The Main loop does these things:
1. Updates the LED pattern aproximately 5 times a second. This causes the corner LEDs to chase around the PCB.
2. Updates the position of the neck servos and the speed / direction of the continuous rotation servos.
3. Reads the compound eye.
4. Calculate new pan / tilt servo positions and continuous rotation servo speeds to maintain object tracking.
5. Read the corner sensors and adjust continuous rotation servo speeds to prevent a collision.
6. Optional behaviour routines such as boredom counters can be added here.

Reading the IR sensors:
Simple IR sensors such as those used on Mr. General cannot measure distance accurately and can be tricked by objects of different colours. For example a small bright white object in the distance and a large dark object nearby can give the same readings depending on how well they reflect IR. Sunlight has a lot of infrared light an can easily trick or blind these sensors. Background light such as this is called ambient light.

To help overcome some of these limitations we use the method proposed by Frits of reading the sensors twice. The first time we read the sensor with the IR LEDs on. This gives us a reading that is equal to the ambient light plus IR LED light that has reflected from a nearby object. The second reading is with the LEDs off so that we read only the ambient light. When we subtract the second reading (ambient IR) from the first (ambient IR + reflected IR) we are left with a value that represents only the IR light that was reflected from a nearby object. This technique allows the robot to track your hand movements while ignoring the IR light coming in from a nearby window.

Tracking Motion:
Using the pan servo as an example the robot must first read the left and right compound eye sensors and subtract the ambient light as previously mentioned.

The distance/reflectivity of the object must be taken into account to prevent servo overcorrection. This is done by using a variable called panscale wich is the average of the left and right readings divided by LRscalefactor which is adjusted to allow for servo speed and tracking sensitivity. Too sensitive and the servos will overshoot causing them to jitter.

A value called "leftright" is the absolute difference between the left sensor and the right sensor divided by the panscale to allow for distance and servo speed. This value is then added or subtracted from the old servo position to provide a new servo position.

Tilting of the head works the same way as the panning of the neck. Arduino users must forgive the clumbsy code as it has been translated from Picaxe basic which has limited math functions and no negative numbers.

Following an object:
In order to track an object properly the robot needs to do more than turn it's head. It must follow with the body. If the neck pans too far left or right then the wheels are driven causing the robot to turn towards the object.

Distance is calculated by averaging the readings of all 4 sensors (up, down, left and right). This is not an accurate measurement of distance but is good enough for the robot to judge things such as which direction is blocked and which direction has room to move. The robot tries to maintain a set distance from the object it's tracking so as not to loose it's lock on the object while avoiding a collision. The wheels are driven forward or backward as required.

Collision avoidance:
Finally the PCB corner sensors are read in the same fasion as the eye sensors. One reading with the LEDs on and one with the LEDs off. This causes all the corner LEDs to pulse briefly. Because of the speed this is happening at the green corner LEDs appear to be dimmly lit.

After the readings have been taken any objects detected are compared with the direction of the motors. If necessary a motor will be stopped to avoid a collision.

Light pattern and sensor indication:
If a corner sensor detects an object or the light pattern determines that that corner LED should be on then that LED will be re-lit causing that corner LED to appear bright rather than dim.

Additional behaviour:
In the original picaxe code I also experimented with a boredom counter which monitored how much time had passed without tracking a moving object. If too much time passed then the robot would wander off in a random direction until it found a new object. If that object failed to move in a given time the robot would wander off again.

Add other counters to monitor things such as frustration if the robot gets stuck in a corner. Excitement if the the robot finds a fast moving object or multiple moving objects. By adding several counters and behaviour routines your robot can appear quite life like.

IR Communications:
If a suitable communications protocol was written then it should be possible for more than one robot to communicate with each other using the IR sensors as IR transcievers.


Picaxe version using the 28X1:  Click on the image below for a high resolution version that can be saved onto your computer.

The Picaxe basic code is actually the original code that the Arduino version was derived from and has some extra features. In the main loop there is a EdgeDetection subroutine as well as an ObjectDetection subroutine.

If you have your sensors pointing down to detect the edge of the table for example then detecting something with your sensors is good and the robot can move freely. If nothing is there then the robot must avoid the edge and the corner LED will light up.

If you have your sensors facing outward to detect objects such as a wall then detecting nothing with the sensors is good and the robot can move freely. If something is detected then the robot must avoid it and the corner LED will light up.

You must choose one subroutine and rem the other out depending on the configuration of your PCB corner sensors. The logic of these routines may be a little confusing as port C has been split with half of it configured as input and half as output.

Picaxe users will note I have included the resistors necessary for programming the picaxe 28X1. A 3.5mm stereo headphone socket with a few jumper wires soldered on will allow you to plug in a standard Picaxe programming cable. See the "Serial Download Circuit" shown in Manual 1 for more details.

Some Clever people like Gareth have made simple breadboard interfaces like this:

This photo was pinched from Gareth's Picaxxo project.

Resonators and Crystals:
Originally Mr. General (both Picaxe and Arduino) used a 3 pin, 16MHz ceramic resonator. This is the cheapest and easiest to use. There are 2 ways this can be installed on the breadboard. The first way is to plug it in near the chip and use 3 jumper wires to connect it to the processor. This usually works ok as long as the jumper wires are not too long.The tracks of the breadboard and the wires act like small capacitors and inductors. These can affect the frequency of the clock or prevent it functioning all together if there is too much.

I prefer to bend the legs of the resonator so that it connects directly to the pins of the processor. This is more reliable. As the resonator pins are a bit short it is even better if you solder some extensions onto the pins. Picaxe 28X1, ATmega8, ATmega168 and ATmega328 all have the GND pin (8) and XTAL pins (9, 10) in the same configuration making this easy.

If you are using Arduino then a 16MHz crystal and two 18pF or 22pF capacitors can be used for better timing accuracy. This might work with picaxe although I have never tried it.

Additional hardware:
The main PCB of Mr. General has all the spare space converted to prototype board. This area is ideal for adding additional circuitry such as line following sensors, speakjet chipset, amplifiers etc. There are 2 great "How to's" for the speakjet here:
http://letsmakerobots.com/node/4305 this is Ant's post using the picaxe.
http://letsmakerobots.com/node/13210 this is Droidbuilder post using the Arduino and TTS256 companion processor.

An ideal amplifier is the TDA2822M. This is a small 8 pin stereo power amplifier that will work with supply voltages from 1.8V to 15V. The two amplifiers in the chip can be connected in "bridge mode" to make a single amplifier with a greater power output. I have attached the datasheet which includes sample schematics showing how to use the amplifier.

As the continuous rotation servos stop position is sensitive to temperature, a temperature sensor such as an LM35 could be added between the servos. Your processor could use this to adjust the stop position of the servos.

Additional voltages:
You may choose to use 3.3V devices. The area under the breadboard is an ideal location for a 3.3V regulator. As the power rail on each side of the robot can be isolated and changed you could connect the rail on one side of the robot to 3.3V and switch all the servos to the other rail which might remain connected to battery +V.

I hope this guide will help owners of the "Mr. General" robot kit to get more enjoyment from it through a better understanding of it's functions.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Hi I'm a newbie at this.I would really appreciate your help. Right now I am enjoying your explanations and trying to understand what I just did. I have just finished assembling the Mr General kit. It comes with the atmega 328 chip on a breadboard plus they included a pololu usb to serial (cp2102 chip) plus a schematic on how to connect to the Atmega. Also I downloaded SI cp2102 drivers for Windows 7 as my computer's OS. However my CD comes with the picaxe prog editor instead.and the corresponding picaxe .bas program.

1. So to program I downloaded the Arduino0022. Would your pde file (found here) work. Which one the At8 or the nano?

2. Now which arduino do I choose from the tab (TOOLS-BOARD-?) since I know I do not have an uno or any of these boards with so many having the atmega328 option????

3. Then what do i choose in TOOLS-BURN BOOTLOADER-???

thanks for your support.



There are 2 versions of the software for Arduino. The ATmega8 version was tested using an ATmega8 but should work on any Arduino. Just set your board to suit the chip you are using.

There is also an Arduino Nano version that takes advantage of the 2 extra analog inputs available on an SMD ATmega 168 / 328 chip. This should work with any Arduino that has 8 or more analog inputs.

Once again choose the board you have. For example the Nano can come with either a 168 or a 328 processor but the code would also run on an Arduino Mega.

I am enjoying reading your "Understanding Mr General"-

Still having problems with mine, though. The Mr General kit came with the Atmega 328p but still having a hard time (probably as i mentioned earlier) with the driver of the pololu usb. So I'm trying to test it now with the Arduino Uno (atmega 328p) which has only 4 analog inputs (on the diagram connects to the "eye" of the robot).. i'm left with digital inputs. where do i connect the sensor wires leading to the  four corners? this is from the infor that atmega 328p and 168 have only 4 analog pins.



First of all if your kit was supplied with an ATmega 328p then that should make no difference, the pin connections are the same as shown in the wiring diagram for the ATmega8A.

The ATmega328p has 6 analog inputs, not 4. The only difference with the 328p is it has more memory.

The Arduino UNO also has 6 analog inputs.

The wiring diagram and code above uses 4 analog inputs for the eye and 4 digital inputs for the corner sensors. Only Arduinos with the SMD version of the chip such as the Arduino Nano have 8 analog inputs.


hi oddbot,

you were right. it's pin configuration of io. used and adapted your code...the corner sensors now work. IR eye tracks something now on the pan but not yet perfected the tilt and the following. still, maybe my hardware connections and servo adjustment. will work on it. 

The reason for my confusion on analog inputs is i have an Adventure Bot from Dagu and it's supposed to be using an at168. i presuppose the schematic and pin config of 168 is the same as that of 328. But on the adventure bot code given, in the io_pins.h the analog inputs for the IR eye are 4,5,6,7 and the senspins are connected to analog inputs 3,2,1,0. The problem with the adventure bot is the corner sensors fail to detect. All else work perfectly. But when I applied your part of the code for the corner sensing. everything goes awry.

I always thought that aside from the 168 and 328 difference the schematic should be the same for both mr general and adventure bot. Now I don't know.

Although Adventure Bot is based on Mr. General the pin configuration and code are different. One reason is that Adventure Bot uses an SMD chip with 2 extra analog pins. This allows the corner sensors to use analog inputs so that they can ignore ambient light. The corner sensors should work with the correct code as they are tested in the factory. You may need to adjust the alignment of the LED and phototransistor on each corner as they can get bent easily.

thanks for the correction. sorry I mistook... lapse of the mind. it is six not four. I stand corrected.

Re-checking my wiring as you said.. seems ok.... Just one thing... my pan/tilt servos are going awry at start up. the head looks straight to the right and stays there for some time, while the body goes in complete circles (counterclockwise) then after some time the head looks up (90 degrees) and then goes all the way to 180 as it bends his head back for some time and then goes back again to the first position (looking right).  what could be wrong, do i need to change the numbers in the program for the servo?

Then also my four corner sensors are not detecting. the Leds do not blink full when it is bumping, nor does the body stop.

Make sure you are using the right code! The new wiring diagram works with the new code. The older code may have used different I/O pins to control the servos.

You definitely need to adjust the program to suit your robot. For example the values for "leftmotorstop" and "rightmotorstop" need to be adjusted so that the wheels do not move when the robot is playing music at the start.

The fact that your head is turning like that indicates that either the eye thinks it is seeing something. Check your wiring for the eye.

Sounds to me like you need to study the basics. Read through the Learning section of the Arduino site.

thank you very much for that link.

now i realize that one problem i couldn't connect is the driver for my pololu usb-serial (cp2102).Arduino software could not recognize it.  It seems they don't work right away with windows 7 according to the pololu site (unlike xp and vista). I'll continue exploring.. any experience on this would be appreciated.