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.
Remove these adsRemove these ads by Signing Up

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.
icuboid made it!1 month ago

Works just as promised

Link for Video

eliakallas made it!2 months ago
worked perfectly , just what i wanted thanks a lot!
dark blue dog8 months ago

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.)


flynn_munroe (author)  dark blue dog6 months ago

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!7 months ago

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

SASOUN1 year ago

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.