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
Attachments
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.
- The attached chassis (robot template) pdf file can be modified as you see fit for your own use.
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.
27 Comments
7 years ago
what are the dimensions for the chassis on the PDF? i need to recreate it in solidworks so i can laser cut it, but the dimension when transferred from PDF to illustrator to solid works is in hundreds of inches. also if there could be more pictures of the wiring and set up of the arduino that would be fantastic
8 years ago
Could you explain a bit more? I don't understand what you want to accomplish.
8 years ago
Hey tony I'm using an L298N motorshield. will 9v be enough?
and can you help eith the program if I'm using L298N?
8 years ago
hey tony,
can you help with the connections in a little more detail
Reply 8 years ago
Can you let me know where you're having trouble?
I always suggest to anyone looking at where connections should be to look into the code. This helps for any project - for example, at the beginning of pretty much any code, people will define pins.
In this case the ultrasonic sensor is defined in this line:
"NewPing(7, 6, MAX_DISTANCE), // Trigger pin, echo pin, and max. Copy & paste this line (changing trig & echo pins) for each sensor you physically use."
So the trigger pin is pin # 7 and the echo pin is # 6.
Let me know if there's any other bits you're having trouble with.....
Reply 8 years ago
Thank you! Yes I'll get working on it:)
9 years ago on Introduction
thanks for such an awesome idea
but i wanted to as if the code changes if i use this shield
http://www.ebay.com/itm/Motor-Drive-Expansion-Shield-Board-Module-L293D-For-Arduino-Duemilanove-Mega-UNO-/221605183415?pt=LH_DefaultDomain_0&hash=item3398b2bfb7
Reply 9 years ago on Introduction
Ishan,
For the majority of motor shield/breakout for a DC motor you will note in the code that there are 3 pins for each motor. The 3 pins related to each one control 3 variables.
These are:
Brake (or Enable), Direction & Speed.
In my own code these are pins 9, 12 & 3 respectively.
The shield you link to above is one of these:
http://playground.arduino.cc/Main/AdafruitMotorShi...
Looking at the sample code on that page, it looks like a different approach was taken. Sainsmart also have a shield that is remarkably similar (I'm assuming that it's the same given the open source nature of Arduino and related builds).
I'm having trouble finding detail on the Sainsmart page in finding pin mapping.
Hopefully, you can make sense of the arduino link above about it. I can also suggest 2 other options:
Remove one of the the L293D chips and breadboard a motor driver or
Buy one of these: http://www.amazon.co.uk/L293D-H-Bridge-Motor-Drive...
I really like the second one as it's a reliable and easy to build unit - I've used one with a Raspberry Pi too. It also means that you only use the pins you need to, whereas the Sainsmart/Adafruit board puts an end to your Uno for anything but motor control really.
Tony
9 years ago
I've followed all of the instructions properly (as far as I can tell) but nothing happens when I connect the batteries other than the LEDs flashing on the UNO. I tried reconnecting everything and double checking all the wires were connected to the correct ports and still nothing. Finally, I plugged the USB cable into the UNO to see if the serial monitor would display any information and it suddenly started to worked perfectly. I disconnected the 9v battery and it continued to work (albeit a little slower) from the USB power. Any clues as to what I'm doing wrong?
Reply 9 years ago on Introduction
Hi,
On USB, your project will get 5V and work slower because of that. With the 9V battery on it, the motors will get 9V. When disconnected from the USB, the 9V attached to the driver shield will also power the UNO below through a voltage regulator on the shield. The voltage regulator is allowing 5V to the UNO to execute the code.
I've noticed weird things happening when the 9V battery drains a bit. Try using a fresh 9V battery to see what happens - if this doesn't solve it, maybe try sending the code to the UNO again and then using the fresh battery.
Let me know how you get on!
Tony
Reply 9 years ago
Hi Tony,
Thanks for the quick reply. The batteries are brand new. I have three of them and none of them work the robot. I've also sent the code to three different UNOs more than once and the problem still occurs. I was hoping maybe I'd done something wrong and needed to wire the motor shield to the UNO in some way (other than of course slotting it into the pins). I am using knock-off chinese UNOs though so maybe that is the problem. Maybe I should try adding another battery and connecting it directly to the UNO? Is 9v too high to connect directly to the UNO?
Richard
Reply 9 years ago
Hi Tony,
You were totally right about the battery. It was a new battery, but a cheap Toshiba one. I switched it out with an Energizer one and it works perfectly. Great instructions and a great design. Thanks!
Richard
Reply 9 years ago on Introduction
Hi,
On USB, your project will get 5V and work slower because of that. With the 9V battery on it, the motors will get 9V. When disconnected from the USB, the 9V attached to the driver shield will also power the UNO below through a voltage regulator on the shield. The voltage regulator is allowing 5V to the UNO to execute the code.
I've noticed weird things happening when the 9V battery drains a bit. Try using a fresh 9V battery to see what happens - if this doesn't solve it, maybe try sending the code to the UNO again and then using the fresh battery.
Let me know how you get on!
Tony
Reply 9 years ago on Introduction
Hi,
On USB, your project will get 5V and work slower because of that. With the 9V battery on it, the motors will get 9V. When disconnected from the USB, the 9V attached to the driver shield will also power the UNO below through a voltage regulator on the shield. The voltage regulator is allowing 5V to the UNO to execute the code.
I've noticed weird things happening when the 9V battery drains a bit. Try using a fresh 9V battery to see what happens - if this doesn't solve it, maybe try sending the code to the UNO again and then using the fresh battery.
Let me know how you get on!
Tony
Reply 9 years ago on Introduction
Hi,
On USB, your project will get 5V and work slower because of that. With the 9V battery on it, the motors will get 9V. When disconnected from the USB, the 9V attached to the driver shield will also power the UNO below through a voltage regulator on the shield. The voltage regulator is allowing 5V to the UNO to execute the code.
I've noticed weird things happening when the 9V battery drains a bit. Try using a fresh 9V battery to see what happens - if this doesn't solve it, maybe try sending the code to the UNO again and then using the fresh battery.
Let me know how you get on!
Tony
9 years ago on Introduction
Whoa! This is really cool, and I like your combination of 3d print stuff and normal electronics.
Reply 9 years ago on Introduction
Cheers SnakoM,
Yeah, I've tried to incorporate the process we have at FabLab (and should be available at any FabLab). The idea being that the code can be applied to any robot chassis commercially available as well as the opportunity for anyone to make their own chassis design.
Tony
9 years ago
Smart idea! Thanks for shearig :)
9 years ago
I always wanted to make one of these robots . But the programming is very frustrating to me ! :(
Thanks for sharing !
Reply 9 years ago on Introduction
No problem! I only really started about 6 months ago and now feel I can properly see what's happening!
Let me know if you make one and how you find the instructable - I'm keen to make sure that it's accessible to all abilities.