Skip to content

lamcnguyen89/Arduino_Obstacle_Avoiding_Robot

Repository files navigation

Arduino Obstacle Avoiding Robot Car

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.

Features

  • 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

Hardware Components

  • 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)

Pin Configuration

  • 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)

Installation

  1. Install the required Arduino libraries:

    • AFMotor library (for motor shield control)
    • Servo library (usually included with Arduino IDE)
  2. Connect the hardware according to the pin configuration above

  3. Upload the code to your Arduino board

Code Versions

This repository contains two versions of the code:

ObstacleAvoidingRobot_Broken

The original code from the tutorial that had some issues with servo positioning and lacked debugging output.

ObstacleAvoidingRobot_Fixed

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

Key Modifications Made

The original tutorial code had to be modified to work properly. The main issues addressed were:

  1. Servo Positioning: Added proper delays for servo movement and reset positioning
  2. Debugging: Added extensive Serial.print statements for monitoring robot behavior
  3. Timing Issues: Improved delays between sensor readings and servo movements
  4. Stability: Better handling of servo positioning after turns

How It Works

  1. Robot moves forward while continuously checking distance ahead
  2. When an obstacle is detected within the stop distance (50cm), the robot stops
  3. Servo turns left (180°) and takes a distance measurement
  4. Servo turns right (0°) and takes another distance measurement
  5. 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
  6. After turning, servo resets to center position and cycle repeats

Configuration Parameters

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)

References

This project is based on the tutorial by Michael Klements from The DIY Life:

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.

License

See the LICENSE file for details.

Troubleshooting

If your robot isn't working properly:

  1. Check all wiring connections
  2. Ensure proper power supply to motors
  3. Open Serial Monitor (9600 baud) to see debug output
  4. Verify servo moves smoothly and returns to center position
  5. Test ultrasonic sensor readings manually
  6. Adjust motor speeds and offsets if robot doesn't move straight

Future Improvements

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

About

Code For Obstacle Avoiding Robot for Arduino. Had to make changes from the tutorial to get it to work

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages