Introduction: Arduino Wall Avoiding Robot

This instructable assumes very little with respect to prior knowledge.

If there are any areas you feel could be improved or clarified, please feel free to message or comment and I will update.

Here are all the details you need to produce a simple wall avoiding robot. Along the way you will have the chance to take a look at (and modify) the arduino code needed, get a bit of info on header files and look at some troubleshooting.

If you think this is complex, I can assure you that it's not. You can look at this project as an assembly project if you wish - however, there is room to get more in depth too.

Step 1: Robot Video

Step 2: What It Is and What You Need

Take a look at the video to see how the finished product works.

What you're seeing is a little bit of the fundamentals of what a microcontroller is used for.

The arduino board itself is pretty much there for the purpose of facilitating the 32 pin ATMEGA chip. This is a microcontroller chip - microcontrollers are used for taking data in, performing calculations and putting data out. In this case, we're using an ultrasonic sensor to measure time between a "ping" and "echo".

The code performs calculations to convert time to distance - based on the speed of sound. This distance is continuously calculated and monitored. If the distance goes below a user-defined level, the robot changes it's outputs to the 2 motors.

Below is the list of parts you will need - I've stuck to known components that are commonly used as these tend to be better supported for users who are kicking off their "maker life"!

  • Arduino Uno
  • Arduino motor shield
  • 9V battery & clip/holder
  • HC-SR04 ultrasonic sensor
  • 2 x geared dc motors with wheels

I will also include files for 3D printed partss and for a laser cut chassis. If you have access to neither of these tools, you will find that you can make a decent chassis from cardboard and double sided tape.

Step 3: Here's Some Files

First, you can download all the required files in this step.

They include:

  • 3D printing:
    • HC-SR04 mounting from user Shaun Saggers on thingiverse (a brilliant bit of design IMO)
    • DC motor mounts modeled by myself and available from thingiverse. If you are making your own chassis, I have also modeled these mounts which need a different motor.
    • Instead of a castor I've modeled this "third wheel" riser to let the robot move around with less friction.
  • Laser cutting:
    • The attached chassis (robot template) pdf file can be modified as you see fit for your own use.
  • The Arduino "NewPing" library.
  • The attached Arduino (Robot_Build.ino) sketch.

Step 4: Here's Some Software

You'll need a couple of bits of software too. Or rather, you'll need one bit (the Arduino IDE) and may find the other (Inkscape) to be one of the most useful pieces of software you'll use as a maker (plus it's free and open source!)

The Arduino IDE is available for linux, windows and mac. FabLabs aim to use free and open source software where possible and as a lover of linux myself I can give help mostly for that platform.

Step 5: Your NewPing File

The newping header file is called by the arduino sketch (btw - an arduino "sketch" is simply an arduino file full of your code).

Open the Arduino IDE on your computer, and open the Robot_Build.ino sketch file - at the very top of the code you will see the text:

#include

This means that the sketch will call some functions that are described by the NewPing.h library. Without this line, you will get errors in verifying and compiling the sketch code. To see the effect of excluding a bit of code you don't have to delete it. Instead you can prefix any bit of text with 2 forward slashes // and hit the verify button (on the top left) to check the code.

You will need to place the NewPing.h library into a folder from which the sketch will read it.

On Ubuntu, create the folder "sketchbook" in your /home/$username/ folder. In the sketchbook folder, create a "libraries" folder. Finally, create a "NewPing" folder in the libraries folder and save the NewPing.h file within this folder. That might seem a little confusing so I'll try to clarify. You need to create all the necessary folders to end up with a location like this: /home/$username/sketchbook/libraries/NewPing/ within which you can place your NewPing.h file.

This structure is a good idea to keep to for any header files you may need. There is a sketchbook folder already in your arduino install folder - but if you do any updates to the IDE you may lose them. So having this ~/sketchbook/libraries/ structure means you hold on to all the hearder files you might end up needing for this and other projects.

Step 6: Sending Your Code to the Arduino

First, you'll need to plug in your Arduino Uno as per the included image. After opening the Robot_Build.ino file you can then select Tools > Serial Port where you will see a list of connected devices. Make sure there's a tick beside your Arduino.

The Arduino IDE can be used with a wide range of different boards and chips. Make sure you select Arduino Uno from the list at Tools > Board > Arduino Uno.

You can now upload your open sketch by hitting the upload button. The one crudely outlined in red in the image!

