#include <LiquidCrystal.h>
int ECHO_PIN = 6;
int TRIG_PIN = 5;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup() {
lcd.begin(16, 2);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop() {
float distance = readDistanceCM();
lcd.setCursor(1, 0);
lcd.print(distance);
lcd.print(" cm");
delay(1000);
}