Arduino Distance Detector With a Buzzer and LED's

337,368

180

41

Introduction: 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

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

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

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

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

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

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

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. 

Arduino Contest

Participated in the
Arduino Contest

21 People Made This Project!

Recommendations

  • Puzzles Speed Challenge

    Puzzles Speed Challenge
  • "Can't Touch This" Family Contest

    "Can't Touch This" Family Contest
  • CNC Contest 2020

    CNC Contest 2020

41 Discussions

0
pantocratos2008
pantocratos2008

4 weeks ago

Hello, can anyone help me with coding?
I am willing to pay

0
lucasmacdonald77
lucasmacdonald77

Question 9 months ago on Step 7

Hi, how would you implement a dc vibrating motor into this circuit, in terms of code and placement onto the breadboard. The picture attatched is my motor.

image0.jpg
0
jasontnguyen
jasontnguyen

Question 1 year ago

Hello,
Is there a way to do wireless distant sensor ?

Thank you

0
nayanems
nayanems

3 years ago

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

0
asad.zahid840
asad.zahid840

Reply 1 year ago

You are putting trig and echo wire on wrong side.

0
markcutey
markcutey

Reply 2 years ago

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.

0
LumB1
LumB1

Reply 3 years ago

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.

0
polkaa
polkaa

Reply 3 years ago

how did u solve the problem?

0
siderio2
siderio2

Reply 3 years ago

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)

0
SimonK83
SimonK83

Reply 3 years ago

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.

0
subramanyab3
subramanyab3

3 years ago

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

Can we increase?

0
asad.zahid840
asad.zahid840

Reply 1 year ago

Use 9 volt buzzer without resistor.

0
CameronB73
CameronB73

Question 2 years ago

Thanks for posting. How would I go about changing the code so lets say the red LEDs came on for only a specific distance and then the yellow and then the green. For example

Red on for distance from 50m to 40 then off

Yellow from 39 to 30 then off

Green from 30 to 0 then off

0
asad.zahid840
asad.zahid840

Answer 1 year ago

You have to change the " if " conditions.
For example:
if(distance<50){
//red light
}
if(distance>30&&distance<40){
//yellow light
}

0
acb1278
acb1278

Question 1 year ago

is it possible to add a toggle switch? A simple two pronged switch?

0
avishkars1
avishkars1

1 year ago

Thanks @flynn_munroe for the tutorial! Was great fun to make

IMG_20180818_160620.jpgIMG_20180818_160111.jpgIMG_20180818_160034.jpgIMG_20180818_160014.jpgIMG_20180818_160620.jpgIMG_20180818_160423.jpgIMG_20180818_160403.jpgIMG_20180818_160330.jpgIMG_20180818_160134.jpg
0
885224384
885224384

Question 2 years ago on Introduction

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

What does this part of the code do? I didn't really understand. Sorry i'm new to coding

0
innovationisoxygen
innovationisoxygen

Answer 2 years ago

He declared two variables- duration and distance and then he turned trigPin to off or 0V.

0
EileenK17
EileenK17

2 years ago

what a bro man this worked perfectly code is fantastic 10/10 would use again