Any errors (there shouldn't be any) will show up in the console at the base of the screen.

Step 7: Connecting Up Your Hardware

The assembly of the chassis is relatively simple and should be apparent from the above images.

The electrical connections are also quite simple.

The first step is to connect your motor shield onto the Arduino Uno. You will notice that the pins line up exactly to be received by the Uno itself. Next you'll need to connect the motors and 9V battery holder.

Let's start with the battery holder. The red wire will need to be connected to the Vin terminal with the black going to the GND terminal. It's worth noting that sometimes terminals can be marked + or - instead. "+" means the same as Vin, Vcc, V+. "-" means the same as GND.

To wire the motors; you will notice that because I have used extensions to the motor wires the colours don't match with the usual red/black colour scheme. Motors are like bulbs in that power can be applied to them in either direction. You don't notice the difference with a bulb, but a motor will rotate in different directions if a battery is switched around in it. When you power up the robot later it's default (in the code) is to drive forward. If this is not what happens in reality there are a few methods of correcting this so don't fret!

The HC-SR04 ultrasonic sensor has 4 pins; Vcc, GND, trigger (Pin 7) and echo (Pin 6). All sensors need power to (Vcc) and back (GND) from them. The trigger is the pin that will receive a signal from the arduino send out an ultrasonic "ping". The arduino code will log the time at which this signal is sent out. The echo pin listens for the reflection of this sound. This is a nice example of bio-mimicry.

Each of the 4 pins are marked on the sensor side and that's it for the electronics. Next, we'll look at some potential problems and how to troubleshoot.

Step 8: Powering Up & Testing

With everything connected you can now connect your 9V battery.

This robot is pretty simple. The code supplied defaults to both wheels going in the same direction to power it forward. If it senses anything within 20cm with the ultrasonic sensor it will reverse one wheel. So to start, make sure the ultrasonic sensor is pointing into open air (so that it won't sense anything within 20cm).

If you find that the motors are not both turning in a forward direction, you can simply swap the terminals where they connect into the motor driver board for any effected channel.

With this sorted, we need to now make sure that the ultrasonic sensor will change the direction of one of the wheels. With the 9V battery plugged in, place your hand in the view of the ultrasonic sensor - within 20cm of course, in order to trigger the condition which will make the robot turn.

If it does turn, and you're not interested in changing it, this is kind of where you're done.

But - if you want to mess with things a little more or if there are any issues, keep going!

Step 9: Troubleshooting and Exploring the Code

First, let's sort out what I suspect will be your only problem; the ultrasonic sensor doesn't seem to work.

We need to find out if we're getting anything from the sensor first. To start troubleshooting, first make sure all 4 wires are connected correctly to both the sensor itself and to the arduino.

Still no joy?

Try looking at the code you sent across. Even though the ultrasonic sensor reads a variable value, it triggers 2 different states for the motor: both going forward or one forward and one reverse. Here is the code for the forward state:

void forward()
{

//forward @ full speed

digitalWrite(12, LOW); //Setting direction of Channel A

digitalWrite(9, LOW); //Disengage the Brake for Channel A

analogWrite(3, 255); //Spins the motor on Channel A at full speed

digitalWrite(13, HIGH); //Setting direction of Channel B

digitalWrite(8, LOW); //Disengage the Brake for Channel B

analogWrite(11, 255); //Spins the motor on Channel B at full speed

}

From above, you can see that 3 variables are set for each motor. The direction can be set to LOW or HIGH for each channel in order to establish the forward direction. This is a coding alternative for sorting out the default forward state of the motors rather than swapping the wires for the effected channels physically.

Now we need to look at the code for the second state. I have named this turn_right but really this arbitrary, depending on how the motor is wired and the use of HIGH & LOW to set the motor directions.

How is this important for the ultrasonic sensor measurement? If the code for both motor states is exactly the same then the ultrasonic sensor could be doing everything right and calling the right commands at the right times. By looking at the code, this will be apparent (now that you know what you're looking for!). It will take nothing more than changing the direction setting for pin 12 or pin 13 from HIGH/LOW to the opposite.

If that doesn't solve your problem, then we need to take a look at what the ultrasonic sensor is seeing. The piece of code in question here is the following:

void oneSensorCycle()

{
for (uint8_t i = 0; i < SONAR_NUM; i++) {

Serial.print(i);

Serial.print("=");

Serial.print(cm[i]);

Serial.print("cm ");

}

Serial.println();

}

Earlier in the code there is another related piece:

void setup() {

Serial.begin(9600);

I mention this so that you can make sure to include it in any adaptations you might make to the code.

This serial section of the code is included so that you can get a readout of values from your sensor. This is true for any sensor and code you will find (it's quite easy to find code for pretty much any arduino compatible sensor).

To see the values you will need to plug in your arduino by USB again and open the Arduino IDE. Once open, make sure that your arduino is being seen by your computer (Tools > Serial Port > choose port). Also make sure again that the Arduino Uno board is selected in the Tools > Board menu.

We want to monitor what the sensor is seeing and there are 2 ways of launching the Serial Monitor which will allow us to do so. You can either choose "Serial Monitor" from the Tools menu or you can choose it through the button which looks like a magnifying glass at the top right of the Arduino IDE. By choosing either of these, you should see the robot pause for a second and then give a readout in the Serial Monitor window. If the read values doesn't fluctuate from 0cm then you need to recheck the connections at the arduino and at the sensor again. You may have mixed up GND/5V or Trigger/Echo pins.

If these suggestions don't solve your issue I would be very surprised and can only suggest that it's possible that somewhere along the way you may have somehow removed some critical code or perhaps not pasted in the complete code to the Arduino IDE? Try ensuring that you have copied and pasted the full code into the IDE before uploading it to the Arduino UNO.

If you have any other questions, please comment and I will try to answer the problems in an update.