How To Interface DS18B20 Temperature Sensor with ESP32?
This tutorial will teach us how to connect a DS18B20 to an ESP32 DevKit Development Board. The DS18B20 is a great option for the temperature sensor if you wish to develop an ESP32-based Web-based Temperature Monitoring System. Learn how to set up the Arduino IDE, show the temperature on an LCD, and use the ESP32 DS18B20 Temperature Sensor interface. A straightforward ESP32 Web Server that continuously displays the temperature is another option.
DS18B20 Temperature Sensor
DS18B20 Temperature Sensor was previously utilized in a few projects that involved Arduino, Raspberry Pi, and ESP8266. The digital Temperature Sensor DS18B20 from Maxim Integrated is capable of measuring temperatures between -550C and +1250C. [Projects for Novice ESP32 Users]
The DS18B20 Temperature Sensor employs 1-Wire Communication to interact with a Microcontroller, in contrast to other digital sensors, which frequently communicate using I2C or SPI.
If you are familiar, 1-Wire bus is a type of communication bus created by Dallas Semiconductor that, as its name suggests, only uses one wire to connect to processors (well, 1 Data Line and 1 GND Wire).
In addition, the DS18B20 Temperature Sensor may receive power directly through the data cable (known as Parasite Power), negating the need for external power (through the VDD Pin).
As a digital temperature sensor, the DS18B20 stores temperature information in its on-chip EEPROM. Other crucial characteristics of the DS18B20 include:
- User configurable resolution between 9-bits and 12-bits.
- A 64-bit Serial Code is unique to each DS18B20 Sensor.
Pin Diagram and Pin Description of DS18B20
The pin diagram for the DS18B20 is shown in the next image, and its pin descriptions are shown in the table that follows.
| Pin of DS18B20 | Description |
| GND | Ground |
| DQ | Input / Output Data. Must be pulled HIGH. |
| VDD | Power Supply |
Different Ways to Power DS18B20
There are a couple of ways in which you can power up DS18B20:
- Normal Way (using VDD)
- Parasite Power
ESP32 DS18B20 Interface
After learning a little bit more about the DS18B20, let’s move on to learning how to connect the DS18B20 to the ESP32. Remember that the DS18B20 is a digital sensor first. The utilisation of 1-Wire Communication is the next critical point to keep in mind.
How does this affect the ESP32? This indicates that we just need one wire for effective communication and may utilise any Digital GPIO Pin of the ESP32 to send and receive data to and from the DS18B20.
Components Required
- ESP32 DevKit Development Board
- DS18B20 Temperature Sensor
- 16×2 LCD
- PCF8574 I2C LCD Module
- Breadboard
- Connecting Wires
- Micro USB Cable
Circuit Diagram
The connections between the ESP32 and the DS18B20 are depicted in the next image. First, I’m using regular power to power the DS18B20. Therefore, the ESP32 Board’s VIN is connected to the DS18B20’s VDD.
NOTE: The DS18B20’s power supply range is 3 V to 5.5 V. Therefore, you can also power DS18B20 using a 3.3V supply from an ESP32 board.
The GND pin is then linked to any ESP32 GND pin. The DQ Pin comes last. To begin with, this pin needs to be pulled HIGH. I therefore wired a 4.7 K resistor between the DQ Pin and 3.3V.
NOTE: The pullup voltage ranges from 3 to 5.5 volts.
GPIO 16 of the ESP32, also known as RX2 on the ESP32 DevKit Board, is now where the DQ pin is linked.
Preparing Arduino IDE
To fully communicate with DS18B20, you must download a few libraries. The 1-Wire Bus is mentioned in the first. In the Arduino IDE, select Tools -> Manage Libraries…
Type “onewire” into the search box. Install the “OneWire” library created by Jim Studt, Paul Stoffregan, and others by scrolling through the options.
The DS18B20 device itself is connected to the following library. Install “DallasTemperature” by Miles Burton et al. by searching for “dallas.”
Displaying Temperature on Serial Monitor
We will now examine how to use the ESP32 to read the temperature data from the DS18B20 and display the result on the Serial Monitor after properly setting up the connections and loading the required libraries as previously indicated.
Code
| Â | #include <OneWire.h> |
| Â | #include <DallasTemperature.h> |
| Â | Â |
| Â | #define DS18B20PIN 16 |
| Â | Â |
| Â | /* Create an instance of OneWire */ |
| Â | OneWire oneWire(DS18B20PIN); |
| Â | Â |
| Â | DallasTemperature sensor(&oneWire); |
| Â | Â |
| Â | void setup() |
| Â | { |
| Â | Serial.begin(115200); |
| Â | /* Start the DS18B20 Sensor */ |
| Â | sensor.begin(); |
| Â | } |
| Â | Â |
| Â | void loop() |
| Â | { |
| Â | sensor.requestTemperatures(); |
| Â | float tempinC = sensor.getTempCByIndex(0); |
| Â | Serial.print(“Temperature = “); |
| Â | Serial.print(tempinC); |
|  | Serial.println(“ºC”); |
| Â | delay(3000); |
| Â | } |
The Serial Monitor, which continuously prints the temperature information every three seconds, is screenshotted in the following image.
ESP32 DS18B20 with I2C LCD
The only use for displaying temperature values on Serial Monitor is to test the connections and the code. The simplest method for viewing the temperature reading on a “Embedded System” is to use a display module of any kind (OLED, 162 Character LCD, Nokia 5110 LCD, graphical LCD, etc.).
To display the temperature values from the ESP32 DS18B20 Interface, I combined a standard 162 Character LCD Display Module with a PCF8574 I2C LCD Module to keep things simple.
I created a specific guide on using an I2C LCD with the ESP32. For further details, see that tutorial. In that lesson, I also covered the required libraries that you must download in order to effectively connect an I2C LCD to an ESP32.
NOTE: In that tutorial, I also covered how to find the I2C LCD module’s slave address. This action is crucial.
Circuit Diagram
Two further parts are needed: a 162 LCD display and an I2C LCD module (based on PCF8574). Connect the I2C LCD Module to the 162 LCD Display’s back. Only four connections are necessary for the I2C LCD Module (two of them are for power and two are for data).
The following circuit diagram shows each of the required connections between the ESP32 and the I2C LCD Module and the DS18B20 Temperature Sensor.
Code
The ESP32 DS18B20 Interface with I2C LCD code is fairly straightforward. The sensor’s initialization phase resembles the earlier code. The only additional code is for the LCD.
| Â | #include <OneWire.h> |
| Â | #include <DallasTemperature.h> |
| Â | #include <Wire.h> |
| Â | #include <LiquidCrystal_I2C.h> |
| Â | Â |
| Â | #define DS18B20PIN 16 |
| Â | Â |
| Â | /* Create an instance of OneWire */ |
| Â | OneWire oneWire(DS18B20PIN); |
| Â | Â |
| Â | DallasTemperature sensor(&oneWire); |
| Â | Â |
| Â | LiquidCrystal_I2C lcd(0x3F, 16, 2); |
| Â | Â |
| Â | byte degree_symbol[8] = |
| Â | Â Â Â Â Â Â { |
| Â | Â Â Â Â Â Â Â 0b00111, |
| Â | Â Â Â Â Â Â Â 0b00101, |
| Â | Â Â Â Â Â Â Â 0b00111, |
| Â | Â Â Â Â Â Â Â 0b00000, |
| Â | Â Â Â Â Â Â Â 0b00000, |
| Â | Â Â Â Â Â Â Â 0b00000, |
| Â | Â Â Â Â Â Â Â 0b00000, |
| Â | Â Â Â Â Â Â Â 0b00000 |
| Â | Â Â Â Â Â Â }; |
| Â | Â |
| Â | void setup() |
| Â | { |
| Â | /* Start the DS18B20 Sensor */ |
| Â | sensor.begin(); |
| Â | lcd.init(); |
| Â | lcd.backlight(); |
| Â | lcd.createChar(0, degree_symbol); |
| Â | lcd.setCursor(0,0); |
| Â | lcd.print(” DS18B20Â with “); |
| Â | lcd.setCursor(0,1); |
|  | lcd.print(” ESP32 DevKit “); |
| Â | delay(2000); |
| Â | lcd.clear(); |
| Â | } |
| Â | Â |
| Â | void loop() |
| Â | { |
| Â | sensor.requestTemperatures(); |
| Â | float tempinC = sensor.getTempCByIndex(0); |
| Â | lcd.setCursor(0,0); |
| Â | lcd.print(“Temp = “); |
| Â | lcd.print(tempinC); |
| Â | lcd.write(0); |
| Â | lcd.print(“C”); |
| Â | delay(1000); |
| Â | } |
Conclusion
I hope all of you understand how to interface the DS18B20 Temperature sensor with ESP32 DevKit Development Board. We MATHA ELECTRONICS will be back soon with more informative blogs soon.