#include <LiquidCrystal.h> // Include the LiquidCrystal library
// Define LCD pin connections
#define LCD_RS 7
#define LCD_E 8
#define LCD_D4 9
#define LCD_D5 10
#define LCD_D6 11
#define LCD_D7 12
// Initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
// Define ultrasonic sensor pins
#define trigPin 2
#define echoPin 3
long duration;
int distance;
void setup() {
// Set the pin modes
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize serial communication
Serial.begin(9600);
// Initialize the LCD
lcd.begin(16, 2); // Set the LCD size to 16 columns and 2 rows
lcd.print("SAMIKSHA IS SMART");
delay(2000); // Display the message for 2 seconds
lcd.clear(); // Clear the LCD for the distance display
}
void loop() {
// Trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pin
duration = pulseIn(echoPin, HIGH);
// Calculate distance
distance = duration * 0.0344 / 2;
// Print distance to the serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Display distance on the LCD
lcd.setCursor(0, 0); // Set cursor to column 0, row 0
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
// Delay before next reading
delay(100);
}