Random Nerd Tutorials
Shares

Guide for DS18B20 Temperature Sensor with Arduino

Shares

In this guide I’ll show you how to read the temperature with the DS18B20 temperature sensor with the Arduino board.

The DS18B20 Temperature Sensor

The DS18B20 temperature sensor is a 1-wire digital temperature sensor. This means that you can read the temperature with a very simple circuit setup. It communicates on common bus, which means that you can connect several devices and read their values using just one digital pin of the Arduino.

The sensor has just three pins as you can see in the following figure:

DS18B20

The DS18B20 is also available in waterproof version:

waterproof

Features

Here’s some main features of the DS18B20 temperature sensor:

  • Comunicates over 1-wire bus communication
  • Operating range temperature: -55ºC to 125ºC
  • Accuracy +/-0.5 ºC (between the range -10ºC to 85ºC)

Read the temperature with the DS18B20 temperature sensor and the Arduino

In this example, you’ll read the temperature using the DS18B20 sensor and the Arduino, and the values will be displayed on the Arduino Serial Monitor.

Parts needed

Schematics

The sensor can operate in two modes:

  • Normal mode: 3-wire connection is needed. Here’s the schematic you need to follow:

normal mode_bb

  • Parasite mode: only 2 wires required, the data and ground. The sensor derives its power from the data line. In this case, here’s the schematic you need to follow:

parasite mode_bb

You can read the temperature of more than one sensor at the same time using just one digital Arduino pin. For that, you just need to connect together all the DQ pins to any digital Arduino pin.

Code

You’ll need to install the OneWire Library and DallasTemperature Library.

Installing the OneWire Library

  1. Click here to download the OneWire library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get OneWire-master folder
  3. Rename your folder from OneWire-master to OneWire
  4. Move the OneWire folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Installing the DallasTemperature Library

  1. Click here to download the DallasTemperature library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get Arduino-Temperature-Control-Library-master folder
  3. Rename your folder from Arduino-Temperature-Control-Library-master to DallasTemperature
  4. Move the DallasTemperature folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

After installing the needed libraries, upload the following code to your Arduino board.

»» View and download code on GitHub!

Finally, you should open the Arduino IDE serial monitor at a 9600 baud rate and you’ll see the temperature displayed in both Celsius and Fahrenheit:

serial monitor

Wrapping Up

I hope you’ve found this guide useful.

Share this post with a friend that also likes electronics.

Thanks for reading,

-Rui

Leave a Comment:

Add Your Reply