#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin where the DS18B20 is connected
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to DallasTemperature
DallasTemperature sensors(&oneWire);
// Create an LCD object with the I2C address 0x27 (common for many I2C LCDs)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address if needed
void setup() {
// Start the serial communication for debugging
Serial.begin(9600);
// Start the temperature sensor
sensors.begin();
// Initialize the LCD
lcd.begin(16, 2);
lcd.backlight(); // Turn on the backlight
}
void loop() {
// Request temperature readings
sensors.requestTemperatures();
// Get the temperature in Celsius
float temperatureC = sensors.getTempCByIndex(0);
// Print temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
// Clear the LCD and display the temperature
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to first row
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print(" C");
// Wait for a second before the next reading
delay(1000);
}