Obstacle Avoiding Robot Car

An obstacle avoiding robot car is a robotic vehicle designed to navigate its environment without colliding with obstacles. It uses sensors and microcontrollers to detect and avoid obstacles in its path. These robots are commonly used in automation, robotics education, and as introductory projects for learning about electronics and programming.

Components

  1. Arduino Uno
  2. Ultrasonic Sensor (e.g., HC-SR04)
  3. Servo Motor
  4. Motor Driver Module (e.g., L298N)
  5. DC Motors
  6. Battery Pack
  7. Switch
  8. Arduino IDE

Working Principle

  1. Sensor Data Collection:
    • The ultrasonic sensor continuously measures the distance between the robot and obstacles.
  2. Decision Making:
    • The Arduino Uno processes sensor input.
    • If the distance to an obstacle is below a threshold, the Arduino triggers a response (e.g., stop, turn left/right).
  3. Actuation:
    • Based on the decision, the motor driver controls the DC motors to move the car forward, turn, or reverse.

Steps to Build an Obstacle Avoiding Robot Car

  1. Assemble the Hardware:
    • Mount the motors, ultrasonic sensor, and Arduino on the chassis.
    • Connect the components as per the circuit diagram.
  2. Write the Code:
    • Write or upload the obstacle avoiding code to the Arduino using the Arduino IDE.
    • Use libraries like NewPing for ultrasonic sensors if needed.
  3. Upload and Test:
    • Upload the code to the Arduino Uno.
    • Test the robot to ensure it detects and avoids obstacles correctly.

Circuit Diagram

Obstacle Avoiding Robot Car
Obstacle Avoiding Robot Car

Connections

ComponentArduino PinPurpose
Servo MotorPin 3Controls servo rotation.
Ultrasonic TriggerPin 11Emits ultrasonic pulses.
Ultrasonic EchoPin 12Receives reflected pulses.
Right Motor EnablePin 5Controls speed (PWM).
Right Motor Dir 1Pin 7Sets direction (Forward/Reverse).
Right Motor Dir 2Pin 8Sets direction (Forward/Reverse).
Left Motor EnablePin 6Controls speed (PWM).
Left Motor Dir 1Pin 9Sets direction (Forward/Reverse).
Left Motor Dir 2Pin 10Sets direction (Forward/Reverse).

Code

#include <Servo.h>
#include <NewPing.h>

#define SERVO_PIN 3
#define ULTRASONIC_SENSOR_TRIG 11
#define ULTRASONIC_SENSOR_ECHO 12
#define MAX_REGULAR_MOTOR_SPEED 60
#define MAX_MOTOR_ADJUST_SPEED 250
#define DISTANCE_TO_CHECK 30

// Right motor
int enableRightMotor = 5;
int rightMotorPin1 = 7;
int rightMotorPin2 = 8;

// Left motor
int enableLeftMotor = 6;
int leftMotorPin1 = 9;
int leftMotorPin2 = 10;

NewPing mySensor(ULTRASONIC_SENSOR_TRIG, ULTRASONIC_SENSOR_ECHO, 400);
Servo myServo;

void setup() {
  // Motor pin setup
  pinMode(enableRightMotor, OUTPUT);
  pinMode(rightMotorPin1, OUTPUT);
  pinMode(rightMotorPin2, OUTPUT);

  pinMode(enableLeftMotor, OUTPUT);
  pinMode(leftMotorPin1, OUTPUT);
  pinMode(leftMotorPin2, OUTPUT);

  // Servo setup
  myServo.attach(SERVO_PIN);
  myServo.write(90); // Center the servo initially
  rotateMotor(0, 0); // Stop motors initially
}

void loop() {
  int distance = mySensor.ping_cm();

  // If an object is detected within the threshold distance
  if (distance > 0 && distance < DISTANCE_TO_CHECK) {
    rotateMotor(0, 0);  // Stop motors
    delay(500);

    rotateMotor(-MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED);  // Reverse
    delay(200);

    rotateMotor(0, 0);  // Stop motors
    delay(500);

    // Rotate the servo to the left and check the left side distance
    myServo.write(180);
    delay(500);
    int distanceLeft = mySensor.ping_cm(); 

    // Rotate the servo to the right and check the right side distance
    myServo.write(0);
    delay(500);
    int distanceRight = mySensor.ping_cm();

    // Return the servo to the center position
    myServo.write(90);  
    delay(500);        

    // Handle invalid distance readings (0 means no reading)
    if (distanceLeft == 0) distanceLeft = 999;  // Large value if no valid reading
    if (distanceRight == 0) distanceRight = 999;

    // Decide on which way to turn based on the side distances
    if (distanceLeft > distanceRight) {
      rotateMotor(MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED);  // Turn left (more space on the left)
      delay(200);
    } else {
      rotateMotor(-MAX_MOTOR_ADJUST_SPEED, MAX_MOTOR_ADJUST_SPEED);  // Turn right (more space on the right)
      delay(200);
    }

    rotateMotor(0, 0);  // Stop motors
    delay(200);     
  } else {
    // If no obstacle in front, move forward
    rotateMotor(MAX_REGULAR_MOTOR_SPEED, MAX_REGULAR_MOTOR_SPEED);
  }
}

// Function to control motor rotation based on speed
void rotateMotor(int rightMotorSpeed, int leftMotorSpeed) {
  // Right motor direction
  if (rightMotorSpeed < 0) {
    digitalWrite(rightMotorPin1, LOW);
    digitalWrite(rightMotorPin2, HIGH);
  } else {
    digitalWrite(rightMotorPin1, HIGH);
    digitalWrite(rightMotorPin2, LOW);
  }

  // Left motor direction
  if (leftMotorSpeed < 0) {
    digitalWrite(leftMotorPin1, LOW);
    digitalWrite(leftMotorPin2, HIGH);
  } else {
    digitalWrite(leftMotorPin1, HIGH);
    digitalWrite(leftMotorPin2, LOW);
  }

  // Set motor speeds
  analogWrite(enableRightMotor, abs(rightMotorSpeed));
  analogWrite(enableLeftMotor, abs(leftMotorSpeed));    
}

Applications

  1. Automation:
    • Useful in factories and warehouses for autonomous navigation.
  2. Education:
    • A great project for learning robotics and Arduino programming.
  3. Hobbyist Projects:
    • Popular among robotics enthusiasts for DIY projects.
  4. Research:
    • Basis for advanced navigation and AI-based robots.

More Project

  • Obstacle Avoiding Robot Car

    Obstacle Avoiding Robot Car

    An obstacle avoiding robot car is a robotic vehicle designed to navigate its environment without colliding with obstacles. It uses sensors and microcontrollers to detect and avoid obstacles in its …

  • Amazing LED Chaser Circuit – Beautiful Decoration Idea

    Amazing LED Chaser Circuit – Beautiful Decoration Idea

    LED chasers are a fun and creative way to display a sequence of lights. In this blog post, we’ll show you how to build an LED chaser using a 555 …

  • LED Indicator Circuit

    LED Indicator Circuit

    LED indicators are crucial components in various electronic projects. They provide visual feedback on the status of a circuit or system. In this blog post, we’ll explore how to create …