Interfacing DS18B20 Temperature sensor with Arduino,ESP8266 and ESP32

In this quick tutorial, learn how to measure Realtime temperature using DS18B20 temperature sensor which can be found in two versions, normal transistor type version and waterproof version. You will also learn how to install required libraries and obtain the temperature values between -55ºC to +125ºC from one or multiple DS18B20 sensors.

DS18B20 Temperature sensor two versions

Overview of DS18B20 Temperature sensor

DS18B20 sensor manufactured by Dallas semiconductor Corp., uses only one data line (and GND) to communicate with the microcontroller thus it is also named as 1-Wire® digital temperature sensor. Thus is it easy to connect it with Arduino and other microcontrollers which takes only 1 GPIO pin as no other components are required except a 4.7k ohm resistor which is connected in series to data pin from 5V supply.

Power supply

normal and parasite mode ds18b20 temperature sensor

It can be powered in two methods one is through external supply through VCC ranges from 3V to 5.5V and other one is through the data line which is also called as parasite mode, in this mode the sensor VCC is connected to GND and the power is taken through the data pin. It consumes 1mA of current while temperature measurements.

1-Wire Connection interface

Every DS18B20 temperature sensor is manufactured with an unique 64 bit serial code which makes them easy to differentiate them when connected through single 1-wire bus i.e. multiple sensor data pins connected to single GPIO pin of Arduino or any microcontroller. Which makes it easy to measure the temperature values from multiple sensor through a single data bus.

Related article: Build Digital Thermometer using Arduino and lm35

Precision control

The Precision of the DS18B20 sensor can be configurable through the code as parameters 9,10,11 and 12 (bits) to get the precision 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively. By default it is set to 12 bits which the highest precision offered by the sensor. Can be adjusted with the help of setResolution() function whic takes the above parameters.

Technical Specifications:

  • Operating voltage     :  3V to 5.5V
  • Current consumption: 1mA
  • Precision                    :  0.5°C, 0.25°C, 0.125°C, and 0.0625°C (9,10,11and 12 bit)
  • Accuracy                    : ±0.5°C
  • conversion time         : <750ms

DS18B20 pinout diagram:

DS18B20 temperature sensor pinout diagramAs per the above pinout diagram there are two versions transistor type version and waterproof type version. and both have 3 pins, 2 pins for VCC and GND and Another for DATA,

In transistor type the pins are in following manner 1.GND, 2.DATA, 3.VCC.

In Waterproof version the wire colors represent pins, Red=VCC, Black =GND, Yellow = Data.

Required Libraries:

DS18B20 sensor requires two libraries to reduce the complexity in parsing the code.

  • DallasTemperature.h (Dallas Temperature by Miles Burton)
  • OneWire.h (One Wire by Paul Stoffregen)

You can download and install this libraries directly through Arduino IDE or manually from our libraries page.

To install directly from IDE follow the below steps:

  • Open Arduino IDE and select the Sketch dropdown from menu bar
  • Choose Include Library and Select Manage Libraries 
  • Now type the required library name in the search box, choose from the results and tap on install.
  • The library is installed successfully.

Interfacing DS18B20 Temperature sensor with Arduino

In this method we are connecting the DS18B20 sensor with Arduino Uno microcontroller.

Required components:

  • Arduino UNO
  • DS18B20 temperature sensor
  • 4.7K ohm resistor
  • Few connecting wires

Circuit diagram Interfacing DS18B20 Temperature sensor with Arduino

Circuit diagram of connecting DS18B20 Temperature sensor with ArduinoAs from the above schematic diagram the VCC and GND of sensor are connected to 5V and GND of Arduino, and DATA pin is connected to pin 7 of Arduino. A 4.7k Ohm pull up resister is connected to data pin from 5V supply for stable data communication.

After connecting all the required components according to the above circuit diagram, we need to upload the code into the Arduino through Arduino IDE.

/*********
CIRCUITSCHOOLS.COM
*********/
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is connected to digital pin 7 on the Arduino
#define ONE_WIRE_BUS 7

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

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

void setup(void)
{
  sensors.begin();	// Start up the library
  Serial.begin(9600);
}

void loop(void)
{ 
  // Send the command to get temperatures
  sensors.requestTemperatures(); 

  //print the temperature in Celsius
  Serial.print("Temperature: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print((char)176);//shows degrees character
  Serial.print("C  |  ");
  
  //print the temperature in Fahrenheit
  Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  Serial.print((char)176);//shows degrees character
  Serial.println("F");
  
  delay(500);
}

After pasting the Above code in Arduino IDE Choose the Board as Arduino and choose the COM Port and click on Upload.

After uploading the code The serial monitor will show the results as below image

You can display the temperature values on any display modules compatible with Arduino.

Refer: Interfacing SSD1306 OLED display with Arduino, ESP32 and ESP8266

Interfacing DS18B20 Temperature sensor with ESP8266

In this method we are connecting the DS18B20 sensor with ESP8266 microcontroller.

Required components:

  • ESP8266 (NodeMCU)
  • DS18B20 temperature sensor
  • 4.7K ohm resistor
  • Few connecting wires

Circuit diagram Interfacing DS18B20 Temperature sensor with ESP8266

Circuit diagram of connecting DS18B20 Temperature sensor with ESP8266 (NodeMCU)

As from the above schematic diagram the VCC and GND of sensor are connected to 3.3V and GND of ESP8266, and DATA pin is connected to pin D2 of ESP8266. A 4.7k Ohm pull up resister is connected to data pin from 3.3V supply for stable data communication.

After connecting all the required components according to the above circuit diagram, we need to upload the code into the Arduino through Arduino IDE.

Here everything in the code is same expect the GPIO pin number of the ESP8266 where data pin of sensor is connected.

#define ONE_WIRE_BUS 4 // as we connected it on D2 the GPIO of it is 4 (Check NodeMCU pinout)

After pasting the Above code in Arduino IDE Choose the Board as ESP8266 or NodeMCU and choose the COM Port and click on Upload.

After uploading the code The serial monitor will show the results same as the output of Arduino.

Interfacing multiple DS18B20 Temperature sensor with ESP32

In this method we are connecting multiple DS18B20 sensors with ESP32 microcontroller.

Required components:

  • ESP32
  • DS18B20 temperature sensor X 3 units
  • 4.7K ohm resistor
  • Few connecting wires

Circuit diagram Interfacing  multiple DS18B20 Temperature sensor with ESP32

Circuit diagram of connecting multiple DS18B20 Temperature sensors with ESP32

Here in this method we are connecting DS18B20 temperature sensors in parallel. i.e, connecting all the VCC pins of 3 sensors together, all GND pins of 3 sensors together and all the data signal pins of 3 sensors together

As from the above schematic diagram the VCC’s and GND’s of sensors are connected to 3.3V and GND of ESP32, and DATA pin is connected to pin D15 of ESP32. A 4.7k Ohms pull up resister is connected to data pin from 3.3V supply for stable data communication.

After connecting all the required components according to the above circuit diagram, we need to upload the code into the Arduino through Arduino IDE.

As we learnt from the above overview, each DS18B20 sensor is manufactured with unique 64 bit serial code to identify the difference, So first, we need to find these address of our 3 sensors using the below code.

To know exact address of individual sensors connect one by one and note the address of them.

Address Scanner

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

// Data wire is plugged into port 15 on the ESP32
#define ONE_WIRE_BUS 15

// 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(115200);

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

after uploading the code and the serial monitor will show the addresses as below image.

scanning for DS18B20 multiple sensor addressesCopy the sensor addresses to paste them on our main code.

Source code:

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

// Data wire is plugged into port 15 on the ESP32
#define ONE_WIRE_BUS 15

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

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

float tempSensor1, tempSensor2, tempSensor3;
 
void setup() {
  Serial.begin(115200);
  delay(100); 
  sensors.begin();              

}
void loop(void) {
  sensors.requestTemperatures();
  tempSensor1 = sensors.getTempC(sensor1); // Gets the values of the temperature
  tempSensor2 = sensors.getTempC(sensor2); // Gets the values of the temperature
  tempSensor3 = sensors.getTempC(sensor3); // Gets the values of the temperature
  
  // Printing the sensor values
  Serial.print("Sensor1: ");
  Serial.println(tempSensor1);
  Serial.print((char)176);//shows degrees character
  Serial.print("C | ");
  Serial.print("Sensor2: ");
  Serial.println(tempSensor2);
  Serial.print((char)176);//shows degrees character
  Serial.print("C | ");
  Serial.print("Sensor3: ");
  Serial.print(tempSensor3);
  Serial.print((char)176);//shows degrees character
  Serial.print("C | ");
  Serial.println("");
}

After uploading the code the serial monitor outputs the temperature values of 3 sensors as show in the

Interfacing multiple DS18B20 Temperature sensor with ESP32 outputAlso Read: Interfacing SHT3x Temperature and Humidity sensor with Arduino

Add a comment

Leave a Reply

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