One of the DS18B20’s features is that multiple DS18B20s can coexist on the same 1-Wire bus. Because each DS18B20 is pre-programmed with a unique 64-bit serial code, they can be distinguished from one another.

The following tutorial will show you how to read the temperature from multiple DS18B20s connected to the same bus. This feature can be extremely useful when you need to control multiple DS18B20s spread across a large area.

Before proceeding with this tutorial, you should be familiar with the basics of the DS18B20 one-wire temperature sensor. Please consider reading the tutorial below first.

Without further ado, let’s connect the DS18B20s to our Arduino.

Wiring Multiple DS18B20 Sensors to Arduino

Connections are pretty straightforward.

Begin by connecting all of the DS18B20s in parallel, sharing all of the VDD, GND, and signal pins. Then connect VDD to the Arduino’s 5V output, GND to the Arduino’s ground, and signal pin to digital pin 2 on the Arduino.

To keep the data transfer stable, add one 4.7k pull-up resistor for the entire bus between the signal and power pin (Note: internal pull-ups on the Arduino do not work here).

Wiring Multiple DS18B20 Temperature Sensors With Arduino
Wiring Multiple DS18B20 Temperature Sensors With Arduino

Installing Library For DS18B20

The 1-Wire protocol is a bit complicated, and it takes a lot of code to make it work. DallasTemperature.h was written to abstract away this unnecessary complexity, allowing us to issue simple commands to obtain temperature readings from the sensor.

To install the library, navigate to Sketch > Include Library > Manage Libraries… Wait for the Library Manager to download the library index and update the list of installed libraries.

Arduino Library Installation - Selecting Manage Libraries in Arduino IDE

Filter your search by entering ‘ds18b20’. There should be a couple of entries. Look for DallasTemperature by Miles Burton. Click on that entry and then choose Install.

Installing Dallas Temperature Library In Arduino IDE

Dallas Temperature is a hardware-specific library that handles lower-level functions. It must be paired with the One Wire Library in order to communicate with any one-wire device, not just the DS18B20. Install this library as well.

Installing OneWire Library In Arduino IDE

Methods for Reading Multiple DS18B20s

There are two methods for reading multiple DS18B20s connected to the same bus.

  • By Index – In this method, we use an index (assigned by the library) to pinpoint each sensor and read its temperature.
  • By Address – In this method, we read each sensor individually by taking advantage of the fact that each sensor has its own 64-bit address (preprogrammed).

Let’s go over each method one by one.

Method 1: Reading DS18B20 By Index

When the Dallas Temperature library is initialized, it scans the bus for all sensors that share the same bus. It considers the entire bus to be an array of sensors and assigns each one an index. As a result, we can identify each sensor by its index and read temperature.

Here is a simple sketch that locates all the DSB1820s on the bus and then shows the temperature of each one.

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

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

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

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
float tempC;

void setup(void)
{
  sensors.begin();	// Start up the library
  Serial.begin(9600);
  
  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");
}

void loop(void)
{ 
  // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures(); 
  
  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    tempC = sensors.getTempCByIndex(i);
    Serial.print(tempC);
    Serial.print((char)176);//shows degrees character
    Serial.print("C  |  ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
    Serial.print((char)176);//shows degrees character
    Serial.println("F");
  }
  
  Serial.println("");
  delay(1000);
}

The result looks like this:

Output of Multiple DS18B20 - By Index Method

Code Explanation:

The sketch begins by including libraries, declaring the pin to which the sensor bus is connected, and creating a DallasTemperature library object. In the same global scope, two variables are defined: deviceCount stores the number of sensors found on the bus, and tempC stores the temperature read from each sensor.

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

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

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

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
float tempC;

In the setup section of the code, we first call the begin() function. It initializes the bus and detects all connected DS18B20s. Each sensor is then assigned an index, and the bit resolution is set to 12-bit.

The getDeviceCount() function is then called to print the number of devices discovered on the bus.

void setup(void)
{
  sensors.begin();	// Start up the library
  Serial.begin(9600);
  
  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");
}

In the looping section of the code, we use the requestTemperatures() function to instruct all sensors on the bus to perform a temperature conversion.

Finally, we use a simple for loop to iterate through the array of sensors and read the temperature of a specific DS18B20 by calling the function getTempCByIndex(i)

void loop(void)
{ 
  // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures(); 
  
  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    tempC = sensors.getTempCByIndex(i);
    Serial.print(tempC);
    Serial.print((char)176);//shows degrees character
    Serial.print("C  |  ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
    Serial.print((char)176);//shows degrees character
    Serial.println("F");
  }
  
  Serial.println("");
  delay(1000);
}

Method 2: Reading DS18B20 By Address

