This project is for making a small robot car and this robot can move and avoid obstacle on road; also it uses Arduino Uno, ultrasonic sensor HC-SR04, motor driver L293D and two DC motors.
Furthermore, this Arduino Based Obstacle Avoidance Robot Circuit is simple and good for beginners and then robot check distance with ultrasonic sensor; if obstacle is near then robot stops and changes direction.
Arduino Code:
int trigPin = 9;
int echoPin = 10;
int motor1pin1 = 2;
int motor1pin2 = 3;
int motor2pin1 = 4;
int motor2pin2 = 5;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance > 20) {
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
} else {
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
delay(500);
}
}
Circuit Working:

Parts List:
| Components | Quantity |
|---|---|
| Arduino UNO | 1 |
| Ultrasonic Sensor HC-SR04 Module | 1 |
| Motor Driver L293D | 1 |
| BO Motor DC Gear Motor with wheels | 2 |
| ON/OFF switch | 1 |
| 9V Battery | 1 |
Arduino Uno is main brain of this project and ultrasonic sensor HC-SR04 connect to Arduino pins.
Here, motor driver L293D run two DC motors, but Arduino alone cannot give enough current to motors, so a 9V battery gives power supply.
Also, ON OFF switch connect in battery positive line for easy control.
When power is ON, ultrasonic sensor keep measure distance and if distance is more than 20 cm then robot goes forward and if distance less than 20 cm then robot stops and turn.
Then after a turn the robot goes forward again and this simple logic help robot avoid obstacles.
Formulas:
The below mentioned formula is for Arduino Based Obstacle Avoidance Robot Circuit:
Ultrasonic sensor formula:
Distance = (Time x Speed of Sound) / 2
where,
- Ultrasonic sensor is for sound wave.
- Sound here hit obstacle and come back.
- Time represents total travel time (go and return).
- Speed of sound is about 343 meter per second (0.034 cm per microsecond).
- Divide by 2 is because sound travel two way, go and back.
So this formula gives one-way distance from sensor to obstacle.
How to Build:
To build a Arduino Based Obstacle Avoidance Robot Circuit follow the below mentioned steps for connections:
- First, gather all parts same as circuit diagram.
- Next, HC-SR04 VCC connect to Arduino 5V, HC-SR04 GND connect to Arduino GND, HC-SR04 Trigger connect to Arduino pin 9 and then HC-SR04 Echo connect to Arduino pin 10.
- Then put motor driver L293D on breadboard.
- Now input1 and input2 connect to Arduino pins 2 and 3 and input3 and input4 connect to Arduino pins 4 and 5.
- After that output1 and Output2 connect to left motor wires and then output3 and output4 connect to right motor wires.
- Also, L293D VSS pin 16 connect to Arduino 5V, L293D VS pin 8 connect to 9V battery positive and then L293D GND pins 4,5,12 and 13 connect to Arduino GND and battery negative.
- Further, battery positive wire go to VS pin 8 of L293D and then battery negative wire go to GND pins 4,5,12 and 13 of L293D.
- Finally, On/Off switch one end connect in battery positive line and other side of switch connect to VS pin 8 of L293D.
Conclusion:
To conclude, this Arduino Based Obstacle Avoidance Robot Circuit make small robot car with Arduino.
Robot use ultrasonic sensor to see obstacle, and when obstacle come near the robot stops and turn and when road is clear then robot go forward.
Moreover, it is easy project for beginner to learn sensor, motor driver and coding and even more sensor or better code can make robot smarter.