Digital temperature sensors
Lesson #11—Make your buildings smartTheory
The Dallas DS18B20 is a low cost temperature sensor with a digital interface.
It has three pins:
- Ground,
- Data, and
- Power (5 V).
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
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).
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).
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.