Tutorials

Interfacing DS18B20 Temperature sensor with Arduino,ESP8266 and ESP32

Learn how to control DS18B20 temperature sensor with Arduino and few other microcontrollers to measure Realtime temperature.

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.

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.

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.

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

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:

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 output

CircuitSchools Staff

We at CircuitSchools publish on basics of electronics and electric components and everything related to evolution of electrical technology with complete analysis of development boards and modules along with latest projects with innovative ideas.

Related Articles

Back to top button