An Arduino-based robot car that uses an ultrasonic sensor to detect obstacles and automatically navigate around them. The robot moves forward until it detects an obstacle, then uses a servo-mounted sensor to look left and right to determine the best direction to turn.
- Autonomous obstacle detection and avoidance
- 4-wheel drive with individual motor control
- Servo-mounted ultrasonic sensor for 180° scanning
- Intelligent path planning (chooses direction with most clearance)
- Serial monitor output for debugging and monitoring
- Arduino Uno microcontroller
- Adafruit Motor Shield v1 (AFMotor library compatible)
- 4x DC motors for wheel drive
- HC-SR04 ultrasonic distance sensor
- Servo motor (for sensor mounting)
- Robot car chassis
- Power supply (battery pack recommended)
- Ultrasonic Sensor:
- Trigger Pin: Digital Pin 2
- Echo Pin: Digital Pin 13
- Servo Motor: Digital Pin 10
- Motors: Connected via Motor Shield (pins 1-4)
-
Install the required Arduino libraries:
- AFMotor library (for motor shield control)
- Servo library (usually included with Arduino IDE)
-
Connect the hardware according to the pin configuration above
-
Upload the code to your Arduino board
This repository contains two versions of the code:
The original code from the tutorial that had some issues with servo positioning and lacked debugging output.
Modified and improved version with the following enhancements:
- Added comprehensive serial debugging output
- Improved servo positioning with proper delays
- Better servo reset functionality after turning
- Enhanced stability with additional positioning delays
- More reliable obstacle detection cycle
The original tutorial code had to be modified to work properly. The main issues addressed were:
- Servo Positioning: Added proper delays for servo movement and reset positioning
- Debugging: Added extensive Serial.print statements for monitoring robot behavior
- Timing Issues: Improved delays between sensor readings and servo movements
- Stability: Better handling of servo positioning after turns
- Robot moves forward while continuously checking distance ahead
- When an obstacle is detected within the stop distance (50cm), the robot stops
- Servo turns left (180°) and takes a distance measurement
- Servo turns right (0°) and takes another distance measurement
- Robot decides which direction to turn based on available space:
- If both directions are clear (≥200cm), turn left
- If both directions are blocked (≤50cm), turn around (reverse 180°)
- Otherwise, turn toward the direction with more space
- After turning, servo resets to center position and cycle repeats
You can adjust these parameters in the code to fine-tune the robot's behavior:
motorSpeed
: Base motor speed (default: 55)motorOffset
: Compensation for motor power differences (default: 10)turnSpeed
: Additional speed when turning (default: 50)stopDist
: Minimum distance to obstacle before stopping (default: 50cm)maxDist
: Maximum sensor detection range (default: 150cm)
This project is based on the tutorial by Michael Klements from The DIY Life:
- Video Tutorial: Arduino Obstacle Avoiding Robot Car
- Blog Post: Arduino Based Obstacle Avoiding Robot Car
Note: The original code from the tutorial required modifications to work properly. This repository contains both the original (broken) version and the fixed version that addresses timing and servo positioning issues.
See the LICENSE file for details.
If your robot isn't working properly:
- Check all wiring connections
- Ensure proper power supply to motors
- Open Serial Monitor (9600 baud) to see debug output
- Verify servo moves smoothly and returns to center position
- Test ultrasonic sensor readings manually
- Adjust motor speeds and offsets if robot doesn't move straight
Potential enhancements for this project:
- Add LED indicators for different states
- Implement speed control based on distance
- Add remote control capability
- Include gyroscope for better navigation
- Implement mapping and path optimization