Things you’ll need:
-LED
-1K ohm resistor
-potentiometer
-LCD screen
-Arduino UNO
-Breadboard
-Cables
-Ultrasonic distance sensor
Schematic:
Code:
#include <LiquidCrystal.h>
const int trigPin = 7;
const int echoPin =8;
const int led =6;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
lcd.begin(16, 2);
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10){
digitalWrite(led,HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Too close");
}
else {
digitalWrite(led,LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(String(distance)+ String(" centimeters"));
float disIn= distance * 0.3937;
lcd.setCursor(0, 1);
lcd.print(String(disIn)+ String(" inches"));
}
delay(500);
}
Video:
Commenti