Interfacing Multiple DS18B20 Digital Temperature Sensors with Arduino

The advantage of DS18B20 is that multiple DS18B20 can use on the same 1-Wire bus. Each DS18B20 has its unique 64-bit serial code, it’s easier to differentiate from one another.
This tutorial shows how to interface multiple DS18B20 on a single bus & get temperature readings from each of them. This feature provides us easier way to control many DS18B20s distributed over large area.

Wiring Multiple DS18B20 Sensors to Arduino

Let’s start doing connection. Connection is very simple.
Frist connect all the DS18B20s in parallel. Connect all the GND and VDD to Arduino GND and 5V pins. Connect signal pin to Arduino pin # 2. Now we add one 4.7k pull up resistor to make stable connection for data transfer.

Installing Library For DS18B20

The Dallas 1-Wire protocol is complex, and requires some code to do the communication. To hide this complexity we will install DallasTemperature.h library so that we can use simple commands to get temperature readings from the sensor.
To install the library click on this link and download the library https://github.com/matmunk/DS18B20.
After downloading the library go to the Sketch > Include Library > Add .ZIP library. Wait to install the library.

Method 1: Reading DS18B20 by Index

In this method, Dallas Temperature library detects all the sensors sharing the same bus. It manage the whole bus as an array of sensors. And assigns them an index. So that we can identify each sensor by its index and read temperature.

#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(DallasTemperaturetoFahrenheit(tempC));
    Serial.print((char)176);shows degrees character
    Serial.println(F);
  }
  
  Serial.println();
  delay(1000);
}

The output on serial monitor

Code Explanation:

Frist we include libraries. After including libraries, declare pin to which sensors is used and create object for DallasTemperature library.
In void setup part, we first call begin() function. It initializes the bus and detects all the DS18B20s present on it. After that each sensor assigned with an index and set bit resolution to 12-bit.
After that, we call getDeviceCount() function to get the number of devices found on the bus.
In void loop part, we use requestTemperatures() function to send command to all the sensors for temperature conversion.
Next, using a simple for(int i = 0; i < deviceCount; i++) loop we can iterate through the array of sensors and read temperature of DS18B20 at an index i by simply calling getTempCByIndex(i).

Method 2: Reading DS18B20 By Address

Each DS18B20 has a unique 64-bit address assigned to it to differentiate them from one another. In this method, we’ll find that address to label each sensor consistently. These address can be used to read data from each sensor individually.

Finding Addresses Of DS18B20s On Bus

The following code detects all the DS18B20s present on the bus and prints their one-wire address on the serial monitor.
You can wire just one sensor at a time to find its address (or successively add a new sensor) so that you’re able to identify each one by its address. Then, you can label each sensor.

#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("");
}

The output on serial monitor

Reading DS18B20s by Address

The following code reads the temperature from DS18B20s by their addresses. Before uploading the code, you need to change the addresses of DS18B20s with the one that you have found in previous code.

#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 on serial monitor

Code Explanation:

As usual, first we include libraries. Then declaring pin to which sensor bus is connected and creating object of DallasTemperature library.
After that, we enter the addresses that we found previously for each temperature sensor. In our case, we have the following.
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 void setup part, we call begin() function to initialize the library and initialize serial communication with PC.
In void loop part, we use requestTemperatures() function to send command to all sensors for temperature conversion.
We then call printTemperature(DeviceAddress deviceAddress) custom function to print the temperature of the sensor whose deviceAddress is passed as parameter.
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”);
}

°

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
YouTube
× Contact us