Introduction: Obstacle Avoider Using HC-SR04 Ultrasonic Sensor

About: Current City is the place where you learn and have fun with electronics and Arduino. Get free tutorials and projects in Arduino and electronics complete with all source code and project files!!


This project uses an HC-SR04 Ultrasonic sensor and an Arduino to measure the distance between the user and an obstacle and cause an LED, buzzer and motor to flash, beep and vibrate respectively to alert the user that he is approaching an obstacle. This device can be very useful to blind people and other people with some form of impaired vision.


An HC-SR04 ultrasonic sensor works with the Arduino to measure the distance between the device/user and an obstacle in front of it. Based on the distance between the device and the user, the LED flashes, the buzzer beeps and the motor vibrates. The most important thing to note here is that the intervals at which the LED, piezo buzzer and DC Motor flash, beep and vibrate respectively is directly proportional to the distance between the ultrasonic sensor and the obstacle, hence the smaller the distance between the sensor and the obstacle in front of it, the faster the LED, buzzer and motor turn on and off and vice versa.

Step 1: Schematic and Simulation

Here is a breadboard circuit of the project designed in Fritzing. If you want to learn how to use Frizing, check out this tutorial I created.

Components You'll need
Here are the components you'll need for this project

  • Arduino Uno
  • HC-SR04 Ultrasonic sensor
  • Solderless Breadboard
  • Small 5v DC motor
  • Piezo Buzzer1
  • Red LED
  • 1 220 ohm resistor
  • 1 1kOhm resistor
  • 1 BC547 NPN Transistor
  • 1 1N4001 diode
  • A number of male to male jumpers
  • If you live in Ghana, you can get these components from Invent Electronics. If you leave elsewhere, look for an electronics or hobby store near you.

Step 2: Arduino Code

Arduino Code

/* This project uses an HC-SR04 Ultrasonic sensor and an arduino to detect obstacles in front of the user and sound an alarm. The interval at which the alarm sounds in proportional to the distance between the user and the alarm.

created 01 Mar 2016 by Isaac Sesi Current City */

#include // This library helps makes our readings more accurate

const int TRIGGER_PIN = 5; const int ECHO_PIN = 6; const int ledPin = 9; const int motor = 10; const int buzzer = 11;

const int MAX_DISTANCE = 300; // Set maximum distance which can be measured to 300cm const int SAMPLE_INTERVAL = 25; // Take a sample reading every 25milliseconds const int PING_ITERATIONS = 3; // Take 3 readings before returning the average of the 3 NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);//Create a new instance of the object

int systemState = LOW; //Make sure everything is off long previousMillis = 0; long interval;

void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); pinMode(buzzer, OUTPUT); pinMode(motor, OUTPUT); } void loop() { float d1 = getDistance(); // call the function which measures the distance

/* Map the distance measured to the number of milliseconds that LED, buzzer and motor should stay on or off. VERY IMPORTANT! */ interval = map(d1, 0, 250, 0, 2000); if(d1>0 && d1<=250) //Only do something if distance measured falls within this range { changeStateWithoutDelay(interval); //Call function to turn LED, Buzzer or motor on/off } else{ digitalWrite(ledPin, LOW); digitalWrite(buzzer, LOW); digitalWrite(motor, LOW); }

Serial.print(d1); Serial.print("cm \t"); //delay(100); //Uncomment this line to make the Serial monitor print out stuff more slowly Serial.print(interval); Serial.print("milliseconds \t\n"); Serial.println(); }

//Function which measures distance float getDistance() { float t = sonar.ping_median(PING_ITERATIONS); // microseconds float d = sonar.convert_cm(t); // centimeters return d; }

// Function which turns LED,buzzer and motor on or off based on how far sensor is from the obstacle void changeStateWithoutDelay(int interval) { unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { // save the last time you turned everything off previousMillis = currentMillis;

// if the LED, buzzer and motor is off turn it on and vice-versa: if (systemState == LOW) systemState = HIGH; else systemState = LOW;

// set the LED, buzzer and motor with the systemState of the variable: digitalWrite(ledPin, systemState); digitalWrite(buzzer, systemState); digitalWrite(motor, systemState); } }

Code looks more organized when you view it here.

I have tried to comment the code as much as possible to make it very easy to understand, however parts of the code which may still be unclear are explained in the video. If you haven't installed the Arduino IDE already, download and install from here

Step 3: Final Circuit and Resources


Download this zipped file containing the resources needed for this project. It contains the following:

  • Arduino SketchNewPing library (Extract into your Arduino Libraries folder).
  • Arduino Hex File For Proteus
  • If you are interested in running the simulation and do not already have the Arduino Library for Proteus, The Engineering Projects created a cool library for that.
  • Download both the Ultrasonic sensor library and the Arduino library for Proteus

And That's it!

Visit our website for more tutorials and project in Arduino and Electronics,