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
- Arduino UNO – read Best Arduino Starter Kits
- 1x 74HC595 8 Bit Shift Register
- 1x Breadboard
- 8x LEDs (for example: 3x red, 3x yellow, 2x green)
- 9x 220 Ohm Resistors
- 1x Buzzer
- 1x Ultrasonic Sensor (for exemple: HC-SR04)
- Jumper Wires
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
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);
}
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.
AWESOME…. I LIKED WHOLE SITE…. 🙂
Thanks, I’m really glad you like it 🙂
I’ll be posting more tutorials soon
Hi , Can the code be use with different kind of ultrasonic , example : PING(((
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?
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?
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?!
It depends on the ultrasonic sensor and LCD screen you’re using, but it’s pretty easy to do what you need, just need to make a few changes on my code…
Ok
Thanks Rui..
And thanks for the great tutorials ..
Thanks ! 🙂
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
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
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)
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?
leds not glowing properly as u said ly 3 turning on and off , have u resolved it???
what you mean? my circuit is working fine… I never said it wasn’t working :S
good one thanks alot 🙂 keep doing the work…will check the ckt and let me ask some doubt’s if i hav 🙂
Thanks for your input.
Anything you have in mind just ask!
in this circuit i want to display the distance of obstacle using lcd . how to combine ?
buzzer sound is fine but leds not glowing . something wrong in the ckt diagram
Thanks for making my project!
My diagram is fine… because this project worked for more people already…
you’re probably missing just a wire…
your IC is in the correct way?
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 !!!
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!
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 🙂
Feel free to contact me here:
https://randomnerdtutorials.com/contact
I didn’t understand exactly what you meant by labview for my circuit
Is there a TI model of shift register which would work or can this be completed without the SR? If so, how exactly?
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
SRF04 is?
I’m using the HC-SR04 Ultrasonic sensor.
I’ve create a tutorial for that:
https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/
Ultrasonic.
I’m use HY-SRV05 not working?
What are you talking about?
Please use google translator or something… I can’t help you otherwise sorry…
hey how would i change the code if my distance sensor only has 1 signal pin
Hi Sean,
Which sensor are you using?
the ping)))
Will this work to measure the depth of the water on a tank?
Yes Mark.
I think they will work fine to measure the level of the water.
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]
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..)
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)
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.
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
Heres the output that we made for our project. We’ll be using it as a water level indicator. Thanks for the tutorial.
http://i.imgur.com/ByhdmaD.jpg?1
That’s a really impressive work Mark!
Thanks for sharing!
I’m glad you found my work useful.
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!
Hi Tigtog,
You want to do a similar projects as mine but with a RGB LED strip?
so, what the result??i want to see it to because i have a same project like this but using the led rgb strips
Do you have the code for this? I’m doing the same project and I cant seem make it on my own. Can you help?
why my buzzer keep beeping? are there problems on my ultrasonic sensor or otherwise?
Hi zizy,
Seems like your ultrasonic sensor is measuring wrong the distance… the LEDs are working fine?
Ola amigo, poderia disponibilizar o esquema do fritzing deste projeto? Obrigado!
Hello friend, can you share the scheme in fritzing of this project? Thanks!
Esse foi mesmo daqueles projetos em que nao guardei o projeto fritzing… peço desculpa.
mas julgo que seja facil de seguir pelo esquematico.
Abraço,
Rui
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.
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!!!
It runs just fine, thanks !
Awesome!
Please I need your contact details, its very urgent and important please.
You are great man, you make arduino seem so easy. ::taking down my hats
Thank you!
Rui
Hi sir,
I need circuit and code for automatic water level motor wireless switch
Thanks in advance.
You can modify my code to do exactly that.
Thank you,
Rui
Where can I download you the codes you used ?
You can download right from this blog post.
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,
};
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 🙂
I have been download your book.. That’s a nice book. Thank you
You’re welcome Adin. Thanks for reading!
Regards,
Rui
What is the app you use to upload the code to arduino
I use the Arduino IDE. Thanks for asking,
Rui
where is the code??
Hi.
If you’re have trouble finding the code on your browser, you can check the following link.
https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/Ultrasonic_Sensor_with_LEDs_bar_graph_and_buzzer.c
Regards,
Sara.