Distance Meter With Arduino
by simaopintocorreia in Circuits > Tools
775 Views, 3 Favorites, 0 Comments
Distance Meter With Arduino
Hello guys !Ready for a new project?Good,let's make it...
Today I gonna show you how to make a distance meter like that ones that are sold on hardware stores or construction stores ,I gonna use a ultra sonic sensor in my case Vellman VMA306 or other marks:HC-SR05 ,infortunly you can't find many thing on internet about this ultra sonic sensor because the most popular is the HC-SR04,I use a LCD screen 16*2 for display the distance.Let's make.
Supplies
- Arduino UNO R3 (you can use NANO or MEGA).
- jumpers.
- Breadborde.
- LCD 16*2.
- HC-SR05 ultra-sonic sensor.
- 10K variable resistor.
Schematic:
Note : the IC is not a IC is the ultra sonic sensor .
Code:
#include<LiquidCrystal.h>
LiquidCrystal lcd(2, 4, 7, 8, 9, 10);
// definiçao de constantes para pinos #define TRIG_PIN 13 #define ECHO_PIN 12
#define RED 6 #define GREEN 5 #define BLUE 3
// definiçao de constantes para facilitar a manutençao das distancias #define BLUE_LONG 60 #define BLUE_SHORT 30 #define GREEN_LONG 45 #define GREEN_SHORT 15 #define RED_LONG 30 #define RED_SHORT 0 void setup() { // inicia o display LCD lcd.begin(16,2); // seta o modo dos pinos do LED pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); // seta o modo dos pinos do HC-SR05 pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT);
// apaga o LED (anodo comum, apaga com HIGH) digitalWrite(RED, HIGH); digitalWrite(GREEN, HIGH); digitalWrite(BLUE, HIGH); } void loop() { // declaracao de variaveis long duration, distance; // le a distancia e converte para cm digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); duration = pulseIn(ECHO_PIN, HIGH); distance = (duration/2) / 29.1; // de acordo com a distancia, mapeia a cor do led usando pwm // azul: de 60cm a 30cm // verde: de 45cm a 15cm // vermelho: de 30cm a 0cm if (distance > RED_LONG) digitalWrite(RED, HIGH); else analogWrite(RED, map(distance, RED_LONG, RED_SHORT, 255, 0)); if ((distance > GREEN_LONG) || (distance < GREEN_SHORT)) digitalWrite(GREEN, HIGH); else analogWrite(GREEN, map(distance, GREEN_LONG, GREEN_SHORT, 255, 0)); if ((distance > BLUE_LONG) || (distance < BLUE_SHORT)) digitalWrite(BLUE, HIGH); else analogWrite(BLUE, map(distance, BLUE_LONG, BLUE_SHORT, 255, 0)); // escreve a distancia no display lcd.clear(); lcd.setCursor(0,0); lcd.print("simaopintocorreia"); lcd.setCursor(0,1); lcd.print("Distancia: "); if (distance > 450) lcd.print("> 50"); else lcd.print(distance); lcd.print(" cm"); // aguarda para poder ler o sensor novamente delay(400); }
Obs:
This distance meter can mesure distances between 0cm and 450cm.
This can be used every where.
I expect you enjoy this project .If you have trouble on this project ,find new ways to use the sensor live in the coments new ideas for projects, photos from your project.
See you soon .
simaopintocorreia .