ESP32 – connection with DS18B20 temperature sensor

Reading time: 2 min.

ESP32 module has gained its popularity due to its small size, huge capabilities, wireless communication and light price. It allows you to create unlimited IoT projects using various types of sensors and other actuators. ESP is also used in smart home systems, where it is visible as a node. Thanks to WiFi communication, there is no need to run additional wires to the control panel, as data is sent over the wireless network. In this tutorial, we will connect the ESP32 module to a popular DS18B20 temperature sensor, and display the sensor data on a serial monitor and in a web browser.

Shopping list

From the title it is easy to deduce that we will need an ESP32 module and a DS18B20 temperature sensor to develop the project. In our store you can find a ready-made kit with useful sensors and modules to get started with ESP32, individual components can also be purchased separately.

The full list of products can be found below:

Connection of DS18B20 sensor with ESP32

The sensor can be connected to any ESP32 digital pin. Our chosen connection will lead to pin D4 of the ESP module. Place resistor between temperature sensor’s digital pin (the center one – DQ) and the power supply.

 width=
 width=

Preparation of Arduino IDE for ESP32 module

The ESP32 module can be easily programmed with the Arduino IDE environment. To do this, you need to upload the appropriate package. To upload ESP32 boards, open Arduino IDE, then go to File -> Preferences and in the Additional Boards Manager URLs to the board manager field, paste the link:

https://dl.espressif.com/dl/package_esp32_index.json

and press the OK button.


 width=

The next step is to install the ESP32 board. Go to Tools -> Boards -> Boards Manager and in the box type “ESP32” and press the Install button.


 width=

Sometimes the drivers for the CP210x USB to UART converter chip may be missing from your system. You have to install them manually in Windows Device Manager.

Installing relevant Arduino libraries

The next step is to install the appropriate libraries for the DS18B20 sensor. The sensor uses the OneWire by Paul Stoffregen and Dallas temperature libraries. The libraries have to be installed according to the following steps:

1) Open the Arduino IDE environment and go to Draft -> Attach Library -> Manage Libraries and type in “onewire”, then search for OneWire by Paul Stroffregen libraries and click install.


 width=

2. The next required library is DallasTemperature by Miles Burton. Type “dallas” into search engine and install the Dallas library.


 width=

After installing the libraries, restart the Arduino IDE environment.

ESP32 programming

After installing all the necessary libraries, you can program your ESP32. The following code allows you to simply read temperature values and display them in the Arduino IDE serial monitor.


 width=

Once the code is uploaded to the board, press the Serial Monitor button to view the temperature measurement results:

 width=

Serial monitor should display the measurement result. The measurement will be repeated every 5 seconds (5000 ms).

Web server temperature reading

The ESP32 module uses WiFi, so the temperature can be read remotely using a web browser. To do this, you need to install an additional library: ESPAsyncWebServer, which allows to easily create a web server.

  1. Download the ESPAsyncWebServer library.
  2. Upload the library by going to Draft -> Attach Library -> Attach .ZIP Library and search for the downloaded ZIP package. Then press the Open button.

Next required  library is the Async TCP Library. Install it according to the steps below:

  1. Download the Async TCP library.
  2. Upload the library by going to Draft -> Attach Library -> Attach .ZIP Library and search for the downloaded ZIP package. Then press the Open button.

Code for DS18B20 Async Web Server

Open the Arduino IDE and paste the code below. In the code, enter the SSID of your home WiFi router and the password.


 width=

All set, ready to go! More ESP32 articles coming soon – be sure to check us out at Botland Blog.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Share:

Picture of Grzegorz Galuba

Grzegorz Galuba

He doesn’t have time for chit-chat - Grzegorz is always up to date with all of the new products from Botland’s offer. He chooses only the best ones and makes sure they appear on time. His extensive knowledge about technical specification of our products is a priceless support to our whole team. He rides a bike to work and it’s high time everybody followed his example. Oasis of peace.

See more:

Leave a Reply

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

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.