Posts Tagged ‘Obstacle avoidance’

Obstacle avoidance Arduino robot – build your own larryBot


So after 5 previous versions that had various flaws, I now have an Arduino robot that actually works and although basic is very cheap – although there a probably a few more flaws so please point them out to me but this is a good start on how to make your own robot.

In order to catch up, please see my previous posts below, describing the problems that the other 5 versions had, how the h-bridge chip works and using the SRF05 ultrasound distance sensor.

larryBot – Arduino robot versions 0.1 to 0.5 lessons learned
Control a DC motor with Arduino and L293D chip

Arduino SRF05 Distance Sensor

Now that you’re up to speed, lets start by fixing the flaws in the previous version, this was the case that my motors were drawing way too much current and the L293D chip from ST Micro couldn’t output enough current for each motor.

So, I replaced the chip with the snappy named ‘SN754410‘ from Texas Instruments. This has EXACTLY the same 16 pin layout as the L293D chip and all of the same features except that it can output 1.2 amps per channel rather than the now tiny 0.6 amps of the L293D. Pin configuration diagram below is the same for the L293D as it is for the SN754410, I recommend the SN754410 Arduino comination.

L293D Pin layout

Great I’ve now got more current to my motors, but their stall current is still at over 2 amps, I could add a heatsink to the chip and pass more current through it, but instead I got some more efficient motors than the Mabuchi FA-130’s that came with the Tamiya gearbox. These motors are made by Solarbotics and are their RM3 series which fit perfectly, can handle 4 times the voltage but use a fraction of the current – typically at 9v they use just over 1 amp. Perfect.

Having corrected this, larryBot v0.6 was go! I still faced a lack of power to the DC motors – either because my batteries were running low or not able to supply the current. But since my new motors could run up to 12 volts (instead of the puny 3v of the originals) I decided to use a 9v battery to power them instead of my 4 AA’s.

Watching larryBot move is great, even on carpet and with the tank tracks 9 times out of ten he can climb small obstacles or has enough traction to shunt them out the way. Anyway enough waffling – here’s how he’s made…

The Arduino Robot Tracked Chassis

You could use anything you want really – construction sets, your own custom fabricated chassis etc… But since I’m cheap I managed to get a pile of foamboard for my chassis. I can waste and reuse as much of this as I want so its no problem if I make a mistake or want to improve it. Also in theory this leads to rapid prototyping, so when I do decide to fabricate a chassis I know exactly where the best places are for holes, mounts etc…

The robot chassis parts and tools:

Small Phillips/ cross-head screwdriver
Craft knife
Assorted nuts and bolts – A good set of M series nuts and bolts
Foamboard 5mm thick – 1 A4 sheet is plenty
Tamiya gearbox 70097 – assembled in mode A
Tamiya track and wheel set 70100
Elastic bands (normally dropped by the postie)

Sizing up the robot base

First of all the size of our chassis design is dictated by a few things. The axle length: our tank tracks need about 5mm clearance so the space on the axle is roughly 65mm wide that I can mount on. Next we have the length of the tracks and how many wheels will be used, I kept my track footprint small so my chassis length didn’t need to be much bigger than the gearbox. Which leads on to gearbox positioning – the Tamiya gearbox I have is roughly 75mm in length and the shape of the tracks will dictate where to position the gearbox as the driving wheels are attached to this. The final consideration of course is mounting all the sensors, battery packs, breadboard and the Arduino board.

In my attempts so far I have a base that is just longer than twice the length of the gearbox (175mm) which gives me space at the front for sensors and space at the back for batteries. I then mount a smaller piece of foamboard on top of this that then houses the gearbox and spaces it far enough above the running wheels for the tracks at the bottom – also giving enough tension in the tracks for them not to slip off (unlike larryBot v0.4). From here I can continue to bolt on additional structures to position the breadboard and so on.

