The DS18B20 is a three-pin digital thermometer that provides digital temperature measurements as 9 to 12-bit packets over a single wire serial connection. Its temperature range is between -55 and 125 degrees Celcius, with excellent accuracy of 0.5 degrees between -10 and 85 degrees Celcius.
It can convert a temperature to a 12 bit reading in 750ms.
You can download the device datasheet from here.
Required libraries to use the DS18B20
To use the temperature sensor, one must include the Arduino Library for Maxim Temperature Integrated Circuits developed by Miles Burton. Constructing a DallasTemperature instance requires a reference to a OneWire object. OneWire is another library designed by Miles Burton that provides one wire or MicroLan capabilities. Constructing a OneWire object requires the Arduino pin to the single-wire device.
The program to read the temperature will therefore look as follows:
#include <OneWire.h>
#include <DallasTemperature.h>
// use pin 10 to read temperature
OneWire oneWire(10);
DallasTemperature dallasTemperature(&oneWire);
void setup()
{
Serial.begin(9600);
sensor.begin();
}
void loop()
{
dallasTemperature.requestTemperatures();
// as we only have one device, the index of the thermometer
//is 0
float temp = dallasTemperature.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println (temp);
delay(800);
}
The setup is as follows: