#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
sensors.begin(); // Start communication with the sensor
}
void loop() {
sensors.requestTemperatures(); // Request temperature readings
// Get the temperature in Celsius for sensor with index 0
float temperatureC = sensors.getTempCByIndex(0);
if (temperatureC != DEVICE_DISCONNECTED_C) {
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println("°C");
} else {
Serial.println("Error: Could not read temperature data");
}
delay(1000); // Wait for a second before reading again
}