So using this knowledge you should be able to size up and cut the foamboard to the dimensions you need – a craft knife will be more than enough to cut this board. To make the holes needed for your screws and bolts just use a small Phillips/ Cross-head screwdriver to bodge a hole through – it won’t take any effort, then you can drive the screws through this guiding hole. If you have washers then use them but the foamboard seems to be able to support all the hardware fine.

Attaching the running wheels and tank tracks

First mark out the position of where you want your wheels, very important as you don’t want them wonky!


To mount the running wheel axles on to the chassis I used a couple of small hexagonal bolts for each side of the axle and then used the glue gun to fix them to the chassis – the best way to do this is to put the bolts on to the axle, use a small amount of glue to hold the bolts in place and then use a shit load of glue over the bolts to secure them properly.

When adding the wheels to the axles, don’t push them all the way on as these axles are slightly shorter than the Tamiya gearbox which will cause you problems with the tracks.

Mounting the Tamiya gearbox, DC motors, sensors, breadboard, Arduino and batteries

To attach the gearbox I just used the screws supplied with the gearbox and bolted this to my smaller piece foamboard. I then in turn bolted this to the main chassis using 4 long bolts and a series of spacers and nuts in between the layers to given the correct spacing and adjustment for my drive wheels.

For the SRF05 distance sensor I just used some blu-tack/ modelling plasticine to hold it in place for now.

The breadboard I mounted above the gearbox, which for this I just fixed it on top of 4 long bolts which then in turn attached the gearbox base. The Arduino board currently then sits on the breadboard held on by the multitude of wires running from it and the power supply cable.

And for the batteries, since I scrapped using the 4xAA’s to power the motor I only had to worry about two 9V batteries, 1 of which was my DC power supply for the board. I fixed them to the chassis just using an elastic band, since I’d want to get to them easily enough.


Simple Arduino Robot Circuit

The parts list doesn’t differ much from my other tutorials for motors and L293D. But I did find it was troublesome to get the parts from the same supplier, so be aware that you may need to look at multiple suppliers and postage may get expensive.

Robotic parts list

2 x Solarbotics RM3 motors
SN754410 motor driver chip
SRF05 Ultrasonic distance sensor
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
2x 220nF multilayer ceramic capacitor (Y5V)
2 x 50V 10uF Capacitor (although I’ve not used them here)
2.1 mm coaxial DC jack
2 x PP3 9Volt battery
PP3 9Volt Connector
9Volt battery holder


You can see that the circuit is pretty simple, nothing actually that fancy, I have the SRF05 using the +5v, GND and digital pins 12 and 13. The SN754410 then uses the digital pins 9 and 10 to control each channel – these can use PWM to do speed control, then there are the switch pins on the h-bridge that go to digital pins 3,4,5 and 6. The spare GND is used to join the GND connection between the motor power and Arduino power supply. Here are the instructions for the 9v battery DC supply. If you want to use the extra 50v 10 uF capacitors then these sit on the power supply for pins 8 and 16 on the SN751140 respectively.


Arduino Robot Code

Nothing much has changed from the larryBot v0.1-0.5 sketch except that I’ve altered the detection distances as I have a much faster response time from the robot.

const int numOfReadings = 10;                   // number of readings to take/ items in the array
int readings[numOfReadings];                    // stores the distance readings in an array
int arrayIndex = 0;                             // arrayIndex of the current item in the array
int total = 0;                                  // stores the cumlative total
int averageDistance = 0;                        // stores the average value

// setup pins and variables for SRF05 sonar device

int echoPin = 12;                               // SRF05 echo pin (digital 2)
int initPin = 13;                               // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0;                    // stores the pulse in Micro Seconds
unsigned long distance = 0;                     // variable for storing the distance (cm)

int motor1Pin1 = 3;                             // pin 2 on L293D
int motor1Pin2 = 4;                             // pin 7 on L293D
int enable1Pin = 9;                             // pin 1 on L293D
int motor2Pin1 = 5;                             // pin 10 on L293D
int motor2Pin2 = 6;                             // pin  15 on L293D
int enable2Pin = 10;                            // pin 9 on L293D

