← All Projects

Obstacle Avoiding Robot with Servo Scanning

365 viewsMarch 8, 2026
ArduinoRobotHC-SR04ServoL298NAutonomous

An autonomous robot that navigates around obstacles using an ultrasonic sensor mounted on a servo for 180° scanning. The robot looks left and right when an obstacle is detected and chooses the clearest path to continue.

Components & Supplies

×1Arduino Uno R3
×1HC-SR04 Ultrasonic Sensor
×1SG90 Servo Motor
×1L298N Motor Driver Module
×2DC Gear Motor (3-6V)
×1Robot Chassis Kit (2WD)
×14x AA Battery Holder

Circuit Connections

ComponentPinPinComponent
ArduinoA0TRIGHC-SR04
ArduinoA1ECHOHC-SR04
Arduino5VVCCHC-SR04
ArduinoGNDGNDHC-SR04
ArduinoD3 (PWM)SignalServo
ArduinoD5 (PWM)ENAL298N
ArduinoD6IN1L298N
ArduinoD7IN2L298N
ArduinoD8IN3L298N
ArduinoD9IN4L298N
ArduinoD10 (PWM)ENBL298N

How It Works

Unlike simple obstacle avoidance robots that only detect obstacles straight ahead, this robot has an HC-SR04 sensor mounted on a servo. When an obstacle is detected within 25cm, the robot stops and the servo sweeps the sensor left and right to measure distances in both directions. The robot then turns toward the direction with the most clearance.

This creates more intelligent navigation behavior and reduces getting stuck in corners.

arduinoObstacle Avoidance Sketch
#include <Servo.h>

Servo scanServo;
const int trigPin = A0;
const int echoPin = A1;
const int enA = 5, in1 = 6, in2 = 7;
const int enB = 10, in3 = 8, in4 = 9;
const int spd = 160;

float getDistance() {
  digitalWrite(trigPin, LOW); delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  return (pulseIn(echoPin, HIGH) * 0.0343) / 2.0;
}

void forward() {
  analogWrite(enA, spd); analogWrite(enB, spd);
  digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
}
void stopBot() { analogWrite(enA, 0); analogWrite(enB, 0); }
void turnLeft() {
  analogWrite(enA, spd); analogWrite(enB, spd);
  digitalWrite(in1, LOW); digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
}
void turnRight() {
  analogWrite(enA, spd); analogWrite(enB, spd);
  digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
  digitalWrite(in3, LOW); digitalWrite(in4, HIGH);
}

void setup() {
  Serial.begin(9600);
  scanServo.attach(3);
  scanServo.write(90);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT);
  delay(2000);
}

void loop() {
  scanServo.write(90);
  delay(200);
  float frontDist = getDistance();

  if (frontDist > 25) {
    forward();
  } else {
    stopBot();
    delay(300);

    scanServo.write(180);
    delay(500);
    float leftDist = getDistance();

    scanServo.write(0);
    delay(500);
    float rightDist = getDistance();

    scanServo.write(90);

    if (leftDist > rightDist) {
      turnLeft();
      delay(400);
    } else {
      turnRight();
      delay(400);
    }
    stopBot();
  }
  delay(50);
}