#include <OneWire.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 2 // Replace with the actual pin connected to the DS18B20
OneWire oneWire(ONE_WIRE_BUS);
LiquidCrystal_I2C lcd(0x27, 20, 4); // Replace with the actual LCD address if different
void setup() {
lcd.begin(20, 4); // Initialize the LCD
lcd.print("Temperature:"); // Display a message on the LCD
}
void loop() {
float temperature = getTemperature();
lcd.setCursor(0, 1); // Set the cursor to the second line of the LCD
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
delay(1000); // Delay for 1 second before updating the temperature
}
float getTemperature() {
byte data[9];
float temp;
if (oneWire.reset()) {
oneWire.write(0xCC); // Skip ROM command
oneWire.write(0x44); // Start temperature conversion
delay(750); // Wait for temperature conversion to complete
oneWire.reset();
oneWire.write(0xCC); // Skip ROM command
oneWire.write(0xBE); // Read scratchpad command
for (int i = 0; i < 9; i++) {
data[i] = oneWire.read(); // Read 9 bytes of data from the DS18B20
}
temp = ((data[1] << 8) | data[0]) * 0.0625; // Calculate the temperature
}
return temp;
}