void setup() {
  // set the motor pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enable2Pin, OUTPUT);
  // set enablePins high so that motor can turn on:
  digitalWrite(enable1Pin, HIGH);
  digitalWrite(enable2Pin, HIGH);

  pinMode(initPin, OUTPUT);                     // set init pin 3 as output
  pinMode(echoPin, INPUT);                      // set echo pin 2 as input

  // create array loop to iterate over every item in the array

  for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
    readings[thisReading] = 0;

void loop() {
  digitalWrite(initPin, HIGH);                  // send 10 microsecond pulse
  delayMicroseconds(10);                                // wait 10 microseconds before turning off
  digitalWrite(initPin, LOW);                   // stop sending the pulse
  pulseTime = pulseIn(echoPin, HIGH);           // Look for a return pulse, it should be high as the pulse goes low-high-low
  distance = pulseTime/58;                      // Distance = pulse time / 58 to convert to cm.
  total= total - readings[arrayIndex];          // subtract the last distance
  readings[arrayIndex] = distance;              // add distance reading to array
  total= total + readings[arrayIndex];          // add the reading to the total
  arrayIndex = arrayIndex + 1;                  // go to the next item in the array                                 

  // At the end of the array (10 items) then start again
  if (arrayIndex >= numOfReadings)  {
    arrayIndex = 0;

  averageDistance = total / numOfReadings;      // calculate the average distance

  // check the average distance and move accordingly

  if (averageDistance <= 10){
    // go backwards
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);    


  if (averageDistance <= 25 && averageDistance > 10) {
    // turn
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
  if (averageDistance > 25)   {
    // go forward
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);     


[ad#Google Ad in content]

Some problems you may face – if like my you don’ t have a spare 9V battery connector to hand check this connection if nothing is happening – I used blu-tack to hold my wires in place so it’s a bit temperamental.
Check that your motor wires are properly in contact with the motor terminals if you haven’t soldered them again using some blu-tack or tape is handy for getting a good connection.
Motor’s are under strain – your tracks are too tight.
Tracks come away from the wheels – check your tracks are not too loose and that your running wheels are in line with the drive wheels – the Tamiya gearbox is slightly wider than the Tamiya track and wheel set axles.


I’ve gotten a fairly cheap robot that avoids obstacles, next plan is to extend it to sense various things – for instance detect motion and move towards it, or a light/ heat source. The robot costs are quite high if you factor in the Arduino board and if you don’t have any of the parts – but this can be broken down and used for many other projects so you’ll get a lot of reuse out of these bits, but I reckon that the total cost is around £70-80 in total, so fairly cheap when compared to other bots. Of course if you don’t want tracks (?) then you can just use wheels instead, Tamiya do also make wheels that will fit the gearbox.

Just in case you have trouble getting parts, here’s a small list of people that can supply the various bits – although none of them will have the full set. Shipping from the states is an option, but check the shipping costs as it may negate the cost savings. Please let me know of other sources, the list is in no particular order.

Sparkfun – USA: motor controller and Tamiya parts
Pololu – USA: Tamiya parts and motors
Techbotics – UK: Tamiya parts – just about cheaper than getting parts from Sparkfun/ Pololu in the USA
Active robots – UK: motors, SRF05 but generally overpriced on everything
Rapid Electronics – UK/EU/USA: most component parts and hardware
Farnell – UK/EU/USA: SN754410 chip and most components but shit for orders if your billing and delivery addresses are separate
Mouser – UK/EU/USA: SN754410 chip and most components
SK Pang – UK: SN754410 chip but dodgy VAT calculations (charges tax on shipping as well) few other parts here.

If you need an Arduino board, I reliably found a seller on ebay from Hong Kong that will sell and ship you aboard for far less than paying for it the UK – downside is it takes about a week to arrive.