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