Académique Documents
Professionnel Documents
Culture Documents
ArduinoRobotCarObstacleAvoidance
Back
Blog Resources
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
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.
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.
#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);
}
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
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
9/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
//SetupRIGHTwheel
Back
pinMode(enableRight,OUTPUT);
analogWrite(enableRight,255);
Blog Resources
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
Leave a Reply
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
Web on Devices
Electronics Hacking with JavaScript and other Web Technologies
Twitter
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
13/14
22/9/2016
ArduinoRobotCarObstacleAvoidance
Back
Blog Resources
Mate Marschalko
Front-end Web Developer, Creative Technologist and Maker. Builds Internet connected devices for the Internet of
Things.
http://www.webondevices.com/arduinorobotcarobstacleavoidance/
14/14