Guide for DS18B20 Temperature Sensor with Arduino

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:

Build 25 cool Arduino projects with our course even with no prior experience DOWNLOAD »

Build 25 cool Arduino projects with our course even with no prior experience DOWNLOAD »

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

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

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.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
  Based on the Dallas Temperature Library example
*********/

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

// Data wire is conntec to the Arduino digital pin 2
#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 sensor 
DallasTemperature sensors(&oneWire);

void setup(void)
{
  // Start serial communication for debugging purposes
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
}


void loop(void){ 
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures(); 
  
  Serial.print("Celsius temperature: ");
  // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  Serial.print(sensors.getTempCByIndex(0)); 
  Serial.print(" - Fahrenheit temperature: ");
  Serial.println(sensors.getTempFByIndex(0));
  delay(1000);
}

View raw code

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.

Download our Free eBooks and Resources

Thanks for reading,

-Rui


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

5 thoughts on “Guide for DS18B20 Temperature Sensor with Arduino”

  1. Hello J uses your tutorial . J have errors at compile on dallastemperature.h Can you help me by giving me the dallastemperature library works
    Best regards
    Pierre

  2. Hi Rui,

    Thanks for your tutorial.

    I need to compare two temperature sensors, lets say T1 and T2 and control a relay when T1 e higher than T2.
    How can i achieve this?

    Kind regards,
    Rodrigo Catarino

    • Hi Rodrigo. You need an if statement that compares both temperatures and turns the relay on when T1 > T2.
      Controlling a relay is the same thing as controlling an LED but with inverted logic.
      digitalWrite(relay, HIGH); –> turns the relay off
      digitalWrite(relay, LOW); –> turns the relay on

      You may want to take a look at this tutorial to see how to wire the relay to the Arduino: https://randomnerdtutorials.com/guide-for-relay-module-with-arduino/
      I hope this helps.
      Regards,
      Sara 🙂

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.