Lesson 2: How to Make an Obstacle Avoiding Robot

1. Introduction

Welcome to Robotics Lesson 2! In this hands-on tutorial, you’ll build an obstacle avoiding robot using the HC-SR04 ultrasonic sensor and the Arduino robot car you built in lesson 1. This project is good for robotics beginners who finished learning electronics and Arduino. It teaches you sensor integration, motor control, and autonomous navigation. By the end, your robot will detect obstacles and maneuver around them autonomously. Let’s get started!


2. Components Needed

  • Arduino Uno (or compatible board)
  • HC-SR04 Ultrasonic Sensor (distance measurement)
  • L298N Motor Driver Module (controls motors)
  • 2x DC Motors (6V–12V) with wheels
  • Robot Chassis Kit (includes frame, wheels, screws)
  • 8×1.5V (or 2×18650 Li-ion) Battery Holder (with batteries)
  • Jumper Wires (male-to-male and male-to-female)
  • Mini breadboard (optional, for wiring)

3. Hardware Setup

3.1. Mechanical Assembly
  1. Attach motors to the chassis using screws.
  2. Mount wheels onto motor shafts.
  3. Fix Arduino, L298N, and battery holder to the chassis using screws (or rubber bands/double-sided tape).
  4. Fix the HC-SR04 sensor at the front (facing forward).
3.2. Electrical Connections


HC-SR04 to Arduino:

  • VCC → Arduino 5V
  • GND → Arduino GND
  • TRIG → Arduino Pin 12
  • ECHO → Arduino Pin 13

L298N to Arduino & Motors:

  • ENB→Pin 3
  • IN3 → Pin 4
  • IN4 → Pin 5
  • ENA→Pin 11
  • IN1 → Pin 10
  • IN2 → Pin 9
  • Motor A → Left motor terminals
  • Motor B → Right motor terminals
  • 12V Input → Battery (+)
  • GND → Battery (-) & Arduino GND

Power:

  • Connect Arduino Vin to L298N 5V output.

4. Code

Upload this Arduino Sketch:

const int trig=12;
const int echo=13; 
const int left2=5;   //IN4
const int left1=4;  // IN3
const int leftSpeed=3;  //ENB
const int right1=10;   //IN1
const int right2=9;  //IN2
const int rightSpeed=11; //ENA

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);
  pinMode(left1, OUTPUT);
  pinMode(left2, OUTPUT);
  pinMode(right1, OUTPUT);
  pinMode(right2, OUTPUT);
  pinMode(leftSpeed, OUTPUT);
  pinMode(rightSpeed, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
 long dist=getDistance();
 if(dist<15){
    left();
    delay(500);
  }
  else {
    forward();
  }
}


void forward(){
  digitalWrite(left1,HIGH);
  digitalWrite(left2,LOW);
  analogWrite(leftSpeed,180);
  digitalWrite(right1,HIGH);
  digitalWrite(right2,LOW);
  analogWrite(rightSpeed,80);
}




void backwards(){
  digitalWrite(left2,HIGH);
  digitalWrite(left1,LOW);
  analogWrite(leftSpeed,80);
  digitalWrite(right2,HIGH);
  digitalWrite(right1,LOW);
  analogWrite(rightSpeed,80);
}

void stopCar(){
  digitalWrite(left2,LOW);
  digitalWrite(left1,LOW);
  analogWrite(leftSpeed,0);
  digitalWrite(right2,LOW);
  digitalWrite(right1,LOW);
  analogWrite(rightSpeed,0);
}

void left(){
  digitalWrite(left1,LOW);
  digitalWrite(left2,LOW);
  analogWrite(leftSpeed,0);
  digitalWrite(right1,HIGH);
  digitalWrite(right2,LOW);
  analogWrite(rightSpeed,150);
}

void right(){
  digitalWrite(left1,HIGH);
  digitalWrite(left2,LOW);
  analogWrite(leftSpeed,150);
  digitalWrite(right1,LOW);
  digitalWrite(right2,LOW);
  analogWrite(rightSpeed,0);
}

long getDistance(){
  long duration, distance;

  // Trigger ultrasonic pulse
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  // Calculate distance
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2; // Speed of sound= 340m/s

  Serial.print("Distance: ");
  Serial.println(distance);
  return distance;
}


How It Works:

The Arduino program continuously to:

  1. Measures distance using the HC-SR04 sensor, as previously explained in this lesson.
  2. Decides movement based on obstacle distance
    If an obstacle is detected (<15cm) → Turns right → Resumes.
    Otherwise, it goes forward.
  3. Controls motors via the L298N driver
  4. Repeats this loop automatically

5. Common Troubleshoots

  • Motors not spinning and making weird sounds?
  • Check L298N power supply (battery voltage >7V, recharge if needed).
  • Ensure motor wires are firmly connected to L298N.
  • Car not going forward/turning left or right properly
  • Check your wiring or code, you may need to flip right1, right2 or left1,left2.
  • Incorrect distance readings?
  • Make sure front of HC-SR04 is clear
  • Make sure the HC-SR04 connection are as per code
  • Random movements?
  • Add delay(50) after distance check to reduce noise.
  • Ensure the Arduino and L298N share a common ground.

6. How to Go Further

  1. Make a still robot (use StopCar()) that goes back for 1 sec when you approach it.
  2. Add Speed Control: Try to change the forward speed according to the object’s distance using PWM concept.
  3. Maze Robot: Add side-facing HC-SR04 sensors for better navigation, choosing the clearest path (left/right).
  4. Advanced Logic: Use PID control for smoother turns.

You can also upgrade to a maze-solving robot using a single servo to turn HC-SR04 ultrasonic sensor into a radar, look left and right then move!


Stay tuned to Samerli Lessons to learn more about our robotics, Arduino and electronics.


Posted

in

by

Comments

One response to “Lesson 2: How to Make an Obstacle Avoiding Robot”

  1. […] Path:This basic robot car becomes the foundation for Lesson 2 (Obstacle Avoidance) when you add an HC-SR04 ultrasonic […]

Leave a Reply

Your email address will not be published. Required fields are marked *