Introduction: Arduino - Ultrasonic Sensor With LED's and Buzzer

Picture of Arduino - Ultrasonic Sensor With LED's 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.

Check my website for more electronic projects and information.
Check this video to watch the circuit working
Thanks!



Parts Required:
1x Arduino
1x 74HC595 8 Bit Shift Register
1x Breadboard
8x LED’s (for example: 3x red, 3x yellow, 2x green)
9x 220 Ohm Resisters
1x Buzzer
1x Ultrasonic Sensor (for exemple: HC-SR04)
Jumper Wires

Step 1: Let's Start

Picture of Let's Start

First you should place all the components on the breadboard, check the picture.

Step 2: Wires...

Picture of Wires...

Wire connection: 74HC595 8 Bit Shift Register
Ground (black wire) – Pin 8 and Pin 13
5V (red wire) – Pin 10 and Pin 16
IC Pin 11 (white wire) - Arduino Pin 11
IC Pin 12 (blue wire) – Arduino Pin 12
IC Pin 14 (gree wire)– Arduino Pin 13
1st LED (orange wire) – IC Pin 15
2nd LED (orange wire) to 8th LED – IC Pin 1 to IC Pin 8
Note this connections to the LED’s must be to their positive lead (this means the longer one)


Wire connection: LED’s
Now to the negative lead use one resistor per led and connect them to the ground (black wires)

Step 3: More Wires...

Picture of More Wires...

Wire connection: Ultrasonic Sensor: HC-SR04
VCC (red wire) – 5V
Trig  (violet wire) – Arduino Pin 9
Echo (yellow wire) – Arduino Pin 10
Ground (black wire) –GND

Wire connection: Buzzer
Shorter lead (black wire) – ground
Longer lead (resistor in series, red wire) – Arduino Pin 4

Step 4: Programing

Picture of Programing

Now upload this code:
/*
* IRremote Library - Copyright 2009 Ken Shirriff
* created by Rui Santos, http://randomnerdtutorials.wordpress.com
*  Ultrasonic sensor with LED's and Buzzer
* 2013
*/
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);
}

Step 5: That's the Final Product.

Picture of That's the Final Product.

I hope that you’ve enjoyed.
Visit my website for more tutorials!
Thanks for reading

Comments

kgb10 (author)2016-07-01

Super...super fine - thumbs up !

But I have a questions...is it possible to get the device to start Automatic and turn off when the car is parked.

Because I want to put the device on the carport wall - and when I get home it will start and after parkering the car it will - Automatic turn off (approximately after 30 sec.)

Is it possible and how should it be encodet.

rubencoelho made it! (author)2016-06-16

It's very col and a bit easy to do thanks a lot.

Be carefull how you use de ultrasonic sensor. I uploaded an image to show how to use it

KennethR12 (author)2016-04-07

Could this project be modified to serve as a detector of high frequency noises? There are so called "teen buzz" sounds that only kids and teens can hear but adults cannot. I just want it to light up an LED or make an audible beep or both when it detects sound in the 14.4k range. Teens use them as secret ringtones and it would be cool to bust them.

Jedimaster0 (author)2016-01-21

Just made my own version, it's a little more simplistic in design.
https://www.instructables.com/id/Distance-Detector/

Fatemeh Mohammadi (author)2015-11-26

Hi do you know about signal in dangrees

road bend,project?

Robin_ozil (author)2015-11-01

all leds are lighting up frm the beginning itself irrespective of the distance ...plz help me

chengl2 (author)2015-10-27

everthing is well...but why my buzzer keep emitting the same sound? anyone can help?:( quite rush of time.... what type of buzzer u all using?

Vijendran made it! (author)2015-10-08

Perfect...

WinalP (author)2015-06-18

i have done this project ,but in schematic some wire in leds and HC-SR04 wrong but out of all this is coll project ,thanks

tomrod16 (author)2015-04-20

How can I add a mute button?

EdizonBlu (author)2015-03-31

a

Hi guys, I would like to ask on how to stabilize my ultrasonic sensor mounted in a servo motor. It is because everytime an "Out of Range" displays in the serial monitor it gives current to the servo motor causing it to sweep. The possibility of sweeping the servo left or right only is when there is an object being detected. BTW, the ultrasonic sensor is mounted above the servo mtor.
Here is my code://
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
#include <Servo.h>
int pos = 0; // variable to store the servo position
int defPOS = 0;
Servo myservo;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
myservo.attach(9);
myservo.write(0);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
//0 Left
//1 Center
//2 Right
int buttonState1 = digitalRead(led); //red
int buttonState2 = digitalRead(led2); //green
if( (buttonState1 == 0 && buttonState2 == 1) && (defPOS != 1))
{
delay(2000);
if(defPOS == 2)
{
for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2); // waits 15ms for the servo to reach the position
}
}
else if(defPOS == 0)
{
for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2); // waits 15ms for the servo to reach the position
}
}
defPOS = 1;
}
if (distance < 30) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
if(defPOS == 0)
{
for(pos = 90; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2); // waits 15ms for the servo to reach the position
}
defPOS = 2;
}
else if(defPOS == 2)
{
for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2); // waits 15ms for the servo to reach the position
}
defPOS = 1;
}
else if(defPOS == 1)
{
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2); // waits 15ms for the servo to reach the position
}
defPOS = 0;
}
//myservo.write(180);
// delay(1000);
//myservo.write(0);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 400 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}

