#include <OneWire.h>
#include <DallasTemperature.h>
const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DQ pin
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library
float tempCelsius; // temperature in Celsius
float tempFahrenheit; // temperature in Fahrenheit
void setup()
{
Serial.begin(9600); // initialize serial
tempSensor.begin(); // initialize the sensor
}
void loop()
{
// send the command to get temperatures
tempSensor.requestTemperatures();
// read temperature in Celsius
tempCelsius = tempSensor.getTempCByIndex(0);
// convert Celsius to Fahrenheit
tempFahrenheit = tempCelsius * 9 / 5 + 32;
Serial.print("Temperature: ");
Serial.print(tempCelsius); // print the temperature in Celsius
Serial.print("°C");
// separator between Celsius and Fahrenheit
Serial.print(" ~ ");
Serial.print(tempFahrenheit); // print the temperature in Fahrenheit
Serial.println("°F");
delay(500);
}