Arduino – Ultrasonic Sensor with LEDs and buzzer

Hi guys,

This is my first tutorial,  I’ve just started a few months ago learning electronics (previously I’ve just knew how to program in C and Python). When I finished this project (took me about 5 hours or something) I’ve decided to share to help anyone that is starting in electronics.

What’s this project about?

Basically we have an Ultrasonic sensor that measures the distance and the LED’s bar graph will light up according to our distance from the sensor and as we get closer the buzzer beeps in a different way. This circuit can work as a parking sensor! It’s easy and cheap.

Parts Required

parts

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

Schematics

Upload the Code below

/*
 * created by Rui Santos, https://randomnerdtutorials.com
 * Ultrasonic Sensor with LED's bar graph and buzzer 
*/
int tonePin = 4;    //Tone - Red Jumper
int trigPin = 9;    //Trig - violet Jumper
int echoPin = 10;   //Echo - yellow Jumper
int clockPin = 11;  //IC Pin 11 - white Jumper
int latchPin = 12;  //IC Pin 12 - Blue Jumper
int dataPin = 13;   //IC Pin 14 - Green Jumper
 
byte possible_patterns[9] = {
B00000000, 
B00000001,
B00000011,
B00000111,
B00001111,
B00011111,
B00111111, 
B01111111, 
B11111111,
};
int proximity=0;
int duration;
int distance;
 
void setup() {
  //Serial Port
  Serial.begin (9600);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(tonePin, OUTPUT);
}
 
void loop() {
  digitalWrite(latchPin, LOW);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  /*if (distance >= 45 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }*/
  
  proximity=map(distance, 0, 45, 8, 0);
  //Serial.println(proximity);
  
  if (proximity <= 0){
    proximity=0;
  }
  else if (proximity >= 3 && proximity <= 4){
    tone(tonePin, 200000, 200);
  }
  else if (proximity >= 5 && proximity <= 6){
     tone(tonePin,5000, 200);
  }
  else if (proximity >= 7 && proximity <= 8){
    tone(tonePin, 1000, 200);
  }
  shiftOut(dataPin, clockPin, MSBFIRST, possible_patterns[proximity]);
  digitalWrite(latchPin, HIGH);
 
  
  delay(600);
  noTone(tonePin);
}

View raw code

Watch the video demonstration

Thanks for reading, you can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

72 thoughts on “Arduino – Ultrasonic Sensor with LEDs and buzzer”

  1. can I use this same device to measure the water level??

    and what do I need to do if I want to add an LCD screen to this project?

    Reply
    • You can use an Arduino to measure the water level and you can use the same concept for the LED’s to light up according how much water it has on the tank for example, but you need a sensor for that.
      You want to add an LCD screen to my project to do exactly what? represent the distance instead of the LED’s?

      Reply
      • I want to measure the water level inside a tank. I’m thinking about using the ultrasonic sensor with the LCD screen (for arduino) to display how much water do I have inside the tank. I may use the buzzer to notify the user when the water level is low.

        Can you help?!

        Reply
  2. Suppose we are declaring a particular pin (say pin number 10) as OUTPUT. If we doesn’t use it in our program anywhere, then will it give clock signal? Because you have declare a clock pin, but didn’t configure anything in your program, straightaway you used it in shiftOut, thats why I am asking

    Reply
  3. you need to use the clock signal of course… and I’ve used on the shiftOut() function.
    if you look careful there’s where I’ve used
    shiftOut(dataPin, clockPin, MSBFIRST, possible_patterns[proximity]);
    if you want to know more about that particular function and how to use it you can read this post:
    http://arduino.cc/en/Reference/shiftOut

    Reply
  4. Ya…. Ya…… I saw it later and understood. It is sending clock signal during shiftOut(), because at that time only IC which controlling LEDs require signal. Fine. Does water reflect ultrasonic waves? If it doesn’t then your idea given to Muhammad (another one…. not me) will not work, right? I want to check pulseIn() with a push button. Using pulseIn(), I want to know, how long I am pressing the button. I have tried it, but I got only ‘0’ (I have printed it in the serial monitor)

    Reply
  5. Hi Rui!

    I’ve been trying to run the circuit, but it does not work! it give “out of range” all the time, with 3 red LEDs turning on and off!

    any help?

    Reply
  6. Hi.I am having a problem with this project. I am using a PING ultrasonic sensor with arduino uno. I have made the circuit as shown on a general purpose pcb but when i upload and connect the pcb to arduino, all the led’s and the buzzer goes off without any input to the ultrasonic module. The ultrasonicc module is working ok . I have checked it.
    Please help !!!

    Reply
    • Hi,
      Try to check the circuit again very carefully.
      I know that my circuit is correct and code too because a lot of people already tested and messaged me saying it was working.
      your IC is in the right place? sometimes people use it upside down… I think.

      If you’ve made a project on a PCB it might be some soldering missing.
      Check everything again and I’m sure you’ll find why is not working.

      Thanks so much for trying my projects!

      Reply
  7. hello. i am interested to make your project as my mini project for my “electronic sensor” subject. may i get the lab view circuit for this circuit? and could you give me your email contact so that it will be easier for me to ask anything regarding about your project. thank you 🙂

    Reply
    • I’m almost sure that TI has a similar model to replace this SR.
      If you have an Arduino Mega you can easily change my code and use without a SR. But you’re code will look a lot more confusing…
      And you’ll need to use 8 digital pin. Each pin will control a LED.
      With the SR you only need to use 3 Arduino Pins to control 8 LED’s

      Reply
  8. Thanks for the response. Will be doing it on a aquarium as a prototype. I tried to transfer the design to a PCB. Hope it will work. Im also using a local arduino available on our country. 🙂

    [IMG]http://i61.tinypic.com/35com8m.png[/IMG]

    Reply
    • Awesome, I would love to see a picture of the finish project!

      (The picture you posted for some reason is no longer available, so I didn’t see it..)

      Reply
  9. I have bought a 5 pin Ultrasonic sensor. Which is the available in our country. The pins are Vcc, Input, Pulse output, gnd, gnd respectively. Also when i tried to breadboard it. Only the buzzer is working. And when i tried to upload the code. It says Binary sketch size: xxxxx bytes (of a xxxxx byte maximum)

    Reply
  10. Its working now. I just swap the two wires on Trig and Echo of the sensor. Thanks 😀 Will try to put in the PCB to make it permanent.

    Reply
    • Cool, I’m glad you made it work.
      Sorry for taking so long to answer I’ve been really busy in the past days.
      Have a nice day,
      Rui

      Reply
  11. Hi,

    A cool project. I was wondering whether you’ve tried the ultrasonic sensor with rgb led strip lights? I’m a newbie to electronic and currently I am trying to combine rgb led strip with the ultrasonic sensor. However it’s still in the early stage of gathering the materials and I’m also concurs to what resistor I should get?

    Brought a 5050 5M RGB SMD LED Strip Light+Power Supply Adapter
    5 meter (500cm) and 150pcs 5050 RGB SMD LEDs IP67 Waterproof

    Power Supply Specification
    1.Input Voltage: 100-240V 1.5A
    2.Output Voltage: 12V DC
    3.Output Current: 0 – 5A
    4.Output Power: 60W
    5.Safety Compliance: CCC/ FCC / CE / UL / GS
    6.Working Temperature: 0°C-40°
    7.Storage Temperature: -20°C-60°C

    All the best!

    Reply
  12. Ola amigo, poderia disponibilizar o esquema do fritzing deste projeto? Obrigado!
    Hello friend, can you share the scheme in fritzing of this project? Thanks!

    Reply
  13. what about if i using HY-SRF05 ultrasonic module instead using HC-SR04?.. did i must changing the code or some kind?.. please help me, thanks.

    Reply
  14. Hi Rui

    Is there any way that you can add an lcd screen interface to the circuit that you have built? I would like to see through the lcd the distance that I am from the sensor

    Many thanks and keep up the good work!!!

    Reply
  15. Can some explain to me what this part of the code does and what specifically does the byte keyword do?
    byte possible_patterns[9] = {
    B00000000,
    B00000001,
    B00000011,
    B00000111,
    B00001111,
    B00011111,
    B00111111,
    B01111111,
    B11111111,
    };

    Reply
    • Hi.
      That variable contains the possible patterns for the LEDs.
      This means that with B00000000 all the LEDs are off. With B00000001, the rightmost LED is lit, and so on.
      Later on the code, you use that variable to light up a specific number of leds accordingly to the distance to the sensor.
      I hope this helps.
      Regards,
      Sara 🙂

      Reply

Leave a Reply to Mark Cancel reply

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.