#include <OneWire.h>
#include <LiquidCrystal.h>
// Data wire for the DS18B20 temperature sensor
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with DS18B20
OneWire oneWire(ONE_WIRE_BUS);
// LCD module connections (modify these for your specific module)
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
// Initialize the LCD
lcd.begin(16, 2);
// Search for the address of the DS18B20
if (!oneWire.search()) {
lcd.print("No sensor found");
while (1); // Stop here if no sensor is found
}
// Reset the search to prepare for the next search
oneWire.reset_search();
}
void loop() {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
// Search for the next DS18B20 sensor
if (!oneWire.search()) {
// No more sensors found, reset search
oneWire.reset_search();
delay(250);
return;
}
// Check if the found sensor is a DS18B20
if (oneWire.reset() == 0) {
if (oneWire.read(0) == 0x10) {
type_s = 1;
} else {
type_s = 0;
}
oneWire.reset();
oneWire.select(addr);
oneWire.write(0x44, 1); // Start temperature conversion
delay(800); // Wait for conversion to complete
present = oneWire.reset();
oneWire.select(addr);
oneWire.write(0xBE); // Read Scratchpad
// Read data from DS18B20
for (i = 0; i < 9; i++) {
data[i] = oneWire.read();
}
int16_t raw = (data[1] << 8) | data[0];
int16_t temperature = raw / 16.0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
}
delay(1000); // Update temperature every second
}