DS18B20 Arduino User Manual – Part 2 – Getting the Device Address

DS18B20 OneWire Addresses

DS18B20 Temp SensorRecall from Part 1, that one key benefit of OneWire is that you can put more than one device on the same wire.   In order to accomplish that,  each device must have a an address (a unique identifier).  The OneWire devices each have a 64 bit address ( or 8 bytes).

What this means it that your are going to need to know the address of the particular DS18B20 sensors that you are connecting to your Arduino.

Fortunately, the OneWire library included with your Arduino download makes that relatively easy.

Why Get The DS18B20 OneWire Address

If you analyze the code I provide here, you’ll see its just a subset of the example provided with your Arduino download.  In fact, you could just use that code to get your address.

I choose this approach, because it strips away the other noise and simply focuses on what is necessary to retrieve a device address.  In subsequent parts of this series, you will use the address to talk to your DS18B20.

KNOWING the address is pretty important.

While its possible to simply do an address search in a project that uses one DS18B20,   the code required to do so takes up memory that you may want for something else.

With respect to trying to use two or more sensors,  you have the problem of knowing which is which.

Acquiring and recording the address for later use solves both of these problems.

Finding the DS18B20 OneWire Address Tutorial

Connect Your Arduino

This set up is exactly like the last set up.

DS18B20 Ardunio Connections

Copy, Paste and Upload Your Sketch

The sketch includes comments that help to identify

#include <OneWire.h>

OneWire  ds(10);  // This is where DQ of your DS18B20 will connect.

void setup(void) {
  Serial.begin(9600);
  getDeviceAddress();
}

void getDeviceAddress(void) {
  byte i;
  byte addr[8];
  
  Serial.println("Getting the address...\n\r");
  /* initiate a search for the OneWire object we created and read its value into
  addr array we declared above*/
  
  while(ds.search(addr)) {
    Serial.print("The address is:\t");
    //read each byte in the address array
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      // print each byte in the address array in hex format
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    // a check to make sure that what we read is correct.
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        Serial.print("CRC is not valid!\n");
        return;
    }
  }
  ds.reset_search();
  return;
}

void loop(void) {
  // do nothing
}

Open Your Serial Monitor and Get the DS18b20 Address

If you are successful,  you will see a response that looks like the picture below.   The only difference will be that address reported will be different.  Record this address and keep it with your DS18B20.  You will need it later.

DS18B20 Arduino Tutorial 2 Output

 

4 Comments

  1. Marc April 30, 2017
  2. Arturo February 12, 2016
  3. capnfatz@gmail.comAuthor January 25, 2016
  4. simon January 25, 2016

Add a Comment

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