Introduction: Arduino HC-SR04 Ultrasonic Rover

Picture of Arduino HC-SR04 Ultrasonic Rover

Simple Arduno Rover with HC-SR04 Ultrasonic sensor.

Step 1:

Picture of

YOU will need to build this:

HC-SR04 Ultrasonic sensor

Battery holder for 6 AA batteries

6 AA batteries

Arduino UNO (I use UNO R2)
Motor Shield for Arduino (L293)

9g Servo

2 gear motors with wheels

CD box

4 male-to-male cables

4 female-to-female cables

few cable ties

I buy all components for this rover on ebay.

First you will need 2pcs Plastic Tire Wheel + DC 6V Gear Motor 48:1.

I solder 2 male-to-male cables and 104 capacitors.

I used slim CD box as a chassis, I made 2 holes on each side of CD box and then fixed gear motors with cable tie.

Step 2:

Picture of

You can see all the components you need on this photo.

  1. HC-SR04 Ultrasonic sensor
  2. Battery holder for 6 AA batteries
  3. 6 AA batteries
  4. Arduino UNO (I use UNO R2)
  5. Motor Shield for Arduino (L293)

  6. 9g Servo
  7. 2 gear motors with wheels on CD box chassis.
  8. 4 male-to-male cables
  9. 4 female-to-female cables
  10. few cable ties

Step 3:

Picture of

Сonnection scheme is very easy.

  1. Connect 9g servo to SERVO_2 pins on motorshield
  2. Left motor to M4
  3. Right motor to M1
  4. Echo - pin A4
  5. Trig - pin A5
  6. Vcc - pin +5 ; Gnd - Gnd

I fix servo on battery box with cable tie

Step 4:

Picture of

Final result and video.


Faster version without servo        http://youtu.be/hBHpOV5TOY8

Step 5:

Put AFMotor.h and AFMotor.cpp to folder AFMotor in libraries folder Aduino.

Comments

amitbs99 (author)2017-04-23

hello I have made it but it couldn't work. when I operate it the servo once starts and then stops

Earl DsanielV (author)amitbs992017-09-01

it only means that you did not follow all the direction carefully...

IsraelD3 (author)2017-05-16

I make the connections correctly but still it goes crazy, the servant stays looking to one side and goes forward and back without stopping other times it seems to work well but then it goes back malfunctioning.

ZarishMark (author)2017-01-25

Hi, i've seen the codes guy's i need a code of ultrasonic sensor to stop the object on which it's implemented after sensing the obstacle can anyone help?

MoonDocker (author)2014-02-19

I checked the code it will not compile :(

Eagle199393 (author)MoonDocker2014-02-19

Ок, I uploaded correct version of the code. It works. Try it.

RVIRENDER (author)Eagle1993932016-12-12

where is the code

MoonDocker (author)Eagle1993932014-02-19

Okay looks good it compiles okay. I will let you know how it turns out. I am waiting for another HC-SR04. You might want to mention they will need to install the NewPing library. You can find it here --> http://forum.arduino.cc/index.php?topic=106043.0

Thanks for sharing !

amitbs99 (author)2016-12-11

Hi,

Can i have circuit diagram for this project?

Thanks

joshuaron (author)2016-04-20

Hi,

The Ultrasonic sensor doesn't work for me when I connect it to pins A4,A5 in the MotorShield board. Can anyone explain me how does it make sense to use them, if they are "Analog In" pins, wheras the UltraSonic sensor requires "Digital In" & "Digital Out" pins ?!

sonasec (author)joshuaron2016-08-14

pinMode(pinTrig, OUTPUT);

pinMode(pinEcho, INPUT);

thise for configuration

-----

digitalWrite(pinTrig, LOW);

digitalWrite(pinTrig, HIGH);

digitalWrite(pinTrig, LOW);

duration=pulseIn(pinEcho,HIGH);

distance=(duration/2)*Vson;

Serial.print(distance);

Serial.println("cm");

and this on for mesur distance (#define Vson 0.034 // cm/us)

MarkJ108 (author)2016-08-13

i tried this but my bot keeps circling. i didnt change the code and followed each and every instruction properly. please help

Archit B (author)2016-07-28

hey pleasee help earliest mine isnt working it just keeps on rotating i have to submit it by tommorrow help help help
i have
arduino ide 1.6.9
arduino uno rev3
afmotorsheild is same using 12v and 1ah
hcsro4
heeellllp

BILL MOUGIOS (author)2016-06-22

well done

ahmeda489 (author)2016-04-12

please van you send me a code without the sensor ?

AndreaD20 (author)2016-04-12

Hi. Great job! I will try it. Would it work with Hg7881 HG7881CP? I already boutht it

EdizonBlu (author)2015-03-31

a

Hi guys, I would like to ask on how to stabilize my ultrasonic sensor mounted in a servo motor. It is because everytime an "Out of Range" displays in the serial monitor it gives current to the servo motor causing it to sweep. The possibility of sweeping the servo left or right only is when there is an object being detected. BTW, the ultrasonic sensor is mounted above the servo mtor.

Here is my code://

#define trigPin 13

#define echoPin 12

#define led 11

#define led2 10

#include <Servo.h>

int pos = 0; // variable to store the servo position

int defPOS = 0;

Servo myservo;

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(led, OUTPUT);

pinMode(led2, OUTPUT);

myservo.attach(9);

myservo.write(0);

}

void loop() {

long duration, distance;

digitalWrite(trigPin, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin, HIGH);

// delayMicroseconds(1000); - Removed this line

delayMicroseconds(10); // Added this line

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

//0 Left

//1 Center

//2 Right

int buttonState1 = digitalRead(led); //red

int buttonState2 = digitalRead(led2); //green

if( (buttonState1 == 0 && buttonState2 == 1) && (defPOS != 1))

{

delay(2000);

if(defPOS == 2)

{

for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

}

else if(defPOS == 0)

{

for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

}

defPOS = 1;

}

if (distance < 30) { // This is where the LED On/Off happens

digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off

digitalWrite(led2,LOW);

if(defPOS == 0)

{

for(pos = 90; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees

{

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

defPOS = 2;

}

else if(defPOS == 2)

{

for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

defPOS = 1;

}

else if(defPOS == 1)

{

for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

defPOS = 0;

}

//myservo.write(180);

// delay(1000);

//myservo.write(0);

}

else {

digitalWrite(led,LOW);

digitalWrite(led2,HIGH);

}

if (distance >= 400 || distance <= 0){

Serial.println("Out of range");

}

else {

Serial.print(distance);

Serial.println(" cm");

}

delay(500);

}

pchaure (author)EdizonBlu2016-03-30

i want the full code the code u posted is only of the ultrasonic sensor and servo... what about the motors?can u plz help me thanks in advance

IsraelD3 (author)2015-10-14

Hi, I have a question I would like to explain to me how and where I can change the speed and distance detection do not quite understand because the instructions are in the program, thank you.

chiobesek (author)2015-10-09

How does the robot stay in balance with only 2 wheels? Or does it have something else like a ball caster or so?

GrantR2 (author)2015-09-12

Can someone please help me with coding on Arduino

Im new to coding and robotics and I built a biped robot with 4 servos and a HC-SRO4 servo for object avoidance. Im using Arduino uno and I only know how to make the servos move with write.microseconds. I want to make it avoid objects and be autonomous.

ahnaf.rafid.3 (author)2015-01-11

can u help me. I cannot find A4 on the motor driver shield can u show me a daigram

fansblink (author)ahnaf.rafid.32015-01-13

you need to soldering a header to pin A0-A5 on Driver motor shield. Sorry for my bad english

sendaljepits (author)fansblink2015-09-02

mas andy,,punya saya ko cuma maju mundur yah??ada yg salah kah,,saya pakai servo sg90

marciokoko (author)2015-08-31

What is the difference between turnRight()/Left and veerRight()/Left?

marciokoko (author)2015-08-24

Hi! Do you have an AFMotor.cpp that works? The one I downloaded from here gives me a bunch of compile errors about stray '/' and '{rtf...}'

byç2 (author)2015-08-20

i download afmotor.h library but giving error " afmotor.h no such file or directory" why, please help ?

marciokoko (author)2015-08-16

I was wondering if you have a tutorial on how you soldered A4 and A5 from the Arduino to the motor shield.

nahiyan.ahmed.142 (author)2015-04-05

I made it..But I don't have a servo... Sadly...I don't have the idea of programing as well..Could you pls leave the programming code in the comments so that I can copy it and upload in my arduino...One more request...leave the programming code with the same pins....

nik raikar (author)2015-03-06

in the code it says no file or directory such as new ping.h .so should i download the ping library

richardschuiv (author)nik raikar2015-03-22

yes

fansblink made it! (author)2015-01-13

Cool, this realy fun... thanks for tutorial...

BeO2 (author)fansblink2015-03-14

How Can I Contact With You?

fansblink (author)BeO22015-03-22

muhandi.prasetyo@gmail.com

gadge_s (author)2015-02-24

i want to replace arduino with pic18f4520..help me with code plzz!!

gadge_s (author)2015-02-24

i want to replace arduino with pic18f4520..help me with code plzz!!

gadge_s (author)2015-02-24

i want to replace arduino with pic18f4520..help me with code plzz!!

gadge_s (author)2015-02-24

lance.pederson (author)2015-02-07

Any way to incorporate 2 more wheels into the code? or would that just complicate things? I have two more gear boxes and wheels and would like to add them, but im not sure how the turning would work...

miginstruments (author)2015-02-07

Hi I want to build a similar kind of Robot. But I dont ve the Arduino Shield. But I ve got the L293D IC. Can I use the above mentioned code to make my Robot work. if yes will you please help me in the Arduino-293d pin configuration. Thanks in Advance.

vishj (author)2015-02-07

I built the same project .....but this is not run proprly.....what i do ....all the connnection is same as yours

prash28 (author)2015-01-30

Or give me the link 2 buy

prash28 (author)2015-01-30

What type of servo you used is it continous rotation

ahmed.hachicha.125 (author)2015-01-23

can i use l293b???

pesogebra (author)2014-11-23

hi there, i have completed a line follower robot, but i want to combine with he coding of HC-SR04 for stopping the robot. i use the same adafruit motor driver L293D..kindly seeking your help..tq

vladimirkl1 (author)2014-11-17

function void checkPath() { .......

........... line 67 ROBOT.ino

if (curDist > curDist) {maxAngle = pos;}

//what is the condition?


MechanicalMashup (author)2014-09-28

Do you have the code without the servo?

enrique.padillamolina (author)2014-08-08

excelente modelo y trabajo

Zach Sousa (author)2014-06-01

Thank you for the code I tried to make something similar to this but I had trouble with the code and now it works!

mb64 (author)2014-04-06

hello I cannot download your code I get an error