#include <LiquidCrystal_I2C.h>
#define TRIG_PIN 4 // The Arduino Nano pin connected to TRIG pin of ultrasonic sensor
#define ECHO_PIN 3 // The Arduino Nano pin connected to ECHO pin of ultrasonic sensor
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows
float duration_us, distance_cm;
void setup() {
lcd.init(); // Initialize the LCD I2C display
lcd.backlight(); // open the backlight
pinMode(TRIG_PIN, OUTPUT); // config trigger pin to output mode
pinMode(ECHO_PIN, INPUT); // config echo pin to input mode
}
void loop() {
// Produce a 10-microsecond pulse to the TRIG pin.
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the pulse duration from the ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Distance: ");
lcd.print(distance_cm);
delay(500);
}