Introduction: Arduino Distance Detector With a Buzzer and LED's

Picture of Arduino Distance Detector With a Buzzer and LED's

This is a simple guide on how to make a distance detector using an Arduino, a HC-SRO4 Ultrasonic Sensor, a Buzzer, and some LED's.  The ultimate goal of this tutorial is to use the buzzer and LED's to display how far the object is from the ultrasonic sensor.

Step 1: Materials Needed

Picture of Materials Needed

The Materials needed for this project are:

(1x) Arduino Uno
(1x) Breadboard
(1x) HC-SRO4 Ultrasonic Sensor
(1x) Buzzer
(2x) Green LEDs
(2x) Yellow LEDs
(2x) Red LEDs
(7x) 330 ohm Resistors
A lot of jumper wires

Step 2: Setup

Picture of Setup

The photo above shows the setup of the project.  The jumper wires should be connected as follows:

Connect a jumper wire from the 5 volt pin on the Arduino to the bottom channel of the breadboard
Connect another jumper wire from a ground pin on the arduino to the upper channel of the breadboard

Buzzer -> pin 3

(On Ultrasonic Sensor)
Echo -> pin 6
Trig -> pin 7

(In Order from Right to Left)
LED1 -> pin 8
LED2 -> pin 9
LED3 -> pin 10
LED4 -> pin 11
LED5 -> pin 12
LED6 -> pin 13

The jumper wires connected to the LEDs should be connected to the lead on the right, while the left lead of the LED should connected to the ground channel via a 330 ohm resistor.  

Step 3: Assembly: Breadboard

Picture of Assembly: Breadboard

First things first, let's connect the 5 volt and ground pin to the breadboard.  Again, the wire attached to the 5 volt pin should be connected to the bottom channel of the breadboard, while the wire attached to the ground pin should be connected to the upper channel of the breadboard.

Step 4: Assembly: Ultrasonic Sensor

Picture of Assembly: Ultrasonic Sensor

Now it's time to attach the HC-SRO4 ultrasonic sensor.  It is easiest to place the ultrasonic sensor as far right to the breadboard as possible.  Referring back to the setup picture, you should connect the ground pin on the ultrasonic sensor to the ground channel on the breadboard.  Next connect the Echo pin on the sensor to pin 6 on the Arduino.  Now connect the Trig pin on the sensor to pin 7 on the Arduino, and lastly connect the VCC pin on the sensor to the 5 volt channel on the breadboard.  If you did that all correctly, your assembly should look like the picture above.

Step 5: Assembly: LEDs

Picture of Assembly: LEDs

Next is connecting the LED's to the breadboard and Arduino.  Once again referring back to the setup picture, attaching the LEDs is pretty basic, just with a lot of repetition.  The way to connect them is to connect the anode, or the longer leg, or the one on the right, to a pin on the Arduino with a jumper wire, and to connect the cathode, or the shorter leg, or the one on the left, to the ground channel on the breadboard using a 330 ohm resistor.  Then just repeat that step for all six of the LEDs, with the red LED all the way on the right being connected to pin 8 on the Arduino, the anode of the red LED to the left of that one being connected to pin 9 on the Arduino, and so on.  The last LED, that being the green LED all the way on the left, should have it's anode, or right leg, connected to pin 13 on the Arduino.  Once you have done that, your setup should look something like this.  

*DISCLAIMER*
Resistors are not absolutely necessary for the build, however the are highly recommended to be used.  The only reason I am not using them is because I don't have enough resistors.   

Step 6: Assembly: Buzzer

Picture of Assembly: Buzzer

The last part of the physical setup for this build is attaching the buzzer to the breadboard and the Arduino.  This part is probably the easiest part of the setup.  All you have to do is attach the longer leg of the buzzer to pin 3 of the Arduino and attach the shorter leg of the buzzer to the ground channel of the breadboard.  

*DISCLAIMER*
It is HIGHLY recommended to use a resistor in connecting the shorter leg of the buzzer to the ground channel of the breadboard.  This greatly reduces the volume of the buzzer.  You don't have to use a resistor, but if you don't, the buzzer will be very loud and quite frankly annoying.  

Step 7: The Code

Picture of The Code

Now that you have finished the physical setup of the build, now its time for the code.  I assume that you already have the Arduino program on your computer, so now all you have to do is copy and paste the code from below.  

#define trigPin 7
#define echoPin 6
#define led 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8
#define buzzer 3

int sound = 250;


void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(buzzer, OUTPUT);
 
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 

  if (distance <= 30) {
    digitalWrite(led, HIGH);
    sound = 250;
}
  else {
    digitalWrite(led,LOW);
  }
  if (distance < 25) {
      digitalWrite(led2, HIGH);
      sound = 260;
}
  else {
      digitalWrite(led2, LOW);
  }
  if (distance < 20) {
      digitalWrite(led3, HIGH);
      sound = 270;

  else {
    digitalWrite(led3, LOW);
  }
  if (distance < 15) {
    digitalWrite(led4, HIGH);
    sound = 280;
}
  else {
    digitalWrite(led4,LOW);
  }
  if (distance < 10) {
    digitalWrite(led5, HIGH);
    sound = 290;
}
  else {
    digitalWrite(led5,LOW);
  }
  if (distance < 5) {
    digitalWrite(led6, HIGH);
    sound = 300;
}
  else {
    digitalWrite(led6,LOW);
  }
 
  if (distance > 30 || distance <= 0){
    Serial.println("Out of range");
    noTone(buzzer);
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    tone(buzzer, sound);
   
  }
  delay(500);
}

Once you've done that, and you've plugged in your Arduino to your computer, run the code and you're finished.  If you've followed all the directions, the closer you're hand gets to the HC-SRO4, the LEDs should progressively light up until and the closer your hand gets, the buzzer will produce a higher tone each time.  If you have any questions, feel free to email me at datruckk@gmail.com. 

Comments

unoforloop (author)2017-11-17

To solve the continuous LED's being on I switched the sensor Pins 6, 7 (Tigger & Echo) lines. The sensor didn't respond until those two lines were switched. Probably due to the Fritzing picture showing the sensor facing up?

nayanems (author)2016-09-06

Not getting output LEDs are continuosly ON it is not detecting the distance

markcutey (author)nayanems2017-11-05

you need to connect both parts of bread-board with wire. It's basic of bread-board. It's divided in two parts as in structure. Interconnect them and you'll be fine.

LumB1 (author)nayanems2016-09-09

Me too. It is failing to detect the distance. All the LEDs are continously ON, and the serial monitor says out of range all the time.

polkaa (author)LumB12017-04-04

how did u solve the problem?

siderio2 (author)nayanems2016-09-21

The same is happening to me. It's not working, all leds on, no sound and 'out of range' on serial monitor.

Any clue on whats going on?? I've just realized that falilures are from last few weeks. Maybe some change on the firmware, or the compilator or something like that??? (Sorry, just newbie in this world)

SimonK83 (author)siderio22016-11-13

We have the same bug here. Despite the clear instructions, all we get are all led lamps on, no sound, and nothing else happens. We tried several times. We are beginners, and only managed some blink and melody sketches. But this one does not work unfortunately. We don't know now how to proceed. Any hint or advice would be welcome.

MarinikiP made it! (author)2017-06-26

Works well! Thanks for this instructable!

John LimB (author)MarinikiP2017-10-07

Hi can I know how do you solve the LED keep light ON?
Thanks

subramanyab3 (author)2017-05-20

Nice one.. its working perfect... but the buzzer sound is very low.

Can we increase?

vrushalikokate05 (author)2017-04-21

Thank you so much for this easy project

JoelL45 made it! (author)2017-03-23

Works well. Thanks!

cladeport made it! (author)2017-01-15

I added some lines of code to use a 10 LED bar, also I changed the range to 2m. @flynn_munroe I assume this line: /* distance = (duration/2) / 29.1; */ is for setting the measure to centimeters? I am surprised with the range and "precision" of this cheap module.

thanks for the great instructions!

Thalaivar (author)2017-01-07

can i get the code for replacing seial moniter to 16*2 lcd display

Thalaivar (author)Thalaivar2017-01-07

The present code is working very well to me