mohammedkhan131 (author)2015-01-09

HERE YOU GO

Nur LiyanaM (author)2014-12-01

Hi there,

I would like to ask if these circuit can be applied for detecting water level. Was it only able to detect concrete object? How about object like water? :)

shivamundisputed (author)2013-08-31

hey in your schematic diagram position of echo and trig are reversed and the buzzer is not connected to d ground

it's correct my schematics but the image is a bit large... (the yellow rectangle with Vcc trig echo ground)
so it's seems it's wrong... but it's working perfectly because I've received tons of emails of people that made this project and it worked!
If you have more questions just let me know

AmarA2 (author)RuiSantos2014-11-24

Amazing work Rui, I think he is trying to say that on your breadboard when you are connecting the negative side of the buzzer to ground, technically its not because there is a break in the breadboard.

psycho.maggot (author)2014-11-21

what about if without the bit shifter?.. does work or not?

janae.hickmon (author)2014-11-17

Hi. I'm having a problem with B00000000 - B11111111 being declared. The verification process is not accepting this array for some reason. Do you have any ideas as to why?

HEISENBERG_PN made it! (author)2014-08-12

Perfect...
More easy i'm dead! Plug & Play!
Great explains and presentation.

(FR)

cobler (author)2014-02-07

the buzzer used is a 5v buzzer?or a 9v one?

cobler (author)2014-02-07

WHAT is the use of shift register here?

bqalawi (author)2013-09-09

Can you show how to connect multiple hc-sr04
in arduino with same trigger pin by using 74hc595??

tolomeo (author)2013-08-20

Could you please add how to make it independent, by using a 9v battery. I'd like to built it and install in my garage.

RuiSantos (author)tolomeo2013-09-02

I think I've answered to you through facebook, right?
if you have more questions please leave a comment on my website!
thanks so much for checking out my projects!
(http://randomnerdtutorials.com)

edgers (author)2013-07-20

My buzzer is rated 3VDC but i hear the buzzer very soft although it's working. Anything wrong with my buzzer? If i connect directly to a 3V supply the buzzer is loud.

martzsam (author)2013-06-20

Hmm... I put mine all together but all it does is light up all the LEDs and beep the same tone again and again.

RuiSantos (author)martzsam2013-06-25

hum... try to see the serial monitor and see if the distance sensor is measuring the distance properly. It can be that problem

martzsam (author)RuiSantos2013-06-25

I got it working. I had the trig and echo wires reversed.

RuiSantos (author)martzsam2013-06-27

nice I'm glad you make it to work!

frick1 (author)2013-06-23

sir , can you share a program of this project? :)

RuiSantos (author)frick12013-06-25

what you mean ?
If you want the source code it's on my insctructable, feel free to use it ! :)

crazy_thiago (author)2013-06-06

Nice project,

RuiSantos (author)crazy_thiago2013-06-06

Thanks! I'm glad you liked it

hackery21 (author)2013-03-19

What if you don't want the LEDs? and want to use a vibrating motor instead of a buzzer? Just gimme some code lolz

RuiSantos (author)2013-01-23

Thanks man, I'm really glad you like, more projects soon!

rimar2000 (author)2013-01-23

Good work, congratulatios.

About This Instructable

81,154views

132favorites

License:

Bio: Author, Blogger, Electronics Enthusiast and Entrepreneur. For complete Electronics Projects and Tutorials go to: http://randomnerdtutorials.com/start-here
More by RuiSantos:ESP8266 Web Server (Without Arduino)Arduino Simple Memory GameArduino – Datalogger with Temperature Sensor and Photoresistor
Add instructable to: