Let's Make Robots!

My First Robot Dave

Navigate around a table top using IR
Visuals.py_.txt4.95 KB
DaveTheRobot.pde3.51 KB


I've had my Arduino for over a year and had done very little with it until recently.  Mainly because I didn't have the tools or knowhow to actually build anything the Arduino could interact with.  Eventually I decided to buy some Technics Lego and the Lego Power Functions so I could build something involving wheels and motors that I could hopefully control from my Arduino.

I soon realised that I'd also need a motor controller, so I ordered a L293D chip. My plan was to have my robot detect the edge of a table by coupling an LED and an LDR, but my initial tests proved unreliable, so in the end I ordered a Sharp IR Range Sensor.

After building various different contraptions from Lego and wiring them up to the Arduino, I couldn't get anything to run slow enough, even using the lowest PWM the motors would take, so I ordered some extra Lego gears from eBay and managed to construct what you see in the video labelled Dave the Robot 1.  It still runs too fast, but I felt sure that I could get this down to a sensible speed once hooked up to the Arduino using PWM.


So I stripped the ends of some of the Lego wires, figured out what did what, popped the relevant wires into my breadboard, wired them to the L293D and up to the Arduino, and taped the whole thing together with electrical tape.  I added the Sharp IR and wrote some simple code to have Dave (as I've christened him) drive forwards til he saw the edge of the table, then do a 180 degree turn and do the same again - see video Dave the Robot 2


As pleased as punch as I was with my first robot, simply turning around every few seconds made it look like he was programmed to do just that.  I wanted him to make the decision on which way to turn and to only turn as much as he needed to.  The problem was, I didn't know what data was coming back from the IR sensor at any given time, so debugging Dave would prove to be a blind nightmare.

I decided to have Dave send his distance measurements through the serial connection and then write a Python script on my Mac to read these transmissions and display them visually.  This helped me fine tune the threshold I was using to detect the edge of the table and allowed me to add in a lower threshold for detecting walls and other objects in his path. I also discovered that I needed to be taking an average of the distance readings, as the odd spike could push the readings through the threshold and stop Dave when it shouldn't.  In the video labelled Python Visuals you can see the readings display in blue, the average in purple and the two threshold lines in red.


I changed Dave's code so that when he reached the edge of the table he would stop and turn left, then turn right, essentially scanning the area in front of him.  I also added a second graph to the Python visuals that displayed what he saw when he did his scan, which you can see an example of here:

The left section is generated as he turns from his starting position to the left - you can see the graph drop as the table comes into his view in the right hand side of the first box.  The next two sections show him scanning from left to right - you can see the range drop off the edge of the table and then come back on as he reaches the right.  Finally, he rotates himself back to his starting position and you can see from the final section that the range drops off again.

I was hoping I'd be able to come up with an algorithm by looking at these visuals, but I'm no mathematician and so nothing sprang to mind.  In the end, after viewing the graphs Dave produced from different positions on the table I decided that simply working out which side of the graph had the lowest average reading would mean Dave had (probably) encountered more table on that side of the scan, and that choosing that direction to go would look right most of the time.

So I have him work the averages out as he does his scan and then at the end of the scan he rotates in the chosen direction until he sees table, then he drives off and repeats the whole process, visible in the video Dave the Robot 3.

Kind of.

I've attached both the C Arduino code and the Python code to this project.  It's a bit dirty and could do with some refactoring, but should be readable.

I'm starting to feel the limitations of Dave now, and want to progress to building my second robot so I can improve the areas of limitation.  I've included a few paragraphs below detailing some of my thoughts and concerns and I'd love any advice you can give me on them.


I've about reached the limit of what I can build with the Lego I have, so I'm either going to have to buy more Lego, or figure out another way to build a body for my robot.  I've read about using polystyrene sheets to build things, which seems like a material I might be able to manipulate, but I'm concerned about it's strength and how to screw or otherwise attach things like motors to it. Does anyone have any experience or advice here, or can suggest another way I can build things without too many tools and experience?

I'm not very happy with the way I've had to tape my Arduino to the top of my breadboard and then tape the whole lot to the robot, so the huge breadboard has to go.  I'm considering buying the following Arduino shield which you can stick a mini-breadboard to, that should at least get rid of the need to tape the Arduino to a breadboard.



Dave's movement is a bit wonky, probably due to the fact that he has two little pegs touching the table to keep him balanced on his two wheels. I've seen that other people on LMR have used a castor with their two wheel robots, but have no idea where to aquire one, or how to build one and how to attach it.

I'm also considering buying a new set of motors, brackets and wheels so I can use encoders to keep track of how much the wheels have turned.  How much use are encoders in a robot though? I assume I'll be able to calculate how far it's travelled and how much it's rotated, but surely this will differ depending upon the surface it's driving on, carpet vs wood?  Worth it?


The battery pack that comes with the Lego takes 6 AA batteries, which I'm using to power the two motors and the Arduino via the VIN pin.  I suppose I could replace it with a 9V battery, but I don't know my electronics well enough to figure out if that will be suitable to power two motors and the Arduino - any advice?