#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal_I2C library
const int lm35_pin = A1; // Declare a constant integer variable lm35_pin and assign it to pin A1
LiquidCrystal_I2C lcd(0x27, 16, 2); // Define the LCD object with I2C address 0x27, 16 columns, and 2 rows
void setup()
{
Serial.begin(9600); // Initialize serial communication with a baud rate of 9600
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0); // Set the cursor to the first row
lcd.print("Temperature:"); // Print "Temperature:" on the LCD
}
void loop()
{
int temp_adc_val; // Declare an integer variable temp_adc_val to store the ADC value of temperature
float temp_val; // Declare a float variable temp_val to store the temperature value in Celsius
temp_adc_val = analogRead(lm35_pin); // Read the analog value from LM35 temperature sensor and store it in temp_adc_val
temp_val = (temp_adc_val * 4.88); // Convert the ADC value to equivalent voltage (in millivolts)
temp_val = (temp_val/10); // Convert the voltage to temperature value in Celsius (LM35 gives output of 10mV/°C)
Serial.print("Temperature = "); // Print "Temperature = " on the serial monitor
Serial.print(temp_val); // Print the temperature value in Celsius on the serial monitor
Serial.print(" Degree Celsius\n"); // Print " Degree Celsius" on the serial monitor
// Print temperature on the LCD
lcd.setCursor(0, 1); // Set the cursor to the second row
lcd.print(" "); // Clear the second row on the LCD
lcd.setCursor(0, 1); // Set the cursor to the second row
lcd.print(temp_val, 2); // Print the temperature value with 2 decimal places on the LCD
lcd.print((char)223); // Print the degree symbol (ASCII value) on the LCD
lcd.print(" Celcius"); // Print " Celcius" on the LCD
delay(100); // Delay for 1 second
}