DIY Obstacle Avoiding Robot
DIY Obstacle Avoiding Robot Using Arduino
This project creates an intelligent obstacle avoiding robot that autonomously navigates through environments while avoiding obstacles. The robot uses an ultrasonic sensor to detect objects and a servo motor to scan the surroundings, making smart decisions about which direction to move.

.png)
.png)
How the Robot Works
- Ultrasonic Sensor continuously measures distance to obstacles ahead
- When obstacle is detected within 25cm: Robot stops and assesses the environment
- SG90 Servo Motor rotates the ultrasonic sensor to scan left and right directions
- Robot compares left and right side distances to choose the clearest path
- L298N Motor Driver controls four BO motors for precise movement
- Robot makes intelligent decisions: reverse, turn left, or turn right based on environment
- 7.4V battery provides power for autonomous operation
- MDF board chassis provides sturdy foundation for all components
Materials Required
- BO Motors (4x) - Buy Here
- SG90 Servo Motor - Buy Here
- Ultrasonic Sensor - Buy Here
- Ultrasonic Sensor Holder - Buy Here
- L298N Motor Driver - Buy Here
- Arduino Uno - Buy Here
- MDF Board - Buy Here
- Mini Breadboard - Buy Here
- Jumper Wires - Buy Here
- 7.4V Battery - Buy Here
- Rubber Wheels (4x) - Included with BO motors
🎥 Circuit Diagram is given in the video!
Arduino Code
Upload this code to your Arduino Uno after completing all hardware connections:
#include#include #define SERVO_PIN 2 #define ULTRASONIC_SENSOR_TRIG 3 #define ULTRASONIC_SENSOR_ECHO 4 #define MAX_REGULAR_MOTOR_SPEED 110 #define MAX_MOTOR_ADJUST_SPEED 150 #define DISTANCE_TO_CHECK 25 //Right motor int enableRightMotor = 5; int rightMotorPin1 = 6; int rightMotorPin2 = 7; //Left motor int enableLeftMotor = 11; int leftMotorPin1 = 8; int leftMotorPin2 = 9; NewPing mySensor(ULTRASONIC_SENSOR_TRIG, ULTRASONIC_SENSOR_ECHO, 100); Servo myServo; void setup() { // put your setup code here, to run once: pinMode(enableRightMotor, OUTPUT); pinMode(rightMotorPin1, OUTPUT); pinMode(rightMotorPin2, OUTPUT); pinMode(enableLeftMotor, OUTPUT); pinMode(leftMotorPin1, OUTPUT); pinMode(leftMotorPin2, OUTPUT); myServo.attach(SERVO_PIN); myServo.write(90); rotateMotor(0, 0); } void loop() { int distance = mySensor.ping_cm(); // If distance is within 25 cm then adjust motor direction as below if (distance > 0 && distance < DISTANCE_TO_CHECK) { // Stop motors rotateMotor(0, 0); delay(500); // Reverse motors rotateMotor(-MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED); delay(200); // Stop motors rotateMotor(0, 0); delay(500); // Rotate servo to left myServo.write(180); delay(500); // Read left side distance using ultrasonic sensor int distanceLeft = mySensor.ping_cm(); // Rotate servo to right myServo.write(0); delay(500); // Read right side distance using ultrasonic sensor int distanceRight = mySensor.ping_cm(); // Bring servo to center myServo.write(90); delay(500); if (distanceLeft == 0) { rotateMotor(MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED); delay(500); } else if (distanceRight == 0) { rotateMotor(-MAX_MOTOR_ADJUST_SPEED, MAX_MOTOR_ADJUST_SPEED); delay(500); } else if (distanceLeft >= distanceRight) { rotateMotor(MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED); delay(500); } else { rotateMotor(-MAX_MOTOR_ADJUST_SPEED, MAX_MOTOR_ADJUST_SPEED); delay(500); } rotateMotor(0, 0); delay(200); } else { rotateMotor(MAX_REGULAR_MOTOR_SPEED, MAX_REGULAR_MOTOR_SPEED); } } void rotateMotor(int rightMotorSpeed, int leftMotorSpeed) { if (rightMotorSpeed < 0) { digitalWrite(rightMotorPin1, LOW); digitalWrite(rightMotorPin2, HIGH); } else if (rightMotorSpeed >= 0) { digitalWrite(rightMotorPin1, HIGH); digitalWrite(rightMotorPin2, LOW); } if (leftMotorSpeed < 0) { digitalWrite(leftMotorPin1, LOW); digitalWrite(leftMotorPin2, HIGH); } else if (leftMotorSpeed >= 0) { digitalWrite(leftMotorPin1, HIGH); digitalWrite(leftMotorPin2, LOW); } analogWrite(enableRightMotor, abs(rightMotorSpeed)); analogWrite(enableLeftMotor, abs(leftMotorSpeed)); }
Code Explanation:
Setup Function:
Initializes motor control pins, servo motor, and ultrasonic sensor. Sets the servo to center position (90°) and ensures motors are stopped at startup.
Main Loop:
Continuously reads distance from ultrasonic sensor. If obstacle is detected within 25cm, the robot stops, reverses slightly, then scans left and right using the servo-mounted sensor. Based on the clearest path detected, the robot turns in the optimal direction.
Motor Control:
The rotateMotor() function handles precise control of both left and right motor sets, allowing forward, reverse, and turning movements with adjustable speeds.
Step-by-Step Assembly
1. Chassis Construction:
- Cut MDF board to create robot chassis (approx. 15x15cm)
- Mount four BO motors at each corner of the chassis
- Attach rubber wheels to all motor shafts
- Ensure proper alignment for smooth movement
2. Electronics Installation:
- Mount Arduino Uno and L298N motor driver on chassis
- Install mini breadboard for clean wiring
- Attach SG90 servo motor with ultrasonic sensor holder at front
- Position ultrasonic sensor to face forward
3. Circuit Connections:
- Connect BO motors to L298N motor driver outputs
- Wire L298N to Arduino pins 5,6,7,8,9,11
- Connect servo motor to pin 2
- Connect ultrasonic sensor to pins 3 (TRIG) and 4 (ECHO)
- Connect 7.4V battery to power the system
4. Testing & Calibration:
- Upload the Arduino code
- Test motor directions and adjust wiring if needed
- Verify ultrasonic sensor readings are accurate
- Test obstacle avoidance with various objects
- Adjust DISTANCE_TO_CHECK value for sensitivity
🤖 Autonomous Navigation
Robot independently navigates through environments without remote control
🔍 Smart Sensing
Ultrasonic sensor with servo scanning provides 180° environment awareness
⚡ Powerful Motors
Four BO motors with L298N driver provide strong and precise movement
🔋 Portable Power
7.4V battery enables cordless operation for true autonomy
Technical Specifications
- Microcontroller: Arduino Uno
- Motor Driver: L298N Dual H-Bridge
- Motors: 4x BO Motors (200RPM)
- Sensing Range: 2cm - 400cm
- Obstacle Detection: 25cm threshold
- Scanning Angle: 180° (0° to 180°)
- Power Supply: 7.4V Lithium Battery
- Chassis Material: MDF Board
- Operating Time: 1-2 hours per charge
Advanced Modifications
- Add Bluetooth module for remote control override
- Implement speed control based on distance to obstacles
- Add LED indicators for different operating modes
- Integrate line following capability
- Add wireless camera for FPV operation
- Implement path mapping and memory
Comments
Post a Comment