Guide for DS18B20 Temperature Sensor with Arduino

This guide shows how to use the DS18B20 temperature sensor with the Arduino board. You’ll learn how to wire the sensor, install the required libraries and get temperature from one or multiple DS18B20 sensors.

You might also like reading other DS18B20 guides:

Introducing DS18B20 Temperature Sensor

The DS18B20 temperature sensor is a one-wire digital temperature sensor. This means that it just requires one data line (and GND) to communicate with the Arduino.

It can be powered by an external power supply or it can derive power from the data line (called “parasite mode”), which eliminates the need for an external power supply.

The following table shows how you should wire the DS18B20 sensor to your Arduino board:

DS18B20Arduino
GNDGND
DQAny digital pin (with 4.7k Ohm pull-up resistor)
VDD5V (normal mode) or GND (parasite mode)

Each DS18B20 temperature sensor has a unique 64-bit serial code. This allows you to wire multiple sensors to the same data wire. So, you can get temperature from multiple sensors using just one Arduino digital pin.

The DS18B20 temperature sensor is also available in waterproof version.

Here’s a summary of the most relevant specs of the DS18B20 temperature sensor:

  • Communicates over one-wire bus communication
  • Power supply range: 3.0V to 5.5V
  • Operating temperature range: -55ºC to +125ºC
  • Accuracy +/-0.5 ºC (between the range -10ºC to 85ºC)

For more information consult the DS18B20 datasheet.

Parts Required

To show you how the sensor works, we’ll build a simple example that reads the temperature from the DS18B20 sensor with the Arduino and displays the values on the Arduino Serial Monitor.

Here’s a list of parts you need to complete this tutorial

  • Arduino UNO – read Best Arduino Starter Kits
  • DS18B20 temperature sensor (one or multiple sensors) – waterproof version
  • 4.7k Ohm resistor
  • Breadboard
  • Jumper wires

Schematic

The sensor can operate in two modes:

  • Normal mode: 3-wire connection is needed. You provide power to the VDD pin. Here’s the schematic you need to follow:
  • Parasite mode: You only need data and GND. The sensor derives its power from the data line. In this case, here’s the schematic you need to follow:

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

Upload Code – Single DS18B20

To interface with the DS18B20 temperature sensor, you need to install the One Wire library by Paul Stoffregen and the Dallas Temperature library. Follow the next steps to install those libraries.

Installing Libraries

1. Open your Arduino IDE and go to Sketch Include Library > Manage Libraries. The Library Manager should open.

2. Type “OneWire” in the search box and install the OneWire library by Paul Stoffregen.

3. Then, search for “Dallas” and install the Dallas Temperature library by Miles Burton.

After installing the needed libraries, upload the following code to your Arduino board. This sketch is based on an example from the Dallas Temperature library.

/*********
 Gnd_To_Vcc
  Based on the Dallas Temperature Library example
*********/

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

// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 4

// 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);
}

There are many different ways to get the temperature from DS18B20 temperature sensors. If you’re using just one single sensor, this is one of the easiest and simplest ways.

Arduino with DS18b20 Temperature Sensor

How the Code Works

Start by including the OneWire and the DallasTemperature libraries.

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

Create the instances needed for the temperature sensor. The temperature sensor is connected to Pin 4.

// Data wire is conntec to the Arduino digital pin 4
const int oneWireBus = 4;          
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

In the setup(), initialize the Serial Monitor at a baud rate of 9600.

Serial.begin(9600);

Initialize the DS18B20 temperature sensor:

sensors.begin();

In the loop() is where you’ll get the temperature. You need to call the requestTemperatures() method before getting the actual temperature value.

sensors.requestTemperatures(); 

Then, get and print the temperature in Celsius. To get the temperature in Celsius, use the getTempCByIndex() method :

Serial.print(sensors.getTempCByIndex(0)); 

Or use the getTempFByIndex() to get the temperature in Fahrenheit.https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.htmlReport this ad

Serial.println(sensors.getTempFByIndex(0));

The getTempCByIndex() and the getTempFByIndex() methods accept the index of the temperature sensor. Because we’re using just one sensor its index is 0. If you have more than one sensor, you use index 0 for the first sensor, index 1 for the second sensor, and so on.

New temperature readings are requested every second.

delay(5000);

Demonstration

After uploading the code, open the Arduino IDE Serial Monitor at a 9600 baud rate. You should get the temperature displayed in both Celsius and Fahrenheit:

Getting Temperature from Multiple DS18B20 Sensors

The DS18B20 temperature sensor communicates using one-wire protocol and each sensor has a unique 64-bit serial code, so you can read the temperature from multiple sensors using just one single Arduino digital Pin.

Schematic

To read the temperature from multiple sensors, you just need to wire all data lines together as shown in the following schematic diagram:

Upload Code – Multiple DS18B20

Then, upload the following code. It scans for all devices on Pin 4 and prints the temperature for each one. This sketch is based on the example provided by the DallasTemperature library.

/*
 * Gnd_To_Vcc
*/

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

// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

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

int numberOfDevices; // Number of temperature devices found

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

void setup(void) {
  // start serial port
  Serial.begin(9600);
  
  // Start up the library
  sensors.begin();
  
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
  
  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");

  // Loop through each device, print out address
  for(int i=0;i<numberOfDevices; i++) {
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Found device ");
      Serial.print(i, DEC);
      Serial.print(" with address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
		} else {
		  Serial.print("Found ghost device at ");
		  Serial.print(i, DEC);
		  Serial.print(" but could not detect address. Check power and cabling");
		}
  }
}

void loop(void) { 
  sensors.requestTemperatures(); // Send the command to get temperatures
  
  // Loop through each device, print out temperature data
  for(int i=0;i<numberOfDevices; i++) {
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i)){
		
		// Output the device ID
		Serial.print("Temperature for device: ");
		Serial.println(i,DEC);

    // Print the data
    float tempC = sensors.getTempC(tempDeviceAddress);
    Serial.print("Temp C: ");
    Serial.print(tempC);
    Serial.print(" Temp F: ");
    Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
    } 	
  }
  delay(5000);
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
  for (uint8_t i = 0; i < 8; i++) {
    if (deviceAddress[i] < 16) Serial.print("0");
      Serial.print(deviceAddress[i], HEX);
  }
}

How the code works

The code uses several useful methods to handle multiple DS18B20 sensors.

You use the getDeviceCount() method to get the number of DS18B20 sensors on the data line.

numberOfDevices = sensors.getDeviceCount();

The getAddress() method finds the sensors addresses:

if(sensors.getAddress(tempDeviceAddress, i)){

The address is unique for each sensor. So each sensor can be identified by its address.

Then, you use the getTempC() method that accepts as argument the device address. With this method you can get the temperature from a specific sensor:

float tempC = sensors.getTempC(tempDeviceAddress);

To get the temperature in Fahrenheit degrees, you can use the getTemF(). Alternatively, you can convert the temperature in Celsius to Fahrenheit as follows:

DallasTemperature::toFahrenheit(tempC)

Wrapping Up

The DS18B20 temperature sensor is a one-wire digital sensor. To use this sensor with the Arduino, you need the OneWire and the DallasTemperature libraries. You can use one sensor or multiple sensors on the same data line because you can identify each sensor by its unique address.

Now, you can take this project further and display your sensor readings in an OLED display, for example.

We have more tutorials for other Arduino compatible sensors that you may find useful:

We hope you’ve found this guide useful.

Thanks for reading!

Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored.

6 thoughts on “Guide for DS18B20 Temperature Sensor with Arduino

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: