Connecting and using the DS18B20 temperature sensor with a microcontroller

Introduction

The DS18B20 is a 1-Wire digital temperature sensor from Maxim IC. It reports the temperature in degrees Celsius with 9 to 12-bit precision, with a range from -55 to 125 (+/-0.5). Each temperature sensor has a unique 64-Bit serial number etched into the silicon which allows for a huge number of sensors to be used on one data bus with the probability of 9,223,372,036,854,775,807 to 1 that any two will clash!

The DS18B20 Temperature sensor

Features

  • Unique 1-Wire interface that requires only one port pin for communication.
  • Each device has a unique 64-bit serial code stored in an onboard ROM, as mentioned above.
  • Requires no external components.
  • Can be powered from it’s data line.
  • Power supply range is from 3.0V to 5.5V.
  • Measures temperatures from –55°C to +125°C (–67°F to +257°F) with ±0.5°C accuracy from –10°C to +85°C.
  • Thermometer resolution is user-selectable from 9 to 12 bits.
  • Converts temperature to 12-bit digital word in a maximum time of 750ms.
  • Alarm search command which allows it to identify and addresses devices whose temperature is outside of programmed limits (temperature alarm condition).
  • Applications include thermostatic controls, industrial systems, consumer products, thermometers, or any thermally sensitive system.

What you need to make a working thermometer

To construct an electronic thermometer you will need the following:

  • A microcontroller. (In this instance an Arduino or Atmega328p)
  • A DS18B20 sensor and a 4.7k resistor.
  • Breadboard.
  • Jumper leads.

Libraries

You will need to download and install 2 libraries for this exercise. If you are using the arduino then this should be fairly straight forward and can be acheived by following the menus within the IDE. The two libraries that you will need are:

  • 1-wire bus
  • Dallas Temperature

Build the simple circuit

The DS18B20 circuit diagram

To read the data from DS18B20 in the serial monitor of the Arduino IDE, build the circuit in the above schematic.

Connect the sensor pins to the Arduino in the following order: pin 1 to GND; pin 2 to any digital pin (pin 2 in this case); pin 3 to +5V or +3.3V. Connect the pull-up resistor between the Vcc supply and the data wire.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// First we include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>

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

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

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

void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");

// Start up the library
sensors.begin();
}

void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.println("DONE");
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
delay(1000);
}

Place the above code into the IDE and upload via the arduino uploader. If everything is okay you should see the temperature being measured and showed in the serial monitor of the IDE.

^