Translate

DIY Obstacle Avoidance with Arduino

1. Introduction

In the world of robotics, obstacle avoidance is a fundamental behavior. This project demonstrates how to build a 4-wheel drive (4WD) robot that "sees" the world using an ultrasonic sensor. When it detects an object, it stops, scans its surroundings with a servo motor, and decides the best path to take—all without any human input.

2. Components

To build this robot, you will need:

  • Microcontroller: Arduino Uno R3

  • Motor Driver: L298N Module (Dual H-Bridge)

  • Sensor: HC-SR04 Ultrasonic Sensor

  • Actuator: SG90 Servo Motor

  • Motors: 4x DC Gear Motors (6V-9V)

  • Power: 2x 18650 Li-ion Batteries (7.4V total)

  • Chassis: 4WD Robot Car Kit

  • Misc: SPST Switch, Jumper Wires, Breadboard (optional)


3. Circuit and Connections

Circuit Diagram for Obstacle Avoidance

Pin Diagram Ultrasonic Sensor

Pin Diagram of L298N



Based on the schematic, here are the precise wiring connections:


Motor Driver (L298N) to Arduino:

ComponentPinArduino Connection
L298N IN1 / IN2D4 / D5Left Motors Control
L298N IN3 / IN4D6 / D7Right Motors Control
Servo SignalD10Neck Movement
Ultrasonic TrigA1Sound Output
Ultrasonic EchoA2Sound Input
Sensors and Servo:

  • Ultrasonic Trig: Arduino A1

  • Ultrasonic Echo: Arduino A2

  • Servo Signal (Orange): Arduino D10

  • VCC/GND: All connected to Arduino 5V and GND pins.

Power Routing:

  • Battery (+): To Switch → L298N 12V Terminal.

  • Battery (-): To L298N GND Terminal.

  • Arduino Power: Connect L298N 5V out to Arduino 5V pin (Common GND is essential).


4. Circuit Working

The L298N Motor Driver acts as the high-power bridge between the batteries and the motors. Since the Arduino pins cannot provide enough current to spin four motors, the L298N takes logic signals (5V) from the Arduino and switches the higher battery voltage (7.4V) to the motors. The Ultrasonic Sensor acts as the eyes, and the Servo acts as the neck, allowing the "eyes" to rotate 180 degrees.


5. Code

Copy and paste this code into your Arduino IDE:

#include <Servo.h>

// Motor Pins
const int LeftFwd = 4;
const int LeftBack = 5;
const int RightFwd = 6;
const int RightBack = 7;

// Sensor Pins
const int trig = A1;
const int echo = A2;

Servo neckServo;
int distance = 100;

void setup() {
  pinMode(LeftFwd, OUTPUT);
  pinMode(LeftBack, OUTPUT);
  pinMode(RightFwd, OUTPUT);
  pinMode(RightBack, OUTPUT);
  
  neckServo.attach(10);
  neckServo.write(90); // Center the sensor
  delay(2000); 
}

void loop() {
  distance = getDistance();

  if (distance <= 25) {
    stopRobot();
    delay(100);
    moveBackward();
    delay(300);
    stopRobot();
    
    int distRight = look(10);  // Look Right
    delay(200);
    int distLeft = look(170); // Look Left
    delay(200);

    if (distRight >= distLeft) {
      turnRight();
    } else {
      turnLeft();
    }
    stopRobot();
  } else {
    moveForward();
  }
}

int getDistance() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long duration = pulseIn(echo, HIGH);
  return duration * 0.034 / 2;
}

int look(int angle) {
  neckServo.write(angle);
  delay(500);
  int d = getDistance();
  neckServo.write(90);
  return d;
}

void moveForward() {
  digitalWrite(LeftFwd, HIGH); digitalWrite(RightFwd, HIGH);
  digitalWrite(LeftBack, LOW); digitalWrite(RightBack, LOW);
}

void moveBackward() {
  digitalWrite(LeftBack, HIGH); digitalWrite(RightBack, HIGH);
  digitalWrite(LeftFwd, LOW); digitalWrite(RightFwd, LOW);
}

void turnRight() {
  digitalWrite(LeftFwd, HIGH); digitalWrite(RightBack, HIGH);
  digitalWrite(LeftBack, LOW); digitalWrite(RightFwd, LOW);
  delay(500);
}

void turnLeft() {
  digitalWrite(LeftBack, HIGH); digitalWrite(RightFwd, HIGH);
  digitalWrite(LeftFwd, LOW); digitalWrite(RightBack, LOW);
  delay(500);
}

void stopRobot() {
  digitalWrite(LeftFwd, LOW); digitalWrite(LeftBack, LOW);
  digitalWrite(RightFwd, LOW); digitalWrite(RightBack, LOW);
}

6. Code Working

The script uses a conditional logic flow:

  1. Sensing: The getDistance() function triggers the ultrasonic sensor.

  2. Comparison: If distance is less than 25cm, the loop() pauses the motors.

  3. Scanning: The look() function moves the servo, takes a reading, and returns to center.

  4. Action: The if (distRight >= distLeft) statement decides which side has more "room" and tells the motors to rotate in opposite directions to spin the robot toward the clear path.


7. Tips

  • Speed Control: If your robot moves too fast, you can use analogWrite() on the L298N Enable pins (ENA/ENB) to slow it down.

  • Sensor Noise: If the robot shakes or stops for no reason, check for loose wires on the Echo and Trig pins.

  • Turn Calibration: Adjust the delay(500) in the turnRight and turnLeft functions to ensure your robot turns exactly 90 degrees on your specific floor surface.


8. Uses

  • Education: Great for learning about pulse-width modulation (PWM) and ultrasonic physics.

  • Commercial: Concepts used in robotic vacuum cleaners (like Roomba) and warehouse AGVs.

  • Development: A base platform for adding Bluetooth control or Wi-Fi cameras later.


9. Conclusion

The Arduino Obstacle Avoiding Robot is a milestone project for any maker. It combines motor control, distance sensing, and logic into one autonomous package. Once you master this, you can easily add infrared sensors for line following or a Wi-Fi module for IoT control.

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.