ESP32 Arduino: Getting DS18B20 sensor unique identifier

In this tutorial we will learn how to fetch the unique identifier of a DS18B20 sensor, using the ESP32 and the Arduino core. The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board and a waterproof version of the sensor.

Introduction

In this tutorial we will learn how to fetch the unique identifier of a DS18B20 sensor, using the ESP32 and the Arduino core. Please check this previous tutorial for the wiring diagram between the ESP32 and the sensor.

As covered in the mentioned previous tutorial, the DS18B20 uses the OneWire communication protocol. This protocol allows for more multiple devices to be connected to the same bus and each device has a unique factory programmed and non-changeable 64-bit address [1].

As also covered in the previous post, we can use the OneWire and the DallasTemperature libraries to interact with the sensor using a higher level interface, without the need to worry about the lower level details of the OneWire protocol.

One of the aspects we covered is that, when calling the methods to retrieve a temperature measurement, we can pass a device index, which corresponds to an integer representing from which device we want to get the measurement.

This is needed because, as already mentioned, multiple devices can be attached to the same bus and this works well if we don’t need to know from which specific sensor the measurement came from.

Nonetheless, for some applications, we may want to know from which specific device the measurements come from, so it is important to have a way of getting the device address. Furthermore, after knowing the address of the device, we can use it directly to fetch the measurements, rather than passing indexes, as can be seen by the header file of the DallasTempeature library.

So, in this tutorial, we will check how to get the unique address of a DS18B20 sensor by index, using the DallasTemperature library. Please note that the terms “address” and “identifier” are used with the same meaning during this post.

The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board and a waterproof version of the sensor.

The code

We will start our code by including the needed libraries. The first one is the OneWire.h,  which allows to interact with devices using the OneWire protocol. The second one is the DallasTemperature.h, which will expose some higher level methods that will allow us to interact with the DS18B20 sensor.

#include "OneWire.h"
#include "DallasTemperature.h"

After the includes, we will need an object of class OneWire, which allows to exchange bytes with devices connected to the OneWire bus. This object will be used internally by the DallasTemperature library to exchange the data with the sensor.

As input, the constructor of this class receives the number of the microcontroller pin connected to the sensor.

OneWire oneWire(22);

We will also need an object of class DallasTemperature, which is the one will use in our code to get the sensor address.

As input of the constructor of this class we will pass the address of our previously instantiated OneWire object.

DallasTemperature tempSensor(&oneWire);

Moving on to the setup function, we will start by opening a serial connection, to later output the address of the sensor.

Serial.begin(115200);

After that we will call the begin method on our DallasTemperature object. This method will be responsible for initializing the OneWire bus.

tempSensor.begin();

Next we need to declare a byte buffer to hold the ID of the sensor. Since, as already mentioned in the introductory section, the IDs have 64 bits, it means we will need an array with a length of 8 bytes.

uint8_t address[8];

To get the identifier, we simply need to call the getAddress method of the DallasTemperature object, passing as first input our byte buffer and as second input the index of the device we want to check. Since we only have one sensor attached to the OneWire bus, we use the index 0.

This method returns true if the device was found, so we will use the returning value to perform an error check.

if(tempSensor.getAddress(address, 0)){
  Serial.print("Address fetched:");
}else{
  Serial.println("Error fetching the address");
  return;
}

To finalize, we will print each byte of the address in hexadecimal format.

for(int i = 0; i<8; i++){
  Serial.printf("%02X ",address[i]);
}

The final source code can be seen below.

#include "OneWire.h"
#include "DallasTemperature.h"

OneWire oneWire(22);
DallasTemperature tempSensor(&oneWire);

void setup(void)
{
  Serial.begin(115200);
  tempSensor.begin();

  uint8_t address[8];

  if(tempSensor.getAddress(address, 0)){
    Serial.print("Address fetched:");
  }else{
    Serial.println("Error fetching the address");
    return;
  }

  for(int i = 0; i<8; i++){
    Serial.printf("%02X ",address[i]);
  }
}

void loop(void){}

Testing the code

To test the code, simply compile it and upload it to your ESP32, after performing all the electrical connections. Once the procedure finishes, open the Arduino IDE serial monitor. You should get an output similar to figure 1, which shows the 64-bit unique address getting printed.

Printing the unique address of a DS18B20 sensor to the Arduino IDE serial monitor

Figure 1 – Output of the program.

Related posts

ESP32 Arduino: Getting temperature from DS18B20 sensor

References

[1] https://www.maximintegrated.com/en/app-notes/index.mvp/id/1796

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s