cactus.io logo

Hookup DS18B20 Temperature Sensor to Arduino Board

Overview

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements. It uses a 1 wire bus to communicate.

Each device has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus.

It has an operating temperature range of -55°C to +125°C and is accurate to ±0.5°C over the range of -10°C to +85°C.

The DS18B20 is available in a discrete component form or in a pre wired sealed or water proofed package. They are readily available from many suppliers.

In this hookup we are only connecting one device to the Arduino. We also have a hookup guide for connecting several DS18B20 to the Arduino board. Click on one of the following links.

Connect Multiple DS18B20 Sensors to the Arduino Board - using seperate data pins

Images

View Maxim Integrated DS18B20 Temperature Sensor Details
View Waterproof DS18B20 Temperature Sensor Details
Note: Images shown on this page are not to scale.

Hardware and Software Parts List

  • Arduino Board
  • DS18B20 Sensor. It also available pre-wired. For example the Adafruit PN 381 Pre-wired Waterproof Sensor
  • Breadboard hookup wires
  • USB Cable to suite Arduino

Our library has been derived from the OneWire library. Visit http://www.pjrc.com


Data Sheets


Wiring up DS18B20 Temperature Sensor


The DS18B20 can be wired up using 2 or three wires. Using three wires the DS18B20 is powered using the normal Vdd and GND pins. This is the preferred option.

We can also use what is called Parasitic power which is where we get power off the DATA pin. Here we use only two wires. The Vdd is connected to GND. We are not using this power mode as it more complicated to implement.

Hookup Diagram for connecting the DS18B20 to a Arduino Board

Connect Arduino to DS18B20 Temperature Sensor Hookup

Software

The hookup and Arduino Sketch is for a single DS18B20. It does not support multiple devices connected to the same bus.

You will need to download the cactus.io DS18B20 library to work with this sample sketch. The library supports reading the temperature and the DS18B20 serial number. This library only supports a single DS18B20 connected to the data pin.

Limitations
  • Supports only one sensor on the data pin
  • Its blocking code.
Maxim Integrated DS18B20 Sample Sketch
Download as (Arduino Sketch) (Text File)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Example sketch for Maxim Integrated DS18B20 temperature sensor
Written by cactus.io, and requires the cactus_io_DS18B20 library.
This sketch was tested using the Adafruit Prewired DS18B20 Sensor.
For hookup details using this sensor then visit
http://cactus.io/hookups/sensors/temperature-humidity/ds18b20/hookup-arduino-to-ds18b20-temperature-sensor
*/

#include "cactus_io_DS18B20.h"

int DS18B20_Pin = 2; //DS18S20 Signal pin on digital 2
// Create DS18B20 object
DS18B20 ds(DS18B20_Pin);

void setup() {
ds.readSensor();

Serial.begin(9600);
Serial.println(Maxim Integrated DS18B20 Temperature Sensor | cactus.io");
Serial.println("DS18B20 Serial Number: ");

// we pass the serial number byte array into the printSerialNumber function
printSerialNumber(ds.getSerialNumber());

Serial.println("");
Serial.println("");
Serial.println("Temp (C)\tTemp (F)");
}

void loop() {
ds.readSensor();

Serial.print(ds.getTemperature_C()); Serial.print(" *C\t");
Serial.print(ds.getTemperature_F()); Serial.println(" *F");

// Add a 2 second delay.
delay(2000);
}

// We call this function to display the DS18B20 serial number.
// It takes an array of bytes for printing
void printSerialNumber(byte *addr) {
byte i;
for( i = 0; i < 8; i++) {
Serial.print("0x");
if (addr[i] < 16) {
Serial.print('0');
}
Serial.print(addr[i], HEX);
if (i < 7) {
Serial.print(", ");
}
}
}

Sketch Console Output

Connect Arduino to Maxim DS18B20 Temperature Sensor

Other Resources

Arduino.cc DS18B20 Page

Onewire Library Maintainer


License