#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {Serial.begin(9600);
// Initialize the DS18B20 sensor
sensors.begin();
}
void loop() {
// Call sensors.requestTemperatures() to get temperature readings
sensors.requestTemperatures();
// Read temperature in Celsius
int temperatureC = sensors.getTempCByIndex(0);
if (temperatureC != DEVICE_DISCONNECTED_C) {
// Print temperature to serial monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
} else {
Serial.println("Error: Unable to read temperature data");
}
// Wait for a moment before taking the next reading
delay(1000);
}