Robotics is one of the most exciting fields in DIY electronics. Among beginner-friendly projects, the Obstacle Avoiding Robot is a great choice because it combines sensors, motors, and Arduino programming into one fun build. In this post, we’ll go step by step to make your own robot that can detect and avoid obstacles automatically.
Materials Required
To build this project, you’ll need:
-
Arduino Uno board
-
Ultrasonic sensor (HC-SR04)
-
Motor driver module (L298N)
-
Two DC geared motors with wheels
-
Robot chassis (or simple wooden/plywood board)
-
Caster wheel (for balance)
-
Jumper wires & battery pack (9V or 12V)
Circuit Connection
-
Connect the ultrasonic sensor to the Arduino:
-
VCC → 5V
-
GND → GND
-
Trig → Pin 9
-
Echo → Pin 10
-
-
Connect the L298N motor driver to the Arduino:
-
IN1 → Pin 2
-
IN2 → Pin 3
-
IN3 → Pin 4
-
IN4 → Pin 5
-
Motor A and B to the left and right motors
-
-
Power the motor driver with your battery pack, and connect Arduino to the same ground.
Arduino Code Example
#define trigPin 9
#define echoPin 10
#define motor1A 2
#define motor1B 3
#define motor2A 4
#define motor2B 5
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
Serial.begin(9600);
}
void loop() {
long duration, distance;
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) { // If obstacle detected
stopMotors();
delay(500);
turnRight();
delay(600);
} else {
moveForward();
}
}
void moveForward() {
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}
void stopMotors() {
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
void turnRight() {
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}
Working Principle
The ultrasonic sensor constantly measures the distance ahead. If it detects an obstacle closer than 20 cm, the Arduino commands the motors to stop and then turn the robot to avoid the object. If no obstacle is detected, the robot keeps moving forward.
Results
Once assembled and programmed, your robot will roam around while avoiding walls, boxes, and other obstacles. It’s an exciting beginner’s robotics project that gives you hands-on experience with sensors, motor control, and Arduino coding.
With this project, you’ll learn how to combine hardware and software to create an intelligent machine. Try building it, experiment with code, and share your version with your friends!

No comments:
Post a Comment