Interfacing DS18B20 1-Wire Digital Temperature Sensor with Arduino

One of the simplest and cheapest ways to incorporate temperature sensing into your Arduino project is to use a DS18B20 1-Wire Temperature Sensor. These sensors are fairly precise and require no external components to function. So, with just a few connections and some Arduino code, you’ll be able to sense temperature in no time!

DS18B20 1-Wire Temperature Sensor

The DS18B20 is a 1-Wire® temperature sensor manufactured by Dallas Semiconductor (acquired by Maxim Integrated). Because it is a 1-wire device, it only needs one digital pin to communicate with the microcontroller.

The sensor is typically available in two form factors. One comes in a TO-92 package, which resembles a simple transistor. The other comes in the form of a waterproof probe, which is more useful when measuring something far away, underwater, or beneath the ground.

Types Of DS18B20 Temperature Sensor
Types Of DS18B20 Temperature Sensor

The DS18B20 temperature sensor is fairly precise and does not require any external components to function. It has a temperature range of -55°C to +125°C and an accuracy of ±0.5°C.

The temperature sensor’s resolution can be set to 9, 10, 11, or 12 bits. The default resolution at power-up, however, is 12-bit (i.e., 0.0625°C precision).

The sensor operates on a 3V to 5.5V power supply and draws only 1mA during active temperature conversions.

Here are the specifications:

Power Supply3V to 5.5V
Current Consumption1mA
Temperature Range-55 to 125°C
Accuracy±0.5°C
Resolution9 to 12 bit (selectable)
Conversion Time< 750ms

Multiple DS18B20 On a Single Bus

One of the DS18B20’s features is that multiple DS18B20s can coexist on the same 1-Wire bus. Because each DS18B20 is pre-programmed with a unique 64-bit serial code, they can be distinguished from one another.

This feature can be extremely useful when you need to control multiple DS18B20s spread across a large area.

Check out this tutorial to learn how to read multiple DS18B20 temperature sensors.

DS18B20 Sensor Pinout

DS18B20 Pinout Including Waterproof Temperature Sensor

GND is the ground pin.

DQ is a 1-Wire Data Bus that should be connected to a digital pin on the microcontroller.

VDD pin provides power to the sensor, which can range from 3.3V to 5V.

Wiring a DS18B20 Temperature Sensor to an Arduino

Let’s connect the DS18B20 to the Arduino.

The connections are straightforward. Begin by connecting VDD to the Arduino’s 5V pin and GND to ground.

Connect the signal pin DQ to Arduino’s digital pin 2. To keep the data transfer stable, you’ll also need to connect the 4.7k pull-up resistor between the signal and power pins (Note: internal pull-ups on the arduino do not work here).

To avoid overheating and damage, make sure the DS18B20 is connected properly.

Wiring DS18B20 Temperature Sensor to Arduino
Wiring DS18B20 Temperature Sensor to Arduino

If you’re using the waterproof version of the DS18B20, connect the red wire to 5V, the black wire to ground, and the yellow wire to digital pin 2 on the Arduino. You still have to connect a 4.7K pullup resistor between the data and the 5V.

Wiring Waterproof DS18B20 Temperature Sensor to Arduino
Wiring Waterproof DS18B20 Temperature Sensor to Arduino

Installing Library For DS18B20

The 1-Wire protocol is a bit complicated, and it takes a lot of code to make it work. DallasTemperature.h was written to abstract away this unnecessary complexity, allowing us to issue simple commands to obtain temperature readings from the sensor.

To install the library, navigate to Sketch > Include Library > Manage Libraries… Wait for the Library Manager to download the library index and update the list of installed libraries.

Arduino Library Installation - Selecting Manage Libraries in Arduino IDE

Filter your search by entering ‘ds18b20’. There should be a couple of entries. Look for DallasTemperature by Miles Burton. Click on that entry and then choose Install.

Installing Dallas Temperature Library In Arduino IDE

Dallas Temperature is a hardware-specific library that handles lower-level functions. It must be paired with the One Wire Library in order to communicate with any one-wire device, not just the DS18B20. Install this library as well.

Installing OneWire Library In Arduino IDE

Arduino Example Code

The sketch below will provide you with a thorough understanding of how to read temperature readings from a DS18B20 Temperature Sensor and can serve as the foundation for more practical experiments and projects.

#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 device
OneWire oneWire(ONE_WIRE_BUS);	

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

void setup(void)
{
  sensors.begin();	// Start up the library
  Serial.begin(9600);
}

void loop(void)
{ 
  // Send the command to get temperatures
  sensors.requestTemperatures(); 

  //print the temperature in Celsius
  Serial.print("Temperature: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print((char)176);//shows degrees character
  Serial.print("C  |  ");
  
  //print the temperature in Fahrenheit
  Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  Serial.print((char)176);//shows degrees character
  Serial.println("F");
  
  delay(500);
}

Here’s what the output looks like on the serial monitor.

DS18B20 Temperature Sensor Output On Serial Monitor

Code Explanation:

The sketch begins by including the OneWire.h and DallasTemperature.h libraries and declaring the Arduino pin to which the sensor’s signal pin is connected.

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2

To communicate with the DS18B20 sensor, we do two things. First, we create a one-wire object and pass the sensor’s signal pin as a parameter. Second, we create a DallasTemperature library object and pass the reference of the one-wire object (that we just created) as a parameter.

OneWire oneWire(ONE_WIRE_BUS);	
DallasTemperature sensors(&oneWire);

In the setup section, we establish serial communication with the PC and call the begin() function. The begin() function scans the bus for connected DS18B20 sensors and sets the bit resolution (12 bits) for each one.

void setup(void) {
  sensors.begin();	// Start up the library
  Serial.begin(9600);
}

In the loop section, we call the requestTemperatures() function, which instructs all sensors on the bus to perform a temperature conversion.

Then we call the getTempCByIndex(deviceIndex) function, where deviceIndex is the location of the sensor on the bus. This function reads the temperature reading from the corresponding sensor and returns it. 

If you only have one DS18B20 on the bus, set deviceIndex to 0.

void loop(void) { 
  // Send the command to get temperatures
  sensors.requestTemperatures(); 

  //print the temperature in Celsius
  Serial.print("Temperature: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print((char)176);//shows degrees character
  Serial.print("C  |  ");
  
  //print the temperature in Fahrenheit
  Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  Serial.print((char)176);//shows degrees character
  Serial.println("F");
  
  delay(500);
}

Other useful functions in the DallasTemperature.h library

There are many useful functions you can use with the DallasTemperature object. Some of them are listed below:

  • setResolution() function sets the resolution of the DS18B20’s internal ADC to 9, 10, 11, or 12-bits, which correspond to 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively.
  • bool getWaitForConversion() function returns the value of the waitForConversion flag, which is useful when determining whether or not a temperature conversion is complete.
  • setHighAlarmTemp() and setLowAlarmTemp() functions configure a device’s internal high and low temperature alarms in degrees Celsius. The acceptable temperature range is -55 to 125°C.
  • bool hasAlarm() function returns true when the temperature exceeds either the high or low alarm set point.