So you wanna build a robot which never hits anything; you’ve come to the right place ! It barely takes 15 minutes to do this ! So without wasting our time let’s learn how to make an obstacle avoiding robot !
Requirements :-
- Chassis OR any toy car.
- Arduino UNO/Mega.
- Ultrasonic sensor HC SR-04.
- 2 DC motors.
- 9V/12V 1A battery.
- Motor driver module L298.
- Jumpers.
- Single stranded wires.
- A pizza slice & a can of beer to celebrate 😉
Procedure :-
So yes, I’m going to divide the whole making into 4 parts –
Connections of Ultrasonic sensor → Connections of L298N → Code → Testing
If you haven’t used or experimented before with the ultrasonic sensor, I would suggest you to get familiar with it first ! I have a detail tutorial on ultrasonic sensor HC SR-04 which will guide you throughout !
Here is detailed Tutorial on Ultrasonic Sensor HC-SR04
Connections :-

Connections of Ultrasonic sensor –
- VCC – VCC terminal of Arduino.
- GND – GND terminal of Arduino.
- Trigpin – digital pin 9 on Arduino.
- Echo pin – digital pin 10 on Arduino.
Connections of L298N –
- +12V – Positive terminal of the battery.
- GND – a)GND of Arduino b)Negative terminal of battery.
- Input terminal 1 – Pin 4
- Input terminal 2 – Pin 5
- Input terminal 3 – Pin 6
- Input terminal 4 – Pin 7
- Output terminal 1 – Positive of first motor.
- Output terminal 2 – Negative of first motor.
- Output terminal 3 – Positive of second motor.
- Output terminal 4 – Negative of second motor.
Let me tell you, if you’re getting confused between the connections of motor driver and motors, you can first try only with one motor. For more clarity, I have made a separate tutorial for you on interfacing Arduino with Motor driver module L298N. I have provided the connections as well as the code for it.
Coding :-
int trigPin = 9; int echoPin = 10; int revright = 4; //REVerse motion of Right motor int fwdleft = 7; int revleft= 6; int fwdright= 5; //ForWarD motion of Right motor int c = 0; void setup() { //Serial.begin(9600); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(4, OUTPUT); pinMode(7, OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); // put your setup code here, to run once: } void loop() { long duration, distance; digitalWrite(trigPin,HIGH); delayMicroseconds(1000); digitalWrite(trigPin, LOW); duration=pulseIn(echoPin, HIGH); distance =(duration/2)/29.1; //Serial.print(distance); //Serial.println("CM"); delay(10); if((distance>20)) { digitalWrite(5,HIGH); // If you dont get proper movements of your robot, digitalWrite(4,LOW); // then alter the pin numbers digitalWrite(6,LOW); // digitalWrite(7,HIGH); // } else if(distance<20) { digitalWrite(5,HIGH); digitalWrite(4,LOW); digitalWrite(6,HIGH); //HIGH digitalWrite(7,LOW); } }
You may like –
Control LEDs with help of Android (in 5 mins)
&
Control LEDs with Voice commands (in 5 mins)
Testing :-
If the distance detected by the ultrasonic sensor is less than 25cm, then the bot will take a turn else, it will keep moving straight ! Simple… right ?
Well, here is a video tutorial which will make your job more easy !
Do I need to say this, NO… but anyway I will,
If you have any problem regarding connections or coding or during interfacing, all you have to do is comment down below OR contact me 🙂 I’ll very happy to help you
4 comments on “How to make obstacle avoiding robot using Arduino & Ultrasonic sensor ?”
Hello, I was wondering what rpm your dc motors are at?
Hello Ricky, they are 100 rpm DC motors
Why my driver l298n not working even though i have given a 9v battery voltage?
Hi Dika, please check the connections again or there is a minor chance that your module must have fried because of prior wrong connection!