Let's Make Robots!

Project #1 - object avoiding robot that's afraid of the dark

MFMC's picture
Navigate around avoiding obstacles via IR, and is "afraid of the dark", moves away from dark places using an LDR
OAR_Code_280212.txt8.86 KB



This is my first project, a basic object avoiding robot based on the Start Here tutorial, but using Arduino. 

It uses an IR sensor to detect objects and decides which way is best to go, when it goes somewhere dark it will turn around.

I've made some changes to the code and the build of the robot, changed the build to make it a little bit tidier, but also needed to make it a bit easier for the robot to turn. Main reason to change the build though was that everything was stuck together using foamtape, which was fine, but this included the arduino. I'm planning to use the arduino on my next project, but still want to be able to easily switch it back to this bot, so have made it sit in a little box so it's easier for me to swap it round.

My (inefficient) code is attached, and list of components below:

  • Small Servo (9g) - SparkFun
  • Arduino Uno R3
  • AdaFruit motorshield for Arduino
  • 120:1 Gearmotor (x2)
  • 2x wheels (bottom of coke-cans)
  • Infrared Ranger (GP2Y0A21)
  • 1x 9v battery (to power Arduino)
  • 4x 1.2v 2900mAh NiMh AA batteries (to power motorshield)
  • A low resistance LDR
  • A lot of wires and an on/off switch


Current problems/Next steps:

- One of the motors tends to spin faster than the other, I'm trying to correct this through the code, but want to find out whether there is a hardware or wiring solution to this issue as well. 

-- Still correcting this in the code, which seems to work well now

- The distance returned by the IR sensor is not in a specific unit of measurement, I'd like to correct this to measure cm's, but am not sure how the voltage that goes through is different from when the robot is connected to my pc via USB versus when it's driving around autonomously. 

-- Drew up the graph of the distances the IR measures and their metric equivalents, so I at least now know that the robot will change direction when something comes in closer than 15 centimeters 

- The 9v battery powering the Arduino board only has about 15minutes of good life in it, not sure if this is something to do with the nature of the battery, Arduino, or my code. 

-- Soo, none of those! Based on the comments I'm blaming my servo, but have also got a new rechargable 9v which seems to last a while longer

- Planning to add some tactile push button switches to detect when the robot drives into something, just waiting for these to come through in the post now...

-- Am actually not going to bother as the LDR is pretty much fulfilling this function now


So I'm basically considering this project to be done now (I know it is actually not completely done and there's lots of other things I want to try to do with it, but I also want to move on to a new walker-type robot project, I'll put a link in here once I've decided what it's going to be and do and stuff. 




Comment viewing options

Dan M's picture

As far as the distance returned by the IR sensor not being specific units of measure, I just took a series of measurements and made a graph of it. The actual measured distance versus the number returned by the sensor.

The following picture is an example only, because different sensors return different readings. My graph may not work for your sensor(s).


In my case, I did this in Imperial measurements (inches) but I also included centimeters on the right hand end of the graph.

As you see, I set an object at different distances, then took multiple readings,

subtracted the actual reading from 165 which was the highest number returned,

and finally plotted them on a graph.

From there, you can use the results within your programming.

[ If  adjustedreading = 85 then cm = 15           --and so forth.]

NilsB's picture

Dan, can you make a tip out of your comment? This info and mapping table is a first class citizen if you ask me.

You may even turn it into a descreete function distanceInInch = f(rawSensorValue). 

MFMC's picture

Ah that's brilliant, easy enough a solution! I'll go and do my own readings to duplicate your chart right now actually! thanks :) 

Dan M's picture

You're welcome. I'm glad it helps.


ignoblegnome's picture

Are those wheels made from beverage cans? Now that's how to recycle!

Nice combination of behaviors. 

MFMC's picture

Yes! And using a bit of the side of the can to support the back of robot, which is also a cradle for the 9v battery...now looking to see if I can wrap the whole thing in a can...

JAX's picture

Recycle aluminum :)

Motors tend to spin at different rates, even the same models. No two are exactly the same. Often people use PWM on the motor driver's enable lines while watching the wheels with encoders to keep the rotation equal. I doubt the difference in speed has anything to do with your setup.

9V batteries are puny. While they put out relatively high voltage the current capacity is very low. Good for running low power devices. Not so good for energy sucking things like motors.

edit- I reread that you have the 9V for the ardu only. Fifteen minute runtimes do seem short, but again, I submit to you that 9V's are puny.

reedit - after watching the video I see your servo is working constantly. There is your culprit for the short battery life.

The voltage returned by the sharp sensor is related to the voltage of its power source. In this case it returns a fraction of 5V based of the distance. With it plugged into the USB it will be working off 5V. While your 9V has the power, the bot will also be running at 5V autonomously. There will be no difference in the readings returned.

MFMC's picture

Haha, cheers, the wheels came about due to sheer desperation. I had the whole thing built and programmed, but no wheels that would fit. It was one of those beautiful moments where I decided to have a drink and leave it all for another time, but then realised the can I was drinking from was the solution to my problem. I ended up drinking a lot of coke that day. 

Thanks for the comments on the battery, great to know Arduino converts my 9v back into 5v (I am very new to all this!), and will have a think about how I can reduce the power used by the servo (it's constantly turning (1degree at a time, basically using the servo testing code from arduino.cc), but I'm only using input data from the sharp sensor at 3 points). I'm also not sure I fully understand how the battery supply to the arduino and the motor shield works, as the arduino seems to provide the motor shield with some power, to the servo for example, but the motors only appear to kick in when I plug in the additional pack of batteries. 

birdmun's picture

has a 5v regulator that converts 7v(?) to 12v into 5v by way of converting the excess power required into heat. Your arduino requires 5v for its logic as do the inputs for the chip/s on your motor shield. The 5v that comes from the reuglator on the arduino is also fed to the motor shield for the logic chips that are on it. Look up the L293 or the 754410 quad half h-bridge chips. Their pinouts are the same and their function is the same. It might help you understand some things about your robot.

MFMC's picture

Cool, thanks, have been having a read and feel like I'm mostly up to speed now with the logic of the chips, just not entirely sure where the actual excess voltage goes? I can see there are two heatsink pins on the chips but not sure what these would be connected to? or where the heat goes? Should I be including a physical heatsink somewhere to avoid overheating, or won't these voltages generate enough heat for that? Sorry, that's a lot of questionmarks!