DS18B20 Arduino Users Manual – Part 3 – Reading a Single Temperature

Reading the Temperature from One DS18B20

18B20 Temperature From Single DeviceOnce you have a DS18B20 address,  you can instruct it to measure the temperature and then read its value.   There is more than one way of doing this, however, this article is going to focus on what I deem to be the simplest  with the help of both the OneWire and Dallas Temperature Control libraries.

The OneWire library is part of your Arduino download.

The Dallas Temperature Control library is not part of your Arduino software distribution.   You will need to download it HERE.

If you are not familiar with adding libraries,   you can learn about it on the Arduino site.

Basic Arduino Program Flow for Reading Temp from DS18B20

18B20 Temp Sensor Basic FlowYou may want to write your own sketch or incorporate parts of my sketch into yours.  However, in doing so, its pretty important to understand that there are some steps you must perform.

  1.  To begin with,  you have got to set the resolution of your DS18B20.   Valid selections are 9, 10, 11 and 12.    Since you should only need to set the resolution once, it is probably best to do so into your setup routine.  The section below discusses resolution in more detail.
  2.  Next, you’ve got to tell the DS18B20 read a temperature and remember the temperature that it read.
  3. Finally,  you’ll want to go back to that DS18B20 and tell it to deliver to you the temperature you just told it to remember.  It is at this point that you can do things like turn on fans, heaters or simple print to LCDs.

Notes on DS18B20 Resolution

As previously mentioned,  the DS18B20 has four selectable resolutions that range from 9 to 12.   While it may seem nice to have a very detailed response or ‘precise’ measurement, there is a trade off.  Specifically, the higher the resolution, the more time its going to take to make the measurement.   See the table below.

9 bit0.5 degrees C93.75 mSec
10 bit0.25 degrees C187.5 mSec
11 bit0.125 degrees C375 mSec
12 bit0.0625 degrees C750 mSec

 Arduino DS18B20 Reading a Single Temperature Tutorial

 Connect Your Arduino to the DS18B20

There is no change from the previous tutorials.  If you’ve already got your device connected, leave it.   Otherwise, connect it as shown below.

DS18B20 Ardunio Connections

Upload The DS18B20 Sketch for Measuring a Single DS18B20

Copy and paste the code you see below.

NOTE – You are going to have to substitute your specific device address for the one I used.


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

// Create a Onewire Referenca and assign it to pin 10 on your Arduino
OneWire oneWire(10);

// declare as sensor referenec by passing oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// declare your device address
DeviceAddress tempSensor = {0x28, 0xFF, 0x2B, 0x45, 0x4C, 0x04, 0x00, 0x10};

// A Variable to hold the temperature you retrieve
float tempC;

void setup(void)
  // start serial port
  // set the resolution to 9 bit - Valid values are 9, 10, or 11 bit.
  sensors.setResolution(tempSensor, 9);
  // confirm that we set that resolution by asking the DS18B20 to repeat it back
  Serial.print("Sensor Resolution: ");
  Serial.println(sensors.getResolution(tempSensor), DEC); 


void loop(void)
  // Tell the Sensor to Measure and Remember the Temperature it Measured
  sensors.requestTemperaturesByAddress(tempSensor); // Send the command to get temperatures
  // Get the temperature that you told the sensor to measure
  tempC = sensors.getTempC(tempSensor);
  Serial.print("Temp C: ");
  Serial.print(tempC,4);  // The four just increases the resolution that is printed
  Serial.print(" Temp F: ");
  // The Dallas Temperature Control Libray has a conversion function... we'll use it

Open the Serial Monitor and See the Output

If you did things correctly, you should see an output that looks like the picture below.

DS18B20 Arduino Tutorial 3 Output

Possibly an Even Simpler Option to Read Temperature from One DS18B20

I’ve probably made it sound like you MUST have an address of the DS18B20.  You don’t.   If you only have one device in your project, you can make use of a couple of commands.

The first is to use “sensors.requestTemperatures();” as opposed to the requesting the temperature by address.

The second is to use ‘getTempCByIndex‘ instead of ‘getTempC’.

Using the global ‘requestTemperatures‘ tells every DS18B20 connected to get a temperature.  Using ‘getTempCByIndex‘  allows you to get the first and only one it sees.

Try it.  Go ahead and copy and paste the sketch you see below:


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

// Assign to pin 10 of your Arduino to the DS18B20
#define ONE_WIRE_BUS 10

// Create a onewire instanace
OneWire oneWire(ONE_WIRE_BUS);

// Declare a DS18B20 Instance and assing the OneWire reference to it.
DallasTemperature sensors(&oneWire);

void setup(void)
  // Start serial port
  //Start the DallasTemperature Library

void loop(void)
  sensors.requestTemperatures(); // Tell the DS18B20 to get make a measurement
  Serial.println(sensors.getTempCByIndex(0),4); // Get that temperature and print it.

One Comment

  1. jj May 24, 2017

Add a Comment

Your email address will not be published. Required fields are marked *