gpiffaretti (author)2017-01-02

Be careful, check the breadboard/protoboard you have. If the horizontal line where you connect the ground pin and the corresponding one where you connect voltage are divided in the middle, then you re having a different breadboard than the one this guy has. Make sure to connect everything on the same section

PranavJ23 (author)2016-12-31

i did it the code was a little incorrect and made a little corrections

PranavJ23 (author)2016-12-31

the code is not compiling

Mushrif Ahamed (author)2016-10-08

can i fix this without resistors

Zoe Bell (author)2016-08-22

My first "instructable", worked perfectly. Very easy to follow directions.

ysibayama made it! (author)2015-11-06

Thanks brah!

reeverblu (author)ysibayama2016-08-18

how can i put lcd on it?

icuboid made it! (author)2015-05-11

Works just as promised

Link for Video

reeverblu (author)icuboid2016-08-18

is it possible to have it with lcd?

dan321 (author)2016-07-16

Where can I get a piezoelectric, buzzer or speaker capable of producing 80kHz?

FelicianoTech (author)2016-05-23

Hi just wondering but I'm in need of a circuit that used an ultrasonic sensor but turns on LEDs when something is NOT in front of it and turns off when something IS in front of it how would I do this btw all the LEDs need to turn on at once

You can achieve that by simply changing the low/high values of LEDs

Furkan CenkK made it! (author)2016-06-16

Works great but the loop ends when the closest distance in the code triggered. I don't understand why it happens. It freezes with all LEDs on but without sound. And also I didn't use resistors, too.

StefMeister11 (author)2016-06-12

Hi, can you tell me the steps that i have to do to connect a LCD to display the measured distance?? Where do i need to modify the code ?

jpmannrich made it! (author)2015-12-25

Its great! Tanks! It works very well! I din't use the resistors and buzzer.

https://youtu.be/uH0Fi2rMEyE

crichardson14 (author)2015-12-09

just the sensor is capeable of 450 cm

crichardson14 (author)2015-12-09

is there a way to increase the range?

psycho.maggot (author)2015-11-18

Thanks mate this is really works.

psycho.maggot (author)2015-11-18

Thanks mate this is really works.

SaickerP made it! (author)2015-10-11

thank you very much!!!!!!!!!!!!1 it´s great and fantastic!!!!!!!!!!! =)

Harshvardhan Singh made it! (author)2015-09-18

Really interesting project. Worked exactly as promised! Thanks. :)

Skiept made it! (author)2015-08-27

Easy and useful project :)

lcarthew made it! (author)2015-07-10

This was my first project from this site so thank you very much!

eliakallas made it! (author)2015-04-20

worked perfectly , just what i wanted thanks a lot!

dark blue dog (author)2014-11-01

Hi, I am going to build a robot with four of these distance sensors.I like the simplicity of the code compared to others, but there is one flaw. I have already modified the void setup for my sketch with no problems, but I was wondering about the "if" statements. Is there any way to differenciate between the sensors, so that I can tell which sensor I am getting a reading from? I have included the following piece of your code to show you what I mean:


if (distance <= 30) {
digitalWrite(led, HIGH);
sound = 250;

I want to know which sensor I am getting this information from.

Other than that, I really like this piece of code and if I can get this cleared up, I will definitely use it.

(Sorry for the bold lettering, I accidentally highlighted it and made it bold and don't know how to get it off.)

Thanks

Sorry for the late response. The distance variable (along with the duration variable) are all based on the pulseIn() command, that reads data from echoPin. That echoPin is connected to the sensor. In your setup, you are going to need multiple trigPins, pulsePins, durations, distances, etc. Essentially you will need an entire copy of the loop routine for every sensor you have.

psycho.maggot made it! (author)2014-11-22

its works like charm man, thanks. but if want to measure from far distance, don't forget to change the code.

SASOUN (author)2014-03-08

Could you please tell me how to modify this circuit and code for a 10 led bar graph display, while using a Parallax PING))) sensor and buzzer. Thank you.

About This Instructable

256,863views

147favorites

License:

More by flynn_munroe:Arduino Distance Detector with a Buzzer and LED's
Add instructable to: