Arduino HCSR04 Leds Distance Warner
by Surya Krishnakumar in Circuits > Arduino
4070 Views, 35 Favorites, 0 Comments
Arduino HCSR04 Leds Distance Warner
This instructable shows you how to make an ultransonic sensor based distance warner...
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
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.
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.
Breadboard Layout
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);
}
}
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.