Arduino HCSR04 Leds Distance Warner

Introduction: Arduino HCSR04 Leds Distance Warner

This instructable shows you how to make an ultransonic sensor based distance warner...

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Part Lis

1) Arduino uno

2) Hcsr04 Ultransonic sound sensor

3) Red led

4) Green led

5) Yellow led or anything of your choice

6) Piezo buzzer

7) 3* 220ohms resistor

8) Breadboard

9) Jumper wires

Step 2: Connect the HCSR04 to the Arduino

1) Connect the 5V of the hcsr04 to 5v on the arduino.

2) Connect the GND of the hcsr04 to GND on the arduino.

3) Connect the trig of the hcsr04 to pin DIGITALPIN7 on the arduino.

4) Connect the echo of the hcsr04 to DIGITALPIN6 on the arduino.

Step 3: Connecting the Three LEDS and the Buzzer

1) Connect all the three leds's and the buzzer's negative terminal to GND.

2) Connect a 220ohm resistor to each of the three leds positive terminal.

3) Connect the red led's resitor's end to DIGITALPIN11.

4) Connect the yellow led's resistor's end to DIGITALPIN12.

5) Connect the green led's resistor's end to DIGITALPIN13.

6) Connect the Buzzer's positive terminal to DIGITALPIN10.

Step 4: Breadboard Layout

Step 5: Arduino Code

#include "Ultrasonic.h"

//criando objeto ultrasonic e definindo as portas digitais do Trigger - 6 - e Echo - 7

Ultrasonic ultrasonic(6,7);

//Declaração das constantes referentes aos pinos digitais.

const int ledgreen = 13;

const int ledyellow = 12;

const int ledred = 11;

const int buzzer = 10;

long microsec = 0;

float distanciaCM = 0;

void setup () {

Serial.begin(9600); //Inicializando o serial monitor

pinMode(ledgreen,OUTPUT); //Definindo pino digital 13 como saída.

pinMode(ledyellow,OUTPUT); //Definindo pino digital 12 como saída.

pinMode(ledred,OUTPUT); //Definindo pino digital 11 como saída.

pinMode(buzzer,OUTPUT); // Define buzzer pin 10.


void loop () {

microsec = ultrasonic.timing(); //Lendo o sensor

distanciaCM = ultrasonic.convert(microsec, Ultrasonic::CM); //Convertendo a distância em CM



Serial.println(" cm");



//Método que centraliza o controle de acendimento dos leds.

void ledDistancia () {

//Apagando todos os leds





//Acendendo o led adequado para a distância lida no sensor

if (distanciaCM > 20) {




if (distanciaCM <=20 and distanciaCM >= 10) {




if (distanciaCM < 10) {





Step 6: Library to Download and NOTES

1) For the programm to run successfully you need the HCSR04 library .you can download it from here

2) The buzzer is not included in the circuit diagram but i added it additionally to the programming also..

3) Upload the code and open the serial monitor to get feedback from the sensor.'

4) You can always change the delay time and the minimum and maximum distance in the void loop.

Step 7: Have Fun and Build Your Own Security or Parking System With This Concept.

Be the First to Share


    • Backyard Contest

      Backyard Contest
    • Silly Hats Speed Challenge

      Silly Hats Speed Challenge
    • Arduino Contest 2020

      Arduino Contest 2020