#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27
// for a 16 chars and 2 line display
int time = 0;
int distance = 0;
int echo =11; //pin number 11; capture returned signal
int trigger = 12; //pin number 12; send signal
void setup()
{
pinMode(echo, INPUT);
pinMode(trigger, OUTPUT);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight(); //background light ON
Serial.begin(9600); //starts send and receive signal
lcd.begin(16, 2);//begin LCD for a 16 chars and 2 line display
lcd.setCursor(0,0); //set cursor initially
lcd.print("Hello, Peter");
delay(2000);
}
void loop()
{
lcd.clear(); // initialize the lcd; clear the LCD screen
digitalWrite(trigger , LOW);//reset
digitalWrite(trigger , HIGH);//set
//pulseIn() to get the time taken by sound wave to reflect back after hitting the object
time = pulseIn(echo,HIGH);
distance = 0.0172*time;//formulae to covert the signal to distance measured
lcd.setCursor(5,0); //5 is column pixel value
lcd.print(distance);
lcd.print("cm");
delay(1000);
}
//OUTPUT- Click on sensor and change distance value to see output on LCD screen