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.

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 Buzzer
  • 1 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.


Here is a breadboard circuit of the project designed in Fritzing.


Fritzing breadboard layout of the circuit


Here is another design and simulation of the circuit which I created in Proteus. There is a link to the Proteus file and other files and resources for this project at the bottom of the post.


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 <NewPing.h>  // 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() {
  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
    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");

//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;
      systemState = LOW;

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

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

Final Circuit


Circuit assembled on breadboard


Circuit assembled on breadboard


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

  • Arduino Sketch
  • NewPing library (Extract library into your Arduino Libraries folder).
  • Proteus Design File (You’ll have to extract the Arduino Library for Protues and Ultrasonic Sensor library for Proteus for the simultation to work. I used Proteus 7.8)
  • 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 Ardino library for Proteus.

Like this tutorial? Found a mistake? Want to suggest improvements or corrections? Please leave us a comment.

0 0 vote
Article Rating
Would love your thoughts, please comment.x
%d bloggers like this: