Introduction: Arduino HCSR04 Leds Distance Warner

Picture of Arduino HCSR04 Leds Distance Warner

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

Step 1: Part Lis

Picture of 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

Picture of Breadboard Layout

Step 5: Arduino Code

Picture of 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

ledDistancia();

Serial.print(distanciaCM);

Serial.println(" cm");

delay(1000);

}

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

void ledDistancia () {

//Apagando todos os leds

digitalWrite(ledgreen,LOW);

digitalWrite(ledyellow,LOW);

digitalWrite(ledred,LOW);

digitalWrite(buzzer,LOW);

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

if (distanciaCM > 20) {

digitalWrite(ledgreen,HIGH);

digitalWrite(buzzer,LOW);

}

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

digitalWrite(ledyellow,HIGH);

digitalWrite(buzzer,LOW);

}

if (distanciaCM < 10) {

digitalWrite(ledred,HIGH);

digitalWrite(buzzer,HIGH);

}

}

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

http://www.comofazerascoisas.com.br/posts/arq-dld/...

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.

Comments

Surya Krishnakumar (author)2017-06-04

Hellow Friends :D Could you please spare some time and checkout my new instructable ? https://www.instructables.com/id/Simple-LM386-Powe...

Thanks a lot for all your support and appreciations. :D

About This Instructable

2,246views

34favorites

License:

More by Surya Krishnakumar:Arduino HCSR04 leds distance warnerArduino DC Motor Speed Control PotentiometerArduio Dc motor speed control serial monitor ..(no l293d)
Add instructable to: