Arduino Avoidance Bot

Today we are going to build an Arduino-powered robot that can sense and avoid obstacles. The goal is to get your little bot to maneuver around the classroom quickly. First, you will assemble the bot, and then you will modify some code in order to calibrate the bot’s reactions. Ready? Let’s go.

The Parts and Batteries:

First, you need to make sure you have all of the parts.

You should have a kit with:
1 Arduino – aqua colored board
2 bunches of male-female jumper cables – one orange, yellow, green, blue, the other black, white, gray and purple. KEEP THESE CONNECTED IN BUNCHES
1 red male-male jumper cable
1 ultrasonic sensor
1 motor driver – red colored board
1 small screwdriver
1 bot body
1 bot top deck
5 AA batteries
1 9 volt battery holder (not pictured)
1 9 volt battery (not pictured)
1 USB cord (not pictured)
laptop (not pictured)
electric tape (not pictured)

Before we start assembling anything, let’s just take a short tour of the bot body, which, as you may have already noticed, has already been partially assembled.  In addition to the battery compartment, note the on/off button and the four loose wires coming out of it. DO NOT PULL ON ANY OF THESE COMPONENTS.

The on/off button is inside the yellow circle in that photo. The four motor wires are circled in green – the bot has two DC motors in its base, and each has a positive and a negative line coming out of it. Yours should have green tape flags on two of these wires to help you distinguish between the positive and negative wires. The other two wires, circled in red, are the power wires – the red one gives us power from the batteries and the black is the ground wire.

Let’s take a closer look at that ground wire:

Note how this black wire actually has two pins – one halfway down and one at the end. This wire is going to be plugged in twice. Again, do not pull on these wires – they are all soldered in place but solder is not stronger than the average college-aged human!

The first step is to put the AA batteries in. Note that the orientation of these will be strange, so pay attention to the markings in the compartments and compare it to the photo to make sure it have it correct. Here is a photo of how it should look – see how the second and third batteries from the left are facing the same way:

Wiring the Motor Driver

Now we will wire the motors to the motor driver. A quick tour of the motor driver:

There are two sorts of connections on this board- the blue ones that you need to use the screwdriver for, and the pins that you will plug wires onto directly. The yellow circle indicates the pins you will use later. The red circle is where the power cords from the bot will go in a moment. Right now, we are going to focus on those two green circles- that is where we will attach the right and left motors. (In fact, in this photo, you can see the wires already attached.)

Put the four motor wires into the side ports with the green-flagged wires farthest from the bot, like this:

You might need to loosen the screws first in order to get the wires in. Once they are in, tighten the screws so they are held in place:

And your motors are all set! On to the power for the driver. Grab the black and red wires we looked at earlier and put them in the top ports of the driver board. Attach the MIDDLE of the black wire to the middle port and the end of the red to the right hand port:

Now take the red male-male jumper wire from your kit and put one end of it in the left hand port:

Now we are just going to test to make sure it is getting power. A light may have already come on, if so, you’re good. If not, push the button on the left hand side of the bot. A red light should turn on:

IF THE LIGHT WILL NOT TURN ON: make sure the batteries are all making contact on both ends. You might have to wiggle them a bit. If this does not work, raise your hand for help.

Once you have power to the shield, turn the bot off to preserve power.

The last piece you need to connect to the motor shield is the wires that will talk to the Arduino. Grab the black-white-gray-purple set of wires (keep them connected!!) and connect them to the pins at the front of the motor shield – the ones circled in yellow earlier. Color matters here! Make sure the black wire is on the pin closest to the power wires, like in this photo:

The motor shield is all wired up! On to the ultrasonic sensor.

Wiring the Ultrasonic Sensor

This is the little bit that looks kind of like binoculars. Take the orange-yellow-green-blue set of wires and attach it to the ultrasonic sensor like this:

The colors matter here! Make sure the blue wire is connected to the pin labelled GND- the ground pin, green to ECHO, yellow to TRIG and orange to VCC. Now put the other end of the wires through the top of the bot body, like so:

Now fit the bot top on top of the rest of the bot. The ultrasonic sensor should be directly opposite the motor shield. There are three pegs that fit into three narrow rectangles, like so:

Fit the sensor in front:

Then cut a few pieces of electric tape and secure the sensor in place.

Connecting the Arduino

Everything is ready to connect the Arduino. Arduinos are microcontrollers- a board like the motor driver board, except it is general purpose because the main chip on it can be programmed however we want. You are going to program your Arduino later. For now, we are just going to wire it to the bot. Take a moment to look at the board and identify all of the ports. There are two rows of black pins on the long sides of the board. These are female pins, as opposed to the male pins we saw on the ultrasonic sensor and the motor shield. They are all labelled in tiny font- some with letters, many just with numbers. Make a note of the locations of these pins: ~10, ~9, 7, ~6, ~5, 4, 5v, and VIN, as well as the three pins marked GND. You will be using those.

If you know how to follow a wiring diagram, this is what we are aiming for:

There are ten wires that you need to plug in- the four from the ultrasonic sensor, the four from the motor driver, and the two power cords. Start with the four motor wires – the black-white-gray-purple wires. These are going to attach to pins 7, ~6, ~5, and 4 – the black wire to pin 4, the grey to ~5, the white to ~6 and the purple to 7. Since the motors get power from the battery pack, these wires are just carrying data.

Now the wires from the ultrasound sensor – blue to GND (you can use any of the three GND ports if you want to be fancy), yellow to ~9, green to ~10, and orange to 5v. The yellow and green wires are carrying data, while the orange and blue are actually bringing power to the sensor (5 volts, which is why the orange wire is plugged into the 5v port!).

And now the power – end of the black wire into another of the GND ports (remember this is the one that had two parts, and the middle is plugged into the motor driver), and the red wire to the port marked VIN.

And now- on to coding! Plug the USB into your Arduino like so:

The Arduino software:

If you are using your own laptop, you will program the Arduino using the free Arduino software, Arduino IDE, available either as a web or desktop application. I recommend using the online Arduino Web Editor, which will require that you make an account on their website. If you would prefer not to do this, you can download the desktop version. This will require that you install drivers, if you are using a Windows based machine.

Once you have your chosen version of the IDE working, follow these instructions to get your Arduino UNO set up and upload and run your first program, or “sketch.”

Uploading the Avoidance Bot code

Now that your software is set up and communicating with your Arduino, we are going to take a moment to upload the code for today’s project. Click the “New Sketch” button to create a new sketch window (in the desktop IDE, go to File>New). Delete all of the code that appears in the new sketch window. This is important!

Replace it with this code instead. It is a lot of code, so make sure you cut and paste the whole thing:

int trigPin = 9;
int echoPin = 10;
int fwdright= 5;
int revright = 4;
int revleft= 6;
int fwdleft = 7;

void setup() {
pinMode(revright, OUTPUT);
pinMode(fwdright, OUTPUT);
pinMode(revleft, OUTPUT);
pinMode(fwdleft, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance =(duration/2)/29.1;


else if(distance<20){

Click the checkmark button to verify the code- this is just checking that you pasted it correctly. If the code does not verify try cutting and pasting again. Once it works, click the “upload” button – the right-pointing arrow button above your code – to push it to your Arduino, and wait for it to say “finished uploading.”

Power, more power!

The final step is to attach a 9v battery to run the Arduino. Put the battery in the holder, turn the battery holder off, then slot the holder on top of the Arduino and plug it into the barrel jack.

Put your little bot on the ground, turn on the 9v battery first, then press the motor switch. The bot should take off and avoid obstacles!

What is this pile of letters anyway?

Let’s dig in to this code a little bit. If you look over the code that you uploaded, you might notice there are five sections separated by whitespace- sort of like five paragraphs. The first paragraph is made up of lines of code that all start with the letters “int.” This section is definitions – int, in this case, stands for integer – a way to store numbers as variables. Here, we are defining certain terms to make the later code more readable and easier to manipulate. We’ll return to that later- for now, just note that the numbers here are the same as the pins that you plugged wires into on the Arduino, and that the names we give them refer to the object we plugged in (forward left [motor], echopin for the echo from the ultrasonic sensor).

The second paragraph starts with “void setup.” This is a function. All Arduino code has two built in functions – the setup function, which always runs first, and the loop function – to that in a moment. This setup function tells the Arduino which pins to turn on and what to use them for – here, they all have to be used for output except the echopin, which is set to input. But note that this code does so using their variable names. Why? Well, in theory you can use any pins on the board to control your motors, and if you use variable names then if you change the pin, you only have to change it in one place and the rest of the code will still work.

(Note also that this section initializes something called Serial- this is for debugging.)

The third paragraph starts with “void loop.” This is the other build in Arduino function – built-in means that the Arduino already knows what it is, and it does not need to be defined in the code. Where the setup funciton ran once, the loop function runs over and over again. The third, fourth, and fifth paragraphs are all actually part of the loop function, and they work together to help your little bot move forward and turn if it senses an obstacle in its path. The third paragraph here is the most complicated and we won’t go through it line by line – it is enough to know that this is what tells the Arduino how to get information from the ultrasonic sensor.

(Note again that it refers to Serial here- if you plug your Arduino into a computer, this function will actually print data to your screen to help you debug it.)

The fourth and fifth paragraphs work together. They both begin with “if” statements- that is, they are only brought into play if a particular condition is met. The fourth paragraph says that as long as the distance between the bot and any obstacle it senses is greater than 20 centimeters, then the bot moves forward. It does this by sending power to the forward right and forward left motors- note in the code that it turns those motors to HIGH.

The fifth paragraph says if the distance is less than 20 centimeters, then the bot will turn. It does this by reversing its left wheel- so the power to forward left goes to LOW and power to reverse left goes to HIGH.

Advanced Coding

It’s alright if you don’t fully understand the code yet- your bot understands it just fine! But if you want to, you can learn, here, how to manipulate the code a little bit so that we can change how the bot moves. We will start by making it turn in the opposite direction. Go into your code, go to the last chunk of code, and replace each HIGH with a LOW and vice versa. Your last chunk of code, that fifth paragraph, should read thus:

else if(distance<20){

Now plug your bot back in, verify, then upload the code. Turn the bot on and see how it behaves. It should turn the other way when it senses an obstacle, and then go back to going in a straight line.

Now the advanced part. You could, if you wanted, write code telling your bot to turn right or left at particular intervals. You could, even, if you felt particularly brave, remove the sensor entirely and pre-program a sequence into the bot instead. Go here for a hint of what that would look like.