Each DS18B20 is programmed with a unique 64-bit address at the time of manufacture, allowing them to be distinguished from one another. So we’ll use one sketch to find and record the address of each sensor, and then another sketch to read each sensor individually.

Finding the Addresses of DS18B20s on a Bus

The sketch below detects all DS18B20s on the bus and prints their one-wire addresses on the serial monitor.

It is recommended that you connect only one sensor at a time so that you can find each sensor’s address and assign it a name.

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

// Data wire is plugged into port 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);

// variable to hold device addresses
DeviceAddress Thermometer;

int deviceCount = 0;

void setup(void)
{
  // start serial port
  Serial.begin(9600);

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

  // locate devices on the bus
  Serial.println("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");
  
  Serial.println("Printing addresses...");
  for (int i = 0;  i < deviceCount;  i++)
  {
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    sensors.getAddress(Thermometer, i);
    printAddress(Thermometer);
  }
}

void loop(void)
{}

void printAddress(DeviceAddress deviceAddress)
{ 
  for (uint8_t i = 0; i < 8; i++)
  {
    Serial.print("0x");
    if (deviceAddress[i] < 0x10) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
    if (i < 7) Serial.print(", ");
  }
  Serial.println("");
}

Now, open up Serial Monitor. You’ll get to see something like this.

Finding One-Wire Address Of All DS18B20 On the Bus

Make a note of all the addresses because we’ll need them in the next sketch.

Reading DS18B20s By Address

The sketch below uses the addresses of the DS18B20s to read the temperatures. Before you begin uploading the sketch, you must replace the addresses of the DS18B20s with the ones found in the previous sketch.

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

// Data wire is plugged into port 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);

// Addresses of 3 DS18B20s
uint8_t sensor1[8] = { 0x28, 0xEE, 0xD5, 0x64, 0x1A, 0x16, 0x02, 0xEC };
uint8_t sensor2[8] = { 0x28, 0x61, 0x64, 0x12, 0x3C, 0x7C, 0x2F, 0x27 };
uint8_t sensor3[8] = { 0x28, 0x61, 0x64, 0x12, 0x3F, 0xFD, 0x80, 0xC6 };

void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
}

void loop(void)
{
  sensors.requestTemperatures();
  
  Serial.print("Sensor 1: ");
  printTemperature(sensor1);
  
  Serial.print("Sensor 2: ");
  printTemperature(sensor2);
  
  Serial.print("Sensor 3: ");
  printTemperature(sensor3);
  
  Serial.println();
  delay(1000);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(tempC);
  Serial.print((char)176);
  Serial.print("C  |  ");
  Serial.print(DallasTemperature::toFahrenheit(tempC));
  Serial.print((char)176);
  Serial.println("F");
}

The output of the above sketch looks like:

Output of Multiple DS18B20 - By Address Method

Code Explanation:

The sketch begins as usual by including libraries, declaring the pin to which the sensor bus is connected, and creating an object of the DallasTemperature library.

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

// Data wire is plugged into port 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);

Next, we enter the previously discovered addresses for each temperature sensor.

uint8_t sensor1[8] = { 0x28, 0xEE, 0xD5, 0x64, 0x1A, 0x16, 0x02, 0xEC };
uint8_t sensor2[8] = { 0x28, 0x61, 0x64, 0x12, 0x3C, 0x7C, 0x2F, 0x27 };
uint8_t sensor3[8] = { 0x28, 0x61, 0x64, 0x12, 0x3F, 0xFD, 0x80, 0xC6 };

In the setup section, we initialize serial communication with the PC and call the begin() function. The begin() function initializes the bus, detects all connected DS18B20s, and sets the bit resolution for each to 12-bit.

void setup(void) {
  Serial.begin(9600);
  sensors.begin();
}

In the looping section of the code, we use the requestTemperatures() function to instruct all sensors on the bus to perform a temperature conversion.

Finally, we call the custom function printTemperature(deviceAddress) to print the temperature of the sensor whose deviceAddress is passed as a parameter.

void loop(void)
{
  sensors.requestTemperatures();
  
  Serial.print("Sensor 1: ");
  printTemperature(sensor1);
  
  Serial.print("Sensor 2: ");
  printTemperature(sensor2);
  
  Serial.print("Sensor 3: ");
  printTemperature(sensor3);
  
  Serial.println();
  delay(1000);
}

The custom function printTemperature() simply calls the library’s getTempC(deviceAddress) function to display temperature in Celsius and DallasTemperature::toFahrenheit() function to display temperature in Fahrenheit.

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(tempC);
  Serial.print((char)176);
  Serial.print("C  |  ");
  Serial.print(DallasTemperature::toFahrenheit(tempC));
  Serial.print((char)176);
  Serial.println("F");
}

Login
ADS CODE