22/9/2016
ArduinoRobotCarObstacleAvoidance
Back
Blog Resources
Arduino Robot Car Obstacle Avoidance
Published last year by Mate Marschalko
Arduino Robot Car with Obstacle Avoidance. Ive always been excited about
autonomous cars and radio controlled toy cars so it was time for me to build my
own with an Arduino Nano. The nished car has two modes. First is manual mode
which allows you to drive it wirelessly from an Arduino UNO and a joystick shield.
Second is the autonomous mode which drives the car continuously forward and
avoids obstacles by stopping and steering away immediately. Obstacles are detected
by the ultrasonic sensor attached to the front of the car.
Arduino Robot Car with Obstacle Avoidance
Lets now see how I built this car.
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
1/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
I didnt want to spend too much time building the chassis so I decided to buy a robot
Blogmotors
Resources
carBack
kit from ebay. After assembling the kit I soldered the wires onto the
and
screwed the 4xAA battery holder then I was ready to add the Arduino electronics.
Download FREE Ebook: Introduction to JavaScript Electronics
Motor drive circuit
Driving the motors from the Arduino is not possible as they draw too much current
so you need a separate motor drive unit to help the Arduino. I purchased a pretty, red
L298N module from ebay. Heres how it looks like:
L298n motor drive unit
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
2/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
This module can drive two motors up to 2000mA each and maximum 50 volts.
Backperfectly ne as our motors will only be driven from the 4xAABlog
Resources
Thats
batteries
which
is 6 volts in total. The two motors are connected to port 1 and port 2 (see numbers on
Download
Introduction
to JavaScript
the image).
Wires FREE
can beEbook:
interchanged
as reversed
polarityElectronics
only makes the motors go
in reverse.
Arduino Robot Car with Obstacle Avoidance
Behind pin 3 and 4 there is a jumper. If this jumper is in place then you have to make
sure your supply voltage is lower than 12V. It will also allow you to use pin 5 as
regulated 5V output for your Arduino. On my car I connected the positive wire from
the battery pack to pin 3 and the ground wire to number 4. Next I connected the 5V
output pin from the motor drive module, which is pin number 5, to my Arduino
Nanos 5V pin and I also connected pin number 4 to my Arduinos GND. Power is
now connected.
The 6 pins marked with number 6 are there to take motor control commands from
the Arduino, 3 for each motor. Heres how it works:
ENA and ENB are the enable pins for the two motors. If the jumper is in place the
motors will be driven at full speed. If we take o the jumpers and connect a PWM
signal from the Arduino to them then we can change the speed of the motors.
IN1 and IN2 can change the direction of motor number 1. If we send HIGH to IN1
and LOW to IN2 then the motor will go forward, if IN1 is LOW and IN2 is HIGH then
the motor will go backwards. The same applies to the second motor and the IN3 and
IN4 pins.
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
3/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
If I want to run the rst motor at half speed forward then ENA is a PWM value of
Back
Blog Resources
128,
IN1 is HIGH and IN2 is LOW
If I want to run the second motor at full speed backwards then ENB is a PWM value
Download FREE Ebook: Introduction to JavaScript Electronics
of 255, IN3 is LOW and IN4 is HIGH
I added a 1000uF to pin 4 and 5 as the motors draw a lot of current when speeding up
and the Arduino restarted quite often when there was a voltage drop. This xed the
problem and now the the motor drive circuit done.
The hardware build was so easy that my 4-year-old daughter managed to
understand the circuit and what each of the components do.
Autonomous mode and the ultrasonic sensor
I used a cheap ultrasonic distance sensor with a 4 meter range. Thats perfectly
enough as I set the obstacle detection limit to only 20 centimetres. The sensor has 4
pins: VCC is connected to the Arduinos 5V pin, GND to GND, Trig to D6 and Echo to
D5.
The example code to read the sensor is only a few lines:
#definetrigPin6
#defineechoPin5
voidsetup(){
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
4/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
Serial.begin(9600);
Back
pinMode(trigPin,OUTPUT);
Blog Resources
pinMode(echoPin,INPUT);
}
Download FREE Ebook: Introduction to JavaScript Electronics
voidloop(){
floatduration,distance;
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=(duration/2)/29.1;
if(distance>=400||distance<=0){
Serial.println("Outofrange");
}
else{
Serial.print(distance);
Serial.println("cm");
}
}
After I had all the motors and the distance sensor connected up I put the motors into
a continues forward motion for the autonomous mode. In the meantime the car was
constantly checking the distance sensor and instructed the motors to steer away
until there was no longer anything in front. Here's what I did in the loop() function:
//GetDistancesensor
floatduration,distance;
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);//Addedthisline
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=(duration/2)/29.1;
if(distance>20){
digitalWrite(rightBackward,LOW);
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
5/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
digitalWrite(rightForward,HIGH);
Back
Blog
digitalWrite(leftBackward,LOW);
digitalWrite(leftForward,HIGH);
analogWrite(enableRight,200);
Download FREE Ebook: Introduction to JavaScript Electronics
analogWrite(enableLeft,200);
Resources
}else{
digitalWrite(rightBackward,LOW);
digitalWrite(rightForward,HIGH);
digitalWrite(leftBackward,HIGH);
digitalWrite(leftForward,LOW);
analogWrite(enableRight,200);
analogWrite(enableLeft,200);
delay(100);
}
Manual mode and the joystick shield
This was a little bit more complicated as we now have wireless communication. The
controller was an Arduino UNO with a joystick shield. You can also see the nRF24l01
antenna on the left top corner.
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
6/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
This controller was powered from a 9V battery. The button and the joystick on the
Back
shield
are simple sensors so you can read them with the digitalRead()Blog
and Resources
analogRead() functions. The below piece of code is the nal code I have for the
joystick.Download
It rst checks
stateIntroduction
of the joystick
sends Electronics
through the wireless
FREEthe
Ebook:
to then
JavaScript
messages through the nRF24l01 module.
#include<SPI.h>
#include<RF24.h>
#include<RF24_config.h>
//Radiopinsetup
#definecePin9
#definecsnPin10
#definejoyXA0
#definejoyYA1
//InitRF24radio
RF24radio(cePin,csnPin);
constuint64_tpipe0=0xF0F0F0AA;
uint8_tp0=0;
//Variables
intchr[1];
intterminateChar[1];
StringdataToSend="";
voidsetup(){
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe0);
}
voidloop(){
dataToSend+=map(analogRead(joyX),0,1023,0,9);
dataToSend+=map(analogRead(joyY),0,1023,0,9);
intstringSize=dataToSend.length();
for(inti=0;i<stringSize;i++){
intcharToSend[1];
charToSend[0]=dataToSend.charAt(i);
radio.write(charToSend,1);
}
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
7/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
terminateChar[0]=2;
Back
Blog
radio.write(terminateChar,1);
dataToSend="";
}
Download FREE Ebook: Introduction to JavaScript Electronics
Resources
To read this from the car you open a reading pipe then check the radio.available()
function for incoming messages. Below is the nal piece of code that is running on
the car:
#include<SPI.h>
#include<RF24.h>
#include<RF24_config.h>
//Radiopinsetup
#definecePin9
#definecsnPin10
//USsensorpinsetup
#definetrigPin14
#defineechoPin2
#defineaccuracy4
//Motorpinsetup
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
8/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
#defineenableLeft6
Back
#defineleftForward4
#defineleftBackward3
Blog Resources
Download FREE Ebook: Introduction to JavaScript Electronics
#defineenableRight5
#definerightForward8
#definerightBackward7
//InitRF24radio
RF24radio(cePin,csnPin);
constuint64_tpipe0=0xF0F0F0AA;
uint8_tp0=0;
//Variables
StringreceivedMessage="";
intchr[1];
intjoyX=4;
intjoyY=4;
intspeedCoeff=0;
intleft=0;
intright=0;
voidsetup(){
//SetupRF24radio
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(p0,pipe0);
radio.startListening();
//SetupUSsensor
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
//SetupLEFTwhel
pinMode(enableLeft,OUTPUT);
analogWrite(enableLeft,255);
pinMode(leftForward,OUTPUT);
digitalWrite(leftForward,LOW);
pinMode(leftBackward,OUTPUT);
digitalWrite(leftBackward,LOW);
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
9/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
//SetupRIGHTwheel
Back
pinMode(enableRight,OUTPUT);
analogWrite(enableRight,255);
Blog Resources
Download FREE Ebook: Introduction to JavaScript Electronics
pinMode(rightForward,OUTPUT);
digitalWrite(rightForward,LOW);
pinMode(rightBackward,OUTPUT);
digitalWrite(rightBackward,LOW);
}
voidloop(){
//GetDistancesensor
floatduration,distance;
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);//Addedthisline
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=(duration/2)/29.1;
//Watchcontrolmessages
while(radio.available()){
radio.read(chr,1);
charreceivedChar=chr[0];
if(chr[0]!=2){
receivedMessage.concat(receivedChar);
}else{
joyX=receivedMessage.substring(0,1).toInt();
joyY=receivedMessage.substring(1,2).toInt();
if(distance<15){
joyX=4;
joyY=4;
}
if(joyY<4){
//Backward
digitalWrite(rightBackward,HIGH);
digitalWrite(rightForward,LOW);
digitalWrite(leftBackward,HIGH);
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
10/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
digitalWrite(leftForward,LOW);
Back
Blog
speedCoeff=map(joyY,0,4,255,0);
speedCoeff=speedCoeff<0?0:speedCoeff;
speedCoeff=speedCoeff>255?255:speedCoeff;
Download FREE Ebook: Introduction to JavaScript Electronics
}elseif(joyY>4){
//Forward
digitalWrite(rightBackward,LOW);
Resources
digitalWrite(rightForward,HIGH);
digitalWrite(leftBackward,LOW);
digitalWrite(leftForward,HIGH);
speedCoeff=map(joyY,4,9,0,255);
speedCoeff=speedCoeff<0?0:speedCoeff;
speedCoeff=speedCoeff>255?255:speedCoeff;
}
right=map(joyX,4,9,255,0);
right=right<0?0:right;
right=right>255?255:right;
right=(right+speedCoeff)/2;
analogWrite(enableRight,right);
left=map(joyX,0,4,0,255);
left=left<0?0:left;
left=left>255?255:left;
left=(left+speedCoeff)/2;
analogWrite(enableLeft,left);
if(joyY==4){
//Stopped
if(joyX<4){
//Left
digitalWrite(rightBackward,LOW);
digitalWrite(rightForward,HIGH);
digitalWrite(leftBackward,HIGH);
digitalWrite(leftForward,LOW);
right=map(joyX,0,4,255,0);
right=right<0?0:right;
right=right>255?255:right;
analogWrite(enableRight,right);
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
11/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
left=map(joyX,0,4,255,0);
Back
left=left<0?0:left;
Blog Resources
left=left>255?255:left;
analogWrite(enableLeft,left);
Download FREE Ebook: Introduction to JavaScript Electronics
}elseif(joyX>4){
//Right
digitalWrite(rightBackward,HIGH);
digitalWrite(rightForward,LOW);
digitalWrite(leftBackward,LOW);
digitalWrite(leftForward,HIGH);
right=map(joyX,4,9,0,255);
right=right<0?0:right;
right=right>255?255:right;
analogWrite(enableRight,right);
left=map(joyX,4,9,0,255);
left=left<0?0:left;
left=left>255?255:left;
analogWrite(enableLeft,left);
}elseif(joyX==4){
//Stopped
digitalWrite(rightBackward,LOW);
digitalWrite(rightForward,LOW);
digitalWrite(leftBackward,LOW);
digitalWrite(leftForward,LOW);
analogWrite(enableRight,0);
analogWrite(enableLeft,0);
}
}
receivedMessage="";
}
}
}
I also got the bluetooth communication working between my phone and the Arduino
but only with an Arduino UNO. Its probably because the Arduino Nano I was using in
this project requires a 3.3V signal level on RX and TX and the UNO can take 5V. If you
have done this before please email me and let me know!
Back to all
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
12/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
Back to all
Back
Blog Resources
Download FREE Ebook: Introduction to JavaScript Electronics
Leave a Reply
You must be logged in to post a comment.
Free Ebook
Step up your web developer career and learn hardware prototyping.
This ebook will get you started with JavaScript Arduino electronics development in a
couple of hours.
Email address
Send me the PDF
Web on Devices
Electronics Hacking with JavaScript and other Web Technologies
Twitter
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
Facebook
13/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
Back
Blog Resources
Download FREE Ebook: Introduction to JavaScript Electronics
Mate Marschalko
Front-end Web Developer, Creative Technologist and Maker. Builds Internet connected devices for the Internet of
Things.
All rights reserved | Contact at hello@webondevices.com
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
14/14