This project implements an obstacle-avoiding robot using an ultrasonic sensor for distance measurement and motor driver control. The robot moves forward until it detects an obstacle within 15 cm, at which point it stops, moves backward, and makes a left turn to avoid the obstacle.
The robot's motion is controlled by an Arduino and includes functions for moving forward, backward, turning left, and stopping. The ultrasonic sensor continuously monitors the distance to obstacles and adjusts the robot's motion accordingly.
- Arduino Uno (or any compatible board)
- Ultrasonic Sensor (HC-SR04)
- Trig Pin: Sends ultrasonic sound waves
- Echo Pin: Receives reflected waves
- Motor Driver (L298N) or equivalent
- DC Motors (2 motors, left and right)
- Wheels (compatible with the DC motors)
- Power Supply (for motors and Arduino)
- Connecting Wires and a Breadboard
- Obstacle Detection: Measures distance using an ultrasonic sensor.
- Autonomous Navigation: The robot moves forward, avoids obstacles, and recalculates its path.
- Motor Control: Utilizes PWM signals for speed control of DC motors.
Component | Arduino Pin |
---|---|
Ultrasonic Sensor Trig | 2 |
Ultrasonic Sensor Echo | 3 |
Motor Driver ENA | 11 |
Motor Driver ENB | 10 |
Left Motor Pin 1 | 7 |
Left Motor Pin 2 | 6 |
Right Motor Pin 1 | 5 |
Right Motor Pin 2 | 4 |
- Measures the distance to the nearest object using sound waves.
- The trigger pin sends a short pulse, and the echo pin receives the reflected pulse.
- The time taken for the pulse to return is converted to distance.
- The L298N motor driver controls the left and right DC motors.
- Motors are driven in different directions to achieve forward, backward, or turning movements.
- Move Forward: If no obstacle is detected within 15 cm.
- Stop: When an obstacle is detected.
- Move Backward: Reverses direction for 2.5 seconds upon obstacle detection.
- Turn Left: Rotates left for 2 seconds to find a clear path.
- Initializes the ultrasonic sensor pins, motor driver pins, and the serial monitor for debugging.
moveForward()
: Sets motor directions and speeds for forward motion.moveBackward()
: Reverses motor directions for backward motion.turnLeft()
: Spins the robot left by controlling motor directions and speeds.Stop()
: Stops the robot by setting motor speeds to zero.
- Measures the distance to the nearest object.
- Executes movement commands based on the measured distance:
- Distance ≥ 15 cm: Move forward.
- Distance < 15 cm: Stop, move backward, and turn left.
- Connect the components as per the pin configuration table.
- Upload the provided Arduino code to the Arduino board.
- Power the robot using an appropriate power source.
- Place the robot on a flat surface and observe its obstacle-avoidance behavior.
- Add additional sensors for more precise navigation.
- Implement turning right as an additional obstacle avoidance option.
- Enhance the logic to handle more complex obstacle layouts.
- Introduce a buzzer or LED indicators for feedback.
This project is open-source and can be freely used and modified for educational purposes.