DS18B20

 

DS18B20

The DS18B20 is a versatile, programmable, temperature sensor from Maxim. It's popularity is undoubtedly enhanced by Maxim's generous Samples policy and there's so much information on the web, there's no point repeating it here. I'll concentrate, instead, on the one or two issues that cropped up when attempting to use the existing libraries in the Arduino IDE 1.0 environment.

The DS18B20 uses Maxim's 1-wire bus that allows multiple 1-wire devices to share the same bus. The bus is called 1-wire because it only requires the single data wire and ground. It does this by 'leaching' its own power supply from the data wire. Maxim call this “parasite power” but it's also possible to supply it with a "proper" supply of between 3.0 and 5.5 volts.

Using the DS18B20 with the Arduino requires two 3rd party libraries:

  • PJRC OneWire Library. Currently version 2.1 it includes code to handle the Arduino 1.0 IDE.

  • Dallas Temperature Control Library by Miles Burton. Currently, the latest version is TCL 3.7.1.

    After unzipping, check the top few lines of DallasTemperature.cpp.

    
    #include "DallasTemperature.h"
    
     extern "C" {
       #include "WConstants.h"
     }
    
    
    


    I edited this file before saving it to the recommended library location (Documents\My Documents\Arduino\Libraries). It now compiles OK without errors:

    
    
    #include "DallasTemperature.h"
    
    // extern "C" {
    //   #include "WConstants.h"
    // }
    
    // Change for Arduino IDE v 1.0
    
    extern "C" {
      #if ARDUINO >= 100
      #include "Arduino.h"        
      #else
      #include "WConstants.h"     
      #endif
    }
    
    


Most of the examples I found involved getting the device addresses first and then hard-coding those addresses. This program (sketch) discovers the device addresses during the setup() function and copies them into two DeviceAddress arrays (one for each temperature sensor). When memory is short, it makes sense to hard-code the addresses instead.


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

#define ONE_WIRE_BUS 3                             // Data wire is plugged into digital pin 3 on the Arduino
#define NumberOfDevices 2                          // Set maximum number of devices in order to dimension 
                                                   // Array holding all Device Address arrays.

OneWire oneWire(ONE_WIRE_BUS);                     // Setup a oneWire instance to communicate with any OneWire devices

DallasTemperature sensors(&oneWire);               // Pass our oneWire reference to Dallas Temperature. 

byte allAddress [NumberOfDevices][8];              // Device Addresses are 8-element byte arrays.
                                                   // we need one for each of our DS18B20 sensors.

byte totalDevices;                                 // Declare variable to store number of One Wire devices
                                                   // that are actually discovered.
void setup() {
  Serial.begin(9600);
  sensors.begin();
  totalDevices = discoverOneWireDevices();         // get addresses of our one wire devices into allAddress array 
  for (byte i=0; i < totalDevices; i++) 
    sensors.setResolution(allAddress[i], 10);      // and set the a to d conversion resolution of each.
}

byte discoverOneWireDevices() {
  byte j=0;                                        // search for one wire devices and
                                                   // copy to device address arrays.
  while ((j < NumberOfDevices) && (oneWire.search(allAddress[j]))) {        
    j++;
  }
  for (byte i=0; i < j; i++) {
    Serial.print("Device ");
    Serial.print(i);  
    Serial.print(": ");                          
    printAddress(allAddress[i]);                  // print address from each device address arry.
  }
  Serial.print("\r\n");
  return j                      ;                 // return total number of devices found.
}

void printAddress(DeviceAddress addr) {
  byte i;
  for( i=0; i < 8; i++) {                         // prefix the printout with 0x
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');                        // add a leading '0' if required.
      }
      Serial.print(addr[i], HEX);                 // print the actual value in HEX
      if (i < 7) {
        Serial.print(", ");
      }
    }
  Serial.print("\r\n");
}

void printTemperature(DeviceAddress addr) {
  float tempC = sensors.getTempC(addr);           // read the device at addr.
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print(tempC);                          // and print its value.
    Serial.print(" C (");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
    Serial.print(" F)");
  }
}

void loop() {
  delay(4000);
  sensors.requestTemperatures();                // Initiate  temperature request to all devices
  for (byte i=0; i < totalDevices; i++) {
    Serial.print("Device ");
    Serial.print(i);
    Serial.print(": ");
    printTemperature(allAddress[i]);
    Serial.print("\n\r");
  }
  Serial.print("\n\r\n\r");
}



 

Back to Index

This site and its contents are © Copyright 2005 - All Rights Reserved.