Arduino Obstacle Avoiding Robot Car

If you are building an obstacle-avoiding robot using four DC motors, an ultrasonic sensor, and a servo motor for directional scanning. Below are the complete details including components, wiring, and setup.


βœ… Components List

  • Arduino Uno
  • Motor Driver Shield HW-130
  • DC Motors
  • Ultrasonic Sensor (HC-SR04)
  • Servo Motor
  • Robot Chassis with Wheels
  • Power Supply (Battery Pack or Li-ion Battery)
  • Jumper Wires

πŸ”Œ Wiring Connections

🟦 Motor Connections (Motor Shield)

  • DC Motor 1 β†’ M1 terminals
  • DC Motor 2 β†’ M2 terminals
  • DC Motor 3 β†’ M3 terminals
  • DC Motor 4 β†’ M4 terminals

⚠️ Ensure correct polarity for consistent direction of movement.

🟩 Ultrasonic Sensor (HC-SR04)

HC-SR04 PinConnects ToNotes
VCC5V on Arduino (via shield)
GNDGND
TrigA0As per #define trigPin A0
EchoA1As per #define echoPin A1

Please note – Connect servo motor to pins given motor driver shield directly. Based on code signal pin is connected to pin 9.

🟨 Servo Motor

Servo WireConnects ToNotes
Red (VCC)5VCan draw from Arduino’s 5V if low-torque servo
Brown/Black (GND)GND
Orange/Yellow (Signal)Pin 9As per #define servoPin 9

πŸ› οΈ Assembly Steps

  1. Mount motors onto the chassis and connect them to the Motor Shield terminals.
  2. Stack the Motor Shield on top of the Arduino Uno.
  3. Mount the servo to the front-top part of the chassis.
  4. Attach the ultrasonic sensor to the servo horn or bracket.
  5. Wire the ultrasonic sensor to pins A0 (Trig) and A1 (Echo).
  6. Connect the servo signal wire to Servo Pins given on Motor Driver.
  7. Upload the code to Arduino using the Arduino IDE.
  8. Power the system

🟦 Arduino Code

#include <AFMotor.h>
#include <Servo.h>

// Motors: M1–M4
AF_DCMotor motor1(1);  // M1
AF_DCMotor motor2(2);  // M2
AF_DCMotor motor3(3);  // M3
AF_DCMotor motor4(4);  // M4

// Ultrasonic pins
#define trigPin A0
#define echoPin A1

// Servo motor
Servo myServo;
#define servoPin 9  // Servo connected to D9

void setup() {
  Serial.begin(9600);

  // Set motor speeds (0–255)
  motor1.setSpeed(150);  // Increased from 100
  motor2.setSpeed(150);
  motor3.setSpeed(150);
  motor4.setSpeed(150);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Attach and center the servo
  myServo.attach(servoPin);
  myServo.write(90);
  delay(500);
  Serial.println("Setup complete. Servo centered.");
}

void loop() {
  int distance = getDistance();
  Serial.print("Distance: ");
  Serial.println(distance);

  if (distance < 20) {
    stopMotors();
    delay(300);

    Serial.println("Obstacle detected. Reversing...");
    moveBackward();
    delay(500);
    stopMotors();
    delay(300);

    // Scan left
    myServo.write(0);
    delay(700);
    int leftDist = getDistance();
    Serial.print("Left distance: ");
    Serial.println(leftDist);

    // Scan right
    myServo.write(180);
    delay(700);
    int rightDist = getDistance();
    Serial.print("Right distance: ");
    Serial.println(rightDist);

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

    // Decide direction to turn
    if (leftDist > rightDist) {
      Serial.println("Turning left...");
      turnLeft();
    } else {
      Serial.println("Turning right...");
      turnRight();
    }
  } else {
    moveForward();
  }

  delay(100);  // Main loop delay
}

// Get distance from ultrasonic sensor
int getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2;
}

// Move forward
void moveForward() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

// Move backward
void moveBackward() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}

// Turn left with increased speed and duration
void turnLeft() {
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor3.setSpeed(200);
  motor4.setSpeed(200);

  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);

  delay(1000);  // Turn duration

  stopMotors();
  delay(200);

  // Reset speed
  motor1.setSpeed(150);
  motor2.setSpeed(150);
  motor3.setSpeed(150);
  motor4.setSpeed(150);
}

// Turn right with increased speed and duration
void turnRight() {
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor3.setSpeed(200);
  motor4.setSpeed(200);

  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);

  delay(1000);  // Turn duration

  stopMotors();
  delay(200);

  // Reset speed
  motor1.setSpeed(150);
  motor2.setSpeed(150);
  motor3.setSpeed(150);
  motor4.setSpeed(150);
}

// Stop all motors
void stopMotors() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}


πŸ› οΈ Tips & Troubleshooting

  • Use Serial Monitor at 9600 baud to debug distances.
  • Servo jitter? Power it separately.
  • Motors weak? Check battery level or increase PWM speed.
  • Calibrate sensor: Sometimes adding delays/stabilization helps.