Let's Make Robots!

Arduino Object Avoidance with PING and Hacked RC Car


Finished Bot Here: http://letsmakerobots.com/node/26101

Before I begin I am a Senior Electrical Engineer student in college. This is a project im working on for my design class. I am very new to arduino and its language. Having said that here we go:

Working on an object avoiding vehicle using Arduino and Ping ultrasound sensor. I hacked an old rc car. Basically removed all the guts and replaced it with a breadboard and the arduino. I am still using the 9V rechargeable battery pack that came with the car to power the 2 motors through an L293D chip and a regular 9v battery to power the arduino. When I turn on the robot it runs well and responds while im holding it in the air so the code seems to be working. But then things go bad. Here are the issues i've come across so far.

After a while it begins to slow down and stop as you can see in the video. Battery is charged

When changing directions it badly stutters. will post another video soon showing this.

Not enough torque to run on carpet. Runs on tile but slows to a stop when changing directions.

When I grab one wheel to stop it while its running the other slows and eventually stops. Not too much of an issue there but just wondering why it happens since they are running independantly? gueesing it has to do with the overheating of the L293D.

I left the original two capacitors going across the motors attached, tried removing them and had same issue so I put them back. Also the L293D (can handle 0.6 amps) chip gets VERY hot while its running and starts to smell like its burning. I ordered a chip called  ‘SN754410‘ from Texas Instruments. It can handle double the current of the L293D and has the same pin layout. It comes in next week so hopefully that will make a difference. Will update when it comes

I have the PING sensor mounted on a servo. After these issues are addressed I would like to add a section to the code to allow the servo to pan while its driving so it will avoid it from running into anything at any angle OR when it comes to an object look left and right and choose the best direction. I am horrible at code writing so any links to similar codes I can use or if anyone can help would be greatly appreciated.

Will post more pictures and videos as soon as I can.

L293D Chip Layout

Heres how I connected everything: PINS

1 to pin 9 on Arduino board, 2 to pin 3 on Arduino board, 3 to motor1 (either + or -) it wont matter as its DC, 4 to the gnd rail on the breadboard, 5 to the gnd rail on the breadboard, 6 to motor1, 7 to pin 4 Arduino, 8 to power (+) rail., 9 to pin 10 Arduino, 10 to pin 5 Arduino, 11 to motor2, 12 to GND rail, 13 to GND rail, 14 to motor2, 15 to pin 6 Arduino, 16 to power (+) rail





Heres the code I used. Credit to @lucky_larry on twitter for most of the code. I made a few changes to allow it to work for my PING sensor. The original was written for the SRF05 ultrasound sensor.


#include <Ping.h>

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

const int pingpin = 12;                         // ping pin (digital 12)

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(pingpin, OUTPUT);                     

  pinMode(pingpin, INPUT);                      

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

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

    readings[thisReading] = 0;



void loop() {

  pinMode(pingpin, OUTPUT);

  digitalWrite(pingpin, HIGH);                  // send 10 microsecond pulse

  delayMicroseconds(10);                        // wait 10 microseconds before turning off

  digitalWrite(pingpin, LOW);                   // stop sending the pulse

  pinMode(pingpin, INPUT);

  pulseTime = pulseIn(pingpin, 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);



Any comments, help, ideas or input is definately welcome. I also hope this can be used to help someone who is also having similar issues. 


Comment viewing options

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

I was definately thinking that might be an issue once I got it working correctly. They're spinning real fast in the video. I'm not too familiar with PWM.  About to do some research tho

I know this was said in the shoutbox, but to immortalize for future reference, "Don't throw away stuff".   If you continue to do robotics in the future and you don't have a huge budget - go to Thrift Stores, Goodwill, or Second Hand Stores - and buy the RC toys.  They are cheap because:

1. Someone got a better toy, lost interest, or someone else in the family got so irritated by the toy they threw it out.

2. (More commonly) - they lost the remote control for it.

Either way there is a cornucopia of good circuitry (made and tested as consumer products) out there.   I usually pick up a $5 toy, look for the H-bridge (common/simple one is just using large NPN Transistors), power it up and test the connections.  You can then solder wires to it and plug it into the micro-controller.  If you use the toy intact, it's components are made for one-another, so you should not experience overheating & stalling problems...   With the micro-controller you can start adding stuff to turn it into a mutant, sensors, servos, etc......

Maybe, I should do a tip/walkthrough sometime...

Good Luck !

If you decide to do a walkthrough Ill read it.. still new to all of this

When you get the motors running properly, you might try to adjust the center of gravity of the bot so that you could use the torque of the drive wheels to lift the front wheels before turning. It would definitely be an interesting video to watch if you could manage it. :)

When it runs on the ground the front wheels shoot up in the air when it takes off before it slows down and dies.. thats a good idea to consider. thanks

Measure the current draw of your motors. That'll let you confirm if you are trying to draw more power from the L293D motor driver, though if it got hot and smells like it is burning, I think you have your answer.

More importantly, this will tell you if the motors will work within the limits of the SN754410 driver you ordered. As an engineering student, you should understand that it is important to know the requirements and limitiations of your system components. Don't make the same mistake and burn out your new chip. Determine your typical and maximum current draw of your motors, and buy/build a motor driver to match.

Also, I have a question on your mechanical design. Are you intending to turn with a turning servo up front, like a regular RC car? Your code looks like you driving the to motors differentially to turn. If you are going to turn differentially, you will have trouble unless you match the angle of your front wheels to the relative turn radii of your rear wheels. If you don't, you will be trying to drag your front wheels sideways during the turn, and it will not work well at all.

I am currently looking into this issue on my Why Tri robot.

Yea I do need to measure the currents the motors require. thinking the SN754410 which can handle 1.2 amp will still not be sufficient enough. I am planning on doing differential turning where one wheel goes forward and one goes backwards resulting in a turn. I think it would be powerful enough to drag the front wheels on turning. I pretty much have been building this robot on trial and error. I jus looked at your Why Tri robot. The front wheel you made using cardboard as a bracket seems like a great idea! Thanks

Try this. Forget the motor drivers and the microprocessor. Wire up one of your rear wheels directly to the battery. Leave the other disconnected. This should result in a turn with the center on the non-moving wheel, right? Except you will likely find that the front wheels drag so badly that it won't turn well (or at all).

Now try hooking up that disconnected rear wheel directly to the battery, but in reverse. So it should spin on a center point between the two rear wheels, correct? Except it probably won't. It will be trying to drag the front wheels sideways, which will work terribly.

You could turn the front wheels to match the rotation, as I'm planning with Why Try. However, this is not a great solution. I'm only trying it for the fun of learning. Instead, replace both front wheels with a roller or caster, so that the front of the car does not offer significant resistance in any direction. Now you can use differential steering with little/no problem.

Yup.. Saw your video on steering. Might just replace the wheels with a roller if theres too much resistance.