Digital temperature sensors

Fig. 1: DS18B20 pinout.

Theory

The Dallas DS18B20 is a low cost temperature sensor with a digital interface.

It has three pins:

There must be a resistor (typically 4.7k) connected between the data line and the power line (Fig. 2). In electronics terminology, we say that the data line is “pulled up” by the resistor.

Wiring

Fig. 2: Breadboard layout for temperature sensor circuit.

Fig. 2: Breadboard layout for temperature sensor circuit.

Code

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

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Set up the necessary objects to interface with the sensors
OneWire oneWire(2); // Data wire is connected to pin 2 on the Arduino
DallasTemperature sensors(&oneWire); // The data wire contains Dallas temperature sensors

// How many sensors to support?
#define MAX_SENSORS 5

// Create variables to hold the device addresses
int num_sensors;
DeviceAddress addresses [MAX_SENSORS];

/*
 * Setup function. Here we do the basics
 */
void setup(void)
{
  // put your setup code here, to run once:

  // Set the baud rate
  Serial.begin(9600);

  // Scan for temperature sensors
  sensors.begin();
  num_sensors = sensors.getDeviceCount();

  // Print out how many sensors we found
  Serial.print("Found ");
  Serial.print(num_sensors);
  Serial.print(" device(s).");
  Serial.println();

  // Every DS18B20 sensor has a globally unique serial number,
  // called an address. We need the address to speak to a
  // particular sensor. Here we scan for the addresses of each
  // connected device. 
  for (int i = 0; i < num_sensors; i++) {
    // Read its address
    sensors.getAddress(addresses[i], i);

    // Instruct it to use 12 bit precision, which is the maximum.
    // Other choices are 9, 10, 11 and 12.
    sensors.setResolution(addresses[i], 12);

    // Print out a message
    Serial.print("Device ");
    Serial.print(i);
    Serial.print(" address is ");
    printAddress(addresses[i]);
    Serial.println();
  }
}

void loop(void)
{ 
  // put your main code here, to run repeatedly:

  // Request temperature from all sensors
  sensors.requestTemperatures(); // Send the command to get temperatures

  // Read the temperature from every sensor
  for (int i = 0; i < num_sensors; i++) {
    float tempC = sensors.getTempC(addresses[i]);
    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(" temp: ");
    Serial.print(tempC);
    Serial.print(" C");
    Serial.println();
  }

  delay(200);
}

// Function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (int i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) {
      Serial.print("0");
    }
    Serial.print(deviceAddress[i], HEX);
    if (i < 7) {
      Serial.print(":");
    }
  }
}

Exercise

Experiment with the above code.

Extension task

Sharing a sensor with the person next to you, connect two sensors (Fig. 3).

Fig. 3: Breadboard layout for two sensor circuit.

Fig. 3: Breadboard layout for two sensor circuit.

How similar are the temperature measurements of the two different sensors when they are adjacent?

Application ideas

These sensors can be daisy-chained to make a long string. You use a single pull-up resistor for the whole string (Fig. 4).

Fig. 4: Breadboard layout for temperature sensor circuit.

Fig. 4: Breadboard layout for temperature sensor circuit.

These can be used to examine microclimates, e.g. the temperature every 30 cm up the bank of a dry creek bed from shade to sun. You can also deploy the “sensor strings” in built environments, e.g. to look at the vertical temperature gradients in buildings.


This work is licensed under a Creative Commons Attribution 4.0 International License. For comments or corrections, please contact [email protected].