Home Blog Blog Details

How to Using Arduino Designing a Complete Obstacle-Avoiding Robot?

January 03 2025
Ampheo

Inquiry

Global electronic component supplier AMPHEO PTY LTD: Rich inventory for one-stop shopping. Inquire easily, and receive fast, customized solutions and quotes.

QUICK RFQ
ADD TO RFQ LIST
We’ll build an ultrasonic sensor-based obstacle-avoiding robot using Arduino. It’s a common, beginner-friendly approach with a balance of simplicity and effectiveness.

We’ll build an ultrasonic sensor-based obstacle-avoiding robot using Arduino. It’s a common, beginner-friendly approach with a balance of simplicity and effectiveness.

How to Designing a Complete Obstacle-Avoiding Robot? - Blog - Ampheo


📋 1. Components Required:

Electronics:

  • 🧠 Arduino Uno (or compatible microcontroller)
  • 📡 Ultrasonic Sensor (e.g., HC-SR04)
  • 🔌 Motor Driver Module (L298N)
  • 🚗 2 DC Motors with Wheels
  • 🔄 Caster Wheel (for balance)
  • 🔋 Battery Pack (9V or 12V)
  • 📦 Chassis/Body Kit
  • 🪛 Breadboard and Jumper Wires

Optional:

  • ⚡ On/Off Switch
  • 🔊 Buzzer (for sound feedback)
  • 💡 LEDs (for status indication)

🛠️ 2. Circuit Diagram and Connections:

Ultrasonic Sensor (HC-SR04):

  • VCC: 5V on Arduino
  • GND: GND on Arduino
  • Trigger Pin: Digital Pin 9
  • Echo Pin: Digital Pin 10

Motor Driver (L298N):

  • IN1 & IN2 (Motor 1): Digital Pin 4, 5
  • IN3 & IN4 (Motor 2): Digital Pin 6, 7
  • ENA & ENB: Connect to PWM pins (e.g., 3, 11)
  • VCC: Battery Positive
  • GND: Battery Ground & Arduino GND

DC Motors:

  • Motor 1: Left Wheel
  • Motor 2: Right Wheel

💻 3. Arduino Code:

Below is a basic example of an obstacle-avoiding robot code:

cpp
 
// Pin Definitions
const int trigPin = 9;
const int echoPin = 10;
const int motorLeft1 = 4;
const int motorLeft2 = 5;
const int motorRight1 = 6;
const int motorRight2 = 7;
const int motorSpeedLeft = 3;
const int motorSpeedRight = 11;
 
// Variables
long duration;
float distance;
 
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorLeft1, OUTPUT);
pinMode(motorLeft2, OUTPUT);
pinMode(motorRight1, OUTPUT);
pinMode(motorRight2, OUTPUT);
pinMode(motorSpeedLeft, OUTPUT);
pinMode(motorSpeedRight, OUTPUT);
}
 
float getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2; // Convert to centimeters
}
 
void moveForward() {
analogWrite(motorSpeedLeft, 150);
analogWrite(motorSpeedRight, 150);
digitalWrite(motorLeft1, HIGH);
digitalWrite(motorLeft2, LOW);
digitalWrite(motorRight1, HIGH);
digitalWrite(motorRight2, LOW);
}
 
void moveBackward() {
digitalWrite(motorLeft1, LOW);
digitalWrite(motorLeft2, HIGH);
digitalWrite(motorRight1, LOW);
digitalWrite(motorRight2, HIGH);
delay(500);
}
 
void turnRight() {
digitalWrite(motorLeft1, HIGH);
digitalWrite(motorLeft2, LOW);
digitalWrite(motorRight1, LOW);
digitalWrite(motorRight2, HIGH);
delay(500);
}
 
void turnLeft() {
digitalWrite(motorLeft1, LOW);
digitalWrite(motorLeft2, HIGH);
digitalWrite(motorRight1, HIGH);
digitalWrite(motorRight2, LOW);
delay(500);
}
 
void stopMotors() {
digitalWrite(motorLeft1, LOW);
digitalWrite(motorLeft2, LOW);
digitalWrite(motorRight1, LOW);
digitalWrite(motorRight2, LOW);
}
 
void loop() {
distance = getDistance();
Serial.println(distance);
if (distance < 20) { // Obstacle detected within 20 cm
stopMotors();
delay(200);
moveBackward();
delay(500);
turnRight();
delay(500);
  } else {
moveForward();
}
}

🔄 4. Working Principle:

  1. The ultrasonic sensor emits sound waves to detect obstacles.
  2. If an obstacle is detected within 20 cm, the robot:
    • Stops
    • Moves backward
    • Turns (e.g., right)
  3. If no obstacle is detected, the robot continues moving forward.

📊 5. Calibration and Testing:

  1. Upload the code to Arduino.
  2. Power the robot using the battery pack.
  3. Place obstacles in front and observe how it reacts.
  4. Adjust motor speed and distance threshold if needed.

🧠 6. Improvements (Optional):

  • Add multiple ultrasonic sensors (front, left, and right) for better coverage.
  • Integrate an IR sensor for closer range detection.
  • Add a buzzer or LED for alerts.
  • Use AI or camera-based vision systems for smarter navigation.

 

Isn't it easy? Have you learned it?

Ampheo