{"id":86576,"date":"2019-07-14T10:28:44","date_gmt":"2019-07-14T10:28:44","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=86576"},"modified":"2025-03-15T11:53:01","modified_gmt":"2025-03-15T11:53:01","slug":"esp32-ds18b20-temperature-arduino-ide","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-ds18b20-temperature-arduino-ide\/","title":{"rendered":"ESP32 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)"},"content":{"rendered":"\n<p>This is a in-depth guide for the DS18B20 temperature sensor with ESP32 using Arduino IDE. We&#8217;ll show you how to wire the sensor, install the required libraries, and write the code to get the sensor readings from one and multiple sensors. Finally, we&#8217;ll build a simple web server to display the sensor readings.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with DS18B20 Temperature Sensor using Arduino IDE\" class=\"wp-image-87101\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>You might also like reading other DS18B20 guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide\/\">ESP8266 DS18B20 Temperature Sensor with Arduino IDE<\/a><\/li>\n\n\n\n<li><span style=\"font-size: inherit; background-color: initial;\"><a href=\"https:\/\/randomnerdtutorials.com\/micropython-ds18b20-esp32-esp8266\/\">ESP32\/ESP8266 DS18B20 Temperature Sensor with MicroPython<\/a><\/span><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-multiple-ds18b20-temperature-sensors\/\">ESP32 with Multiple DS18B20 Temperature Sensors<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-ds18b20-temperature-sensor-with-arduino\/\">DS18B20 Temperature Sensor with Arduino<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing DS18B20 Temperature Sensor<\/h2>\n\n\n\n<p>The <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\">DS18B20 temperature sensor<\/a> is a one-wire digital temperature sensor. This means that it just requires one data line (and GND) to communicate with your ESP32.<\/p>\n\n\n\n<p>It can be powered by an external power supply or it can derive power from the data line (called &#8220;parasite mode&#8221;), which eliminates the need for an external power supply.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"490\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-tempeature-sensor-pinout.jpg?resize=750%2C490&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor Pinout Pins\" class=\"wp-image-86735\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-tempeature-sensor-pinout.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-tempeature-sensor-pinout.jpg?resize=300%2C196&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Each DS18B20 temperature sensor has a unique 64-bit serial code. This allows you to wire multiple sensors to the same data wire. So, you can get temperature from multiple sensors using just one GPIO.<\/p>\n\n\n\n<p>The DS18B20 temperature sensor is also available in <a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\">waterproof version<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20-waterproof.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor Waterproof version\" class=\"wp-image-86674\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20-waterproof.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20-waterproof.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Here&#8217;s a summary of the most relevant specs of the DS18B20 temperature sensor:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Communicates over one-wire bus communication<\/li>\n\n\n\n<li>Power supply range: 3.0V to 5.5V<\/li>\n\n\n\n<li>Operating temperature range: -55\u00baC to +125\u00baC<\/li>\n\n\n\n<li>Accuracy +\/-0.5 \u00baC (between the range -10\u00baC to 85\u00baC)<\/li>\n<\/ul>\n\n\n\n<p>For more information consult the <a href=\"https:\/\/datasheets.maximintegrated.com\/en\/ds\/DS18B20.pdf\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">DS18B20 datasheet<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To follow this tutorial you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32<\/a> (read\u00a0<a aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best ESP32 development boards<\/a>)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">DS18B20 temperature sensor<\/a> (one or multiple sensors) \u2013\u00a0<a aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">waterproof version<\/a><\/li>\n\n\n\n<li><a aria-label=\"4.7k Ohm resistor\u2028 (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">4.7k Ohm resistor<\/a><\/li>\n\n\n\n<li><a aria-label=\"Jumper wires (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li>\n\n\n\n<li><a aria-label=\"Breadboard (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic &#8211; ESP32<\/h2>\n\n\n\n<p>As mentioned previously, the DS18B20 temperature sensor can be powered through the VDD pin (<strong>normal mode<\/strong>), or it can derive its power from the data line (<strong>parasite mode<\/strong>). You can choose either mode.<\/p>\n\n\n\n<p>If you&#8217;re using an ESP32 follow one of these two schematic diagrams.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parasite Mode<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_parasite_bb.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"816\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_parasite_bb.png?resize=750%2C816&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor with ESP32 Parasite Mode Wiring Schematic Diagram\" class=\"wp-image-86564\" style=\"width:563px;height:612px\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_parasite_bb.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_parasite_bb.png?resize=276%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 276w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Normal Mode<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_normal.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"816\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_normal.png?resize=750%2C816&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor with ESP32 Normal Mode Wiring Schematic Diagram\" class=\"wp-image-86565\" style=\"width:563px;height:612px\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_normal.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_normal.png?resize=276%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 276w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Preparing Your Arduino IDE<\/h2>\n\n\n\n<p>We\u2019ll program the ESP32 using Arduino IDE, so make sure you have the ESP32 add-on installed before proceeding:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Install ESP32 Board in Arduino IDE (Windows, Mac OS X, and Linux instructions)<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Libraries<\/h3>\n\n\n\n<p>To interface with the DS18B20 temperature sensor, you need to install the&nbsp;<a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/PaulStoffregen\/OneWire\" target=\"_blank\">One Wire library by Paul Stoffregen<\/a>&nbsp;and the&nbsp;<a href=\"https:\/\/github.com\/milesburton\/Arduino-Temperature-Control-Library\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Dallas Temperature library<\/a>. Follow the next steps to install those libraries.<\/p>\n\n\n\n<p>1. Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p>2. Type \u201c<strong>onewire<\/strong>\u201d in the search box and install OneWire library by Paul Stoffregen.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install OneWire library by Paul Stoffregen in Arduino IDE\" class=\"wp-image-86581\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n<p>3. Then, search for &#8220;<strong>Dallas<\/strong>&#8221; and install DallasTemperature library by Miles Burton.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?resize=787%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install DallasTemperature library by Miles Burton in Arduino IDE\" class=\"wp-image-86582\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?w=787&amp;quality=100&amp;strip=all&amp;ssl=1 787w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure><\/div>\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code (Single DS18B20)<\/h2>\n\n\n\n<p>After installing the required libraries, you can upload the code to the ESP32. The following code reads temperature from the DS18B20 temperature sensor and displays the readings on the Arduino IDE Serial Monitor.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com  \n*********\/\n\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 4;     \n\n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\n\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);\n\nvoid setup() {\n  \/\/ Start the Serial Monitor\n  Serial.begin(115200);\n  \/\/ Start the DS18B20 sensor\n  sensors.begin();\n}\n\nvoid loop() {\n  sensors.requestTemperatures(); \n  float temperatureC = sensors.getTempCByIndex(0);\n  float temperatureF = sensors.getTempFByIndex(0);\n  Serial.print(temperatureC);\n  Serial.println(&quot;\u00baC&quot;);\n  Serial.print(temperatureF);\n  Serial.println(&quot;\u00baF&quot;);\n  delay(5000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_DS18B20_Single.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>There are many different ways to get the temperature from DS18B20 temperature sensors. However, if you&#8217;re using just one single sensor, this is one of the easiest and simplest ways.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-DS18B20.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Single OneWire Temperature Sensor with ESP32 and Arduino IDE\" class=\"wp-image-86678\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-DS18B20.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-DS18B20.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Start by including the <span class=\"rnthl rntliteral\">OneWire<\/span> and the <span class=\"rnthl rntliteral\">DallasTemperature<\/span> libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;<\/code><\/pre>\n\n\n\n<p>Create the instances needed for the temperature sensor. The temperature sensor is connected to <span class=\"rnthl rntclblue\">GPIO 4<\/span>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 4;\n\n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\n\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the DS18B20 temperature sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.begin();<\/code><\/pre>\n\n\n\n<p>Before actually getting the temperature, you need to call the <span class=\"rnthl rntliteral\">requestTemperatures()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.requestTemperatures(); <\/code><\/pre>\n\n\n\n<p>Then, get the temperature in Celsius by using the <span class=\"rnthl rntliteral\">getTempCByIndex()<\/span> method as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float temperatureC = sensors.getTempCByIndex(0);<\/code><\/pre>\n\n\n\n<p>Or use the <span class=\"rnthl rntliteral\">getTempFByIndex()<\/span> to get the temperature in Fahrenheit.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float temperatureF = sensors.getTempFByIndex(0);<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">getTempCByIndex()<\/span> and the <span class=\"rnthl rntliteral\">getTempFByIndex()<\/span> methods accept the index of the temperature sensor. Because we&#8217;re using just one sensor its index is 0. If you want to read more than one sensor, you use index 0 for one sensor, index 1 for other sensor and so on.<\/p>\n\n\n\n<p>Finally, print the results in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(temperatureC);\nSerial.println(\"\u00baC\");\nSerial.print(temperatureF);\nSerial.println(\"\u00baF\");<\/code><\/pre>\n\n\n\n<p>New temperature readings are requested every 5 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(5000);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After uploading the code, you should get your sensor readings displayed in the Serial Monitor:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"661\" height=\"445\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20-temperature-sensor-readings-on-arduino-ide-1.png?resize=661%2C445&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature readings in Arduino IDE Serial Monitor\" class=\"wp-image-87079\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20-temperature-sensor-readings-on-arduino-ide-1.png?w=661&amp;quality=100&amp;strip=all&amp;ssl=1 661w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20-temperature-sensor-readings-on-arduino-ide-1.png?resize=300%2C202&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 661px) 100vw, 661px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Temperature from Multiple DS18B20 Temperature Sensors<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/multiple-ds18b20.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Multiple Temperature Sensors OneWire with ESP32 and Arduino IDE\" class=\"wp-image-86680\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/multiple-ds18b20.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/multiple-ds18b20.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The DS18B20 temperature sensor communicates using one-wire protocol and each sensor has a unique 64-bit serial code, so you can read the temperature from multiple sensors using just one single GPIO. You just need to wire all data lines together as shown in the following schematic diagram:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_multiple.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"561\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_multiple.png?resize=750%2C561&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Multiple Temperature Sensors with ESP32 Wiring Schematic Diagram\" class=\"wp-image-86569\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_multiple.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_multiple.png?resize=300%2C224&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Code (Multiple DS18B20s)<\/h3>\n\n\n\n<p>Then, upload the following code. It scans for all devices on <span class=\"rnthl rntclblue\">GPIO 4<\/span> and prints the temperature for each one. (This sketch is based on an example provided by the DallasTemperature library).<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com  \n*********\/\n\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ Data wire is plugged TO GPIO 4\n#define ONE_WIRE_BUS 4\n\n\/\/ Setup a oneWire instance to communicate with any OneWire devices (not just Maxim\/Dallas temperature ICs)\nOneWire oneWire(ONE_WIRE_BUS);\n\n\/\/ Pass our oneWire reference to Dallas Temperature. \nDallasTemperature sensors(&amp;oneWire);\n\n\/\/ Number of temperature devices found\nint numberOfDevices;\n\n\/\/ We'll use this variable to store a found device address\nDeviceAddress tempDeviceAddress; \n\nvoid setup(){\n  \/\/ start serial port\n  Serial.begin(115200);\n  \n  \/\/ Start up the library\n  sensors.begin();\n  \n  \/\/ Grab a count of devices on the wire\n  numberOfDevices = sensors.getDeviceCount();\n  \n  \/\/ locate devices on the bus\n  Serial.print(&quot;Locating devices...&quot;);\n  Serial.print(&quot;Found &quot;);\n  Serial.print(numberOfDevices, DEC);\n  Serial.println(&quot; devices.&quot;);\n\n  \/\/ Loop through each device, print out address\n  for(int i=0;i&lt;numberOfDevices; i++){\n    \/\/ Search the wire for address\n    if(sensors.getAddress(tempDeviceAddress, i)){\n      Serial.print(&quot;Found device &quot;);\n      Serial.print(i, DEC);\n      Serial.print(&quot; with address: &quot;);\n      printAddress(tempDeviceAddress);\n      Serial.println();\n    } else {\n      Serial.print(&quot;Found ghost device at &quot;);\n      Serial.print(i, DEC);\n      Serial.print(&quot; but could not detect address. Check power and cabling&quot;);\n    }\n  }\n}\n\nvoid loop(){ \n  sensors.requestTemperatures(); \/\/ Send the command to get temperatures\n  \n  \/\/ Loop through each device, print out temperature data\n  for(int i=0;i&lt;numberOfDevices; i++){\n    \/\/ Search the wire for address\n    if(sensors.getAddress(tempDeviceAddress, i)){\n      \/\/ Output the device ID\n      Serial.print(&quot;Temperature for device: &quot;);\n      Serial.println(i,DEC);\n      \/\/ Print the data\n      float tempC = sensors.getTempC(tempDeviceAddress);\n      Serial.print(&quot;Temp C: &quot;);\n      Serial.print(tempC);\n      Serial.print(&quot; Temp F: &quot;);\n      Serial.println(DallasTemperature::toFahrenheit(tempC)); \/\/ Converts tempC to Fahrenheit\n    }\n  }\n  delay(5000);\n}\n\n\/\/ function to print a device address\nvoid printAddress(DeviceAddress deviceAddress) {\n  for (uint8_t i = 0; i &lt; 8; i++){\n    if (deviceAddress[i] &lt; 16) Serial.print(&quot;0&quot;);\n      Serial.print(deviceAddress[i], HEX);\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_DS18B20_Multiple.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>In this example, we&#8217;re using three DS18B20 temperature sensors. This is what we get on the Arduino IDE Serial Monitor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"454\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/multiple-ds18b20-sensor-serial-monitor-115200.png?resize=708%2C454&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Multiple DS18B20 Temperature Sensors Serial Monitor Print readings\" class=\"wp-image-87090\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/multiple-ds18b20-sensor-serial-monitor-115200.png?w=708&amp;quality=100&amp;strip=all&amp;ssl=1 708w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/multiple-ds18b20-sensor-serial-monitor-115200.png?resize=300%2C192&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 708px) 100vw, 708px\" \/><\/figure><\/div>\n\n\n<p>We have a dedicated article on how to interface multiple DS18B20 temperature sensors with the EPS32. Just follow the next tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-multiple-ds18b20-temperature-sensors\/\">ESP32 with Multiple DS18B20 Temperature Sensors<\/a><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/randomnerdtutorials.com\/esp32-multiple-ds18b20-temperature-sensors\/\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/multiple-ds18b20-temp-sensor-thumbnail.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with multiple DS18B20 temperature sensors Arduino IDE\" class=\"wp-image-67071\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/multiple-ds18b20-temp-sensor-thumbnail.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/multiple-ds18b20-temp-sensor-thumbnail.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Display DS18B20 Temperature Readings in a Web Server<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-Display-DS18B20-Temperature-Readings-Web-Server-Arduino-IDE.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Display DS18B20 Temperature Readings in ESP32 Web Server using Arduino IDE\" class=\"wp-image-87294\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-Display-DS18B20-Temperature-Readings-Web-Server-Arduino-IDE.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-Display-DS18B20-Temperature-Readings-Web-Server-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>To build the web server we\u2019ll use the&nbsp;<a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/me-no-dev\/ESPAsyncWebServer\" target=\"_blank\">ESPAsyncWebServer library<\/a> that provides an easy way to build an asynchronous web server. Building an asynchronous web server has several advantages. We recommend taking a quick look at the library documentation on its <a href=\"https:\/\/github.com\/me-no-dev\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">GitHub page<\/a>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the ESPAsyncWebServer and AsyncTCP libraries<\/h3>\n\n\n\n<p>You need to install the following libraries in your Arduino IDE to build the web server for this project.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer&nbsp;<\/a>by ESP32Async<\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/ESP32Async\/AsyncTCP\" target=\"_blank\" rel=\"noopener\" title=\"\">AsyncTCP<\/a> by ESP32Async<\/li>\n<\/ul>\n\n\n\n<p>You can install those libraries in the Arduino IDE Library Manager. Go to\u00a0<strong>Sketch\u00a0<\/strong>>\u00a0<strong>Include Library<\/strong>\u00a0>\u00a0<strong>Manage Libraries<\/strong>\u00a0and search for the libraries\u2019 names.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code (DS18B20 Async Web Server)<\/h3>\n\n\n\n<p>Open your Arduino IDE and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com  \n*********\/\n\n\/\/ Import required libraries\n#ifdef ESP32\n  #include &lt;WiFi.h&gt;\n  #include &lt;ESPAsyncWebServer.h&gt;\n#else\n  #include &lt;Arduino.h&gt;\n  #include &lt;ESP8266WiFi.h&gt;\n  #include &lt;Hash.h&gt;\n  #include &lt;ESPAsyncTCP.h&gt;\n  #include &lt;ESPAsyncWebServer.h&gt;\n#endif\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ Data wire is connected to GPIO 4\n#define ONE_WIRE_BUS 4\n\n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(ONE_WIRE_BUS);\n\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);\n\n\/\/ Variables to store temperature values\nString temperatureF = &quot;&quot;;\nString temperatureC = &quot;&quot;;\n\n\/\/ Timer variables\nunsigned long lastTime = 0;  \nunsigned long timerDelay = 30000;\n\n\/\/ Replace with your network credentials\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Create AsyncWebServer object on port 80\nAsyncWebServer server(80);\n\nString readDSTemperatureC() {\n  \/\/ Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus\n  sensors.requestTemperatures(); \n  float tempC = sensors.getTempCByIndex(0);\n\n  if(tempC == -127.00) {\n    Serial.println(&quot;Failed to read from DS18B20 sensor&quot;);\n    return &quot;--&quot;;\n  } else {\n    Serial.print(&quot;Temperature Celsius: &quot;);\n    Serial.println(tempC); \n  }\n  return String(tempC);\n}\n\nString readDSTemperatureF() {\n  \/\/ Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus\n  sensors.requestTemperatures(); \n  float tempF = sensors.getTempFByIndex(0);\n\n  if(int(tempF) == -196){\n    Serial.println(&quot;Failed to read from DS18B20 sensor&quot;);\n    return &quot;--&quot;;\n  } else {\n    Serial.print(&quot;Temperature Fahrenheit: &quot;);\n    Serial.println(tempF);\n  }\n  return String(tempF);\n}\n\nconst char index_html[] PROGMEM = R&quot;rawliteral(\n&lt;!DOCTYPE HTML&gt;&lt;html&gt;\n&lt;head&gt;\n  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n  &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/use.fontawesome.com\/releases\/v5.7.2\/css\/all.css&quot; integrity=&quot;sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr&quot; crossorigin=&quot;anonymous&quot;&gt;\n  &lt;style&gt;\n    html {\n     font-family: Arial;\n     display: inline-block;\n     margin: 0px auto;\n     text-align: center;\n    }\n    h2 { font-size: 3.0rem; }\n    p { font-size: 3.0rem; }\n    .units { font-size: 1.2rem; }\n    .ds-labels{\n      font-size: 1.5rem;\n      vertical-align:middle;\n      padding-bottom: 15px;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;h2&gt;ESP DS18B20 Server&lt;\/h2&gt;\n  &lt;p&gt;\n    &lt;i class=&quot;fas fa-thermometer-half&quot; style=&quot;color:#059e8a;&quot;&gt;&lt;\/i&gt; \n    &lt;span class=&quot;ds-labels&quot;&gt;Temperature Celsius&lt;\/span&gt; \n    &lt;span id=&quot;temperaturec&quot;&gt;%TEMPERATUREC%&lt;\/span&gt;\n    &lt;sup class=&quot;units&quot;&gt;&amp;deg;C&lt;\/sup&gt;\n  &lt;\/p&gt;\n  &lt;p&gt;\n    &lt;i class=&quot;fas fa-thermometer-half&quot; style=&quot;color:#059e8a;&quot;&gt;&lt;\/i&gt; \n    &lt;span class=&quot;ds-labels&quot;&gt;Temperature Fahrenheit&lt;\/span&gt;\n    &lt;span id=&quot;temperaturef&quot;&gt;%TEMPERATUREF%&lt;\/span&gt;\n    &lt;sup class=&quot;units&quot;&gt;&amp;deg;F&lt;\/sup&gt;\n  &lt;\/p&gt;\n&lt;\/body&gt;\n&lt;script&gt;\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(&quot;temperaturec&quot;).innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(&quot;GET&quot;, &quot;\/temperaturec&quot;, true);\n  xhttp.send();\n}, 10000) ;\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(&quot;temperaturef&quot;).innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(&quot;GET&quot;, &quot;\/temperaturef&quot;, true);\n  xhttp.send();\n}, 10000) ;\n&lt;\/script&gt;\n&lt;\/html&gt;)rawliteral&quot;;\n\n\/\/ Replaces placeholder with DS18B20 values\nString processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == &quot;TEMPERATUREC&quot;){\n    return temperatureC;\n  }\n  else if(var == &quot;TEMPERATUREF&quot;){\n    return temperatureF;\n  }\n  return String();\n}\n\nvoid setup(){\n  \/\/ Serial port for debugging purposes\n  Serial.begin(115200);\n  Serial.println();\n  \n  \/\/ Start up the DS18B20 library\n  sensors.begin();\n\n  temperatureC = readDSTemperatureC();\n  temperatureF = readDSTemperatureF();\n\n  \/\/ Connect to Wi-Fi\n  WiFi.begin(ssid, password);\n  Serial.println(&quot;Connecting to WiFi&quot;);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println();\n  \n  \/\/ Print ESP Local IP Address\n  Serial.println(WiFi.localIP());\n\n  \/\/ Route for root \/ web page\n  server.on(&quot;\/&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/html&quot;, index_html, processor);\n  });\n  server.on(&quot;\/temperaturec&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/plain&quot;, temperatureC.c_str());\n  });\n  server.on(&quot;\/temperaturef&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/plain&quot;, temperatureF.c_str());\n  });\n  \/\/ Start server\n  server.begin();\n}\n \nvoid loop(){\n  if ((millis() - lastTime) &gt; timerDelay) {\n    temperatureC = readDSTemperatureC();\n    temperatureF = readDSTemperatureF();\n    lastTime = millis();\n  }  \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_DS18B20_Async_Web_Server.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Insert your network credentials in the following variables and the code will work straight away.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>In the following paragraphs, we\u2019ll explain how the code works. Keep reading if you want to learn more or jump to the &#8220;Demonstration&#8221; section to see the final result.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing libraries<\/h4>\n\n\n\n<p>First, import the required libraries for the ESP32 board:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;WiFi.h&gt;\n#include &lt;ESPAsyncWebServer.h&gt;\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Instantiate DS18B20 Sensor<\/h4>\n\n\n\n<p>Define the GPIO that the DS18B20 data pin is connected to. In this case, it\u2019s connected to <span class=\"rnthl rntclblue\">GPIO 4<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define ONE_WIRE_BUS 4<\/code><\/pre>\n\n\n\n<p>Instantiate the instances needed to initialize the sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(ONE_WIRE_BUS);\n\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting your network credentials<\/h4>\n\n\n\n<p>Insert your network credentials in the following variables, so that the ESP8266 can connect to your local network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">AsyncWebServer<\/span> object on port 80.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>AsyncWebServer server(80);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read Temperature Functions<\/h4>\n\n\n\n<p>Then, we create two functions to read the temperature.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">readDSTemperatureC()<\/span> function returns the readings in Celsius degrees.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String readDSTemperatureC() {\n  \/\/ Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus\n  sensors.requestTemperatures(); \n  float tempC = sensors.getTempCByIndex(0);\n\n  if(tempC == -127.00){\n    Serial.println(\"Failed to read from DS18B20 sensor\");\n    return \"--\";\n  } else {\n    Serial.print(\"Temperature Celsius: \");\n    Serial.println(tempC); \n  }\n  return String(tempC);\n}<\/code><\/pre>\n\n\n\n<p>In case the sensor is not able to get a valid reading, it returns -127. So, we have an if statement that returns two dashes (\u2013-) in case the sensor fails to get the readings. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(tempC == -127.00){\n  Serial.println(\"Failed to read from DS18B20 sensor\");\n  return \"--\";<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">reaDSTemperatureF()<\/span> function works in a similar way but returns the readings in Fahrenheit degrees.<\/p>\n\n\n\n<p>The readings are returned as string type. To convert a float to a string, use the <span class=\"rnthl rntliteral\">String()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>return String(tempC);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Building the Web Page<\/h4>\n\n\n\n<p>The next step is building the web page. The HTML and CSS needed to build the web page are saved on the <span class=\"rnthl rntliteral\">index_html<\/span> variable.<\/p>\n\n\n\n<p>In the HTML text we have <span class=\"rnthl rntliteral\">TEMPERATUREC<\/span> and <span class=\"rnthl rntliteral\">TEMPERATUREF<\/span> between <strong>%<\/strong> signs. This is a placeholder for the temperature values.<\/p>\n\n\n\n<p>This means that this <span class=\"rnthl rntliteral\"><strong>%TEMPERATUREC%<\/strong><\/span> text is like a variable that will be replaced by the actual temperature value from the sensor. The placeholders on the HTML text should go between <strong>%<\/strong> signs. <\/p>\n\n\n\n<p>We&#8217;ve explained in great detail how the HTML and CSS used in this web server works in a previous tutorial. So, if you want to learn more, refer to the next project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide\/\">DHT11\/DHT22 Temperature and Humidity Web Server with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Processor<\/h4>\n\n\n\n<p>Now, we need to create the <span class=\"rnthl rntliteral\">processor()<\/span> function, that will replace the placeholders in our HTML text with the actual temperature values.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == \"TEMPERATUREC\"){\n    return readDSTemperatureC();\n  }\n  else if(var == \"TEMPERATUREF\"){\n    return readDSTemperatureF();\n  }\n  return String();\n}<\/code><\/pre>\n\n\n\n<p>When the web page is requested, we check if the HTML has any placeholders. If it finds the <span class=\"rnthl rntliteral\">%TEMPERATUREC%<\/span> placeholder, we return the temperature in Celsius by calling the <span class=\"rnthl rntliteral\">readDSTemperatureC()<\/span> function created previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(var == \"TEMPERATUREC\"){\n  return readDSTemperatureC();\n}<\/code><\/pre>\n\n\n\n<p>If the placeholder is <span class=\"rnthl rntliteral\">%TEMPERATUREF%<\/span>, we return the temperature in Fahrenheit.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>else if(var == \"TEMPERATUREF\"){\n  return readDSTemperatureF();\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor for debugging purposes.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the DS18B20 temperature sensor.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.begin();<\/code><\/pre>\n\n\n\n<p>Connect to your local network and print the ESP32 IP address.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.begin(ssid, password);\nSerial.println(\"Connecting to WiFi\");\nwhile (WiFi.status() != WL_CONNECTED) {\n  delay(500);\n  Serial.print(\".\");\n}\nSerial.println();\n\n\/\/ Print ESP8266 Local IP Address\nSerial.println(WiFi.localIP());<\/code><\/pre>\n\n\n\n<p>Finally, add the next lines of code to handle the web server.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/html\", index_html, processor);\n});\nserver.on(\"\/temperaturec\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/plain\", readDSTemperatureC().c_str());\n});\nserver.on(\"\/temperaturef\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/plain\", readDSTemperatureF().c_str());\n});<\/code><\/pre>\n\n\n\n<p>When we make a request on the root URL, we send the HTML text that is stored in the <span class=\"rnthl rntliteral\">index_html<\/span> variable. We also need to pass the <span class=\"rnthl rntliteral\">processor<\/span> function, that will replace all the placeholders with the right values.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/html\", index_html, processor);\n});<\/code><\/pre>\n\n\n\n<p>We need to add two additional handlers to update the temperature readings. When we receive a request on the&nbsp;<em>\/temperature<\/em>c&nbsp;URL, we simply need to send the updated temperature value. It is plain text, and it should be sent as a char, so, we use the <span class=\"rnthl rntliteral\">c_str()<\/span> method.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/temperaturec\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/plain\", readDSTemperatureC().c_str());\n});<\/code><\/pre>\n\n\n\n<p>The same process is repeated for the temperature in Fahrenheit.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/temperaturef\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/plain\", readDSTemperatureF().c_str());\n});<\/code><\/pre>\n\n\n\n<p>Lastly, we can start the server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.begin();<\/code><\/pre>\n\n\n\n<p>Because this is an asynchronous web server, we don\u2019t need to write anything in the <span class=\"rnthl rntliteral\">loop()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop(){\n\n}<\/code><\/pre>\n\n\n\n<p>That\u2019s pretty much how the code works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After uploading the code, open the Arduino IDE Serial Monitor at a baud rate of 115200.&nbsp;Press the ESP32 on-board RST button and after a few seconds your IP address should show up.<\/p>\n\n\n\n<p>In your local network, open a browser and type&nbsp;the ESP32 IP address.<\/p>\n\n\n\n<p>Now you&nbsp;can see&nbsp;temperature in Celsius and Fahrenheit in your web server. The sensor readings update automatically without the need to refresh the web page.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"687\" height=\"461\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-Web-Server.png?resize=687%2C461&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Web Server with DS18B20 OneWire Temperature Sensor Arduino IDE\" class=\"wp-image-86800\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-Web-Server.png?w=687&amp;quality=100&amp;strip=all&amp;ssl=1 687w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-Web-Server.png?resize=300%2C201&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 687px) 100vw, 687px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. We have guides for other sensors and modules with the ESP32 that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\">ESP32 with BME280 (Pressure, Temperature and Humidity)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-hall-effect-sensor\/\">ESP32 Built-In Hall Effect Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-ssd1306-oled-display-arduino-ide\/\">ESP32 OLED Display with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide\/\">ESP32 DHT Temperature and Humidity Sensor with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you want to learn more about the ESP32 take a look at our course, or check or ESP32 free resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">More ESP32 Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a in-depth guide for the DS18B20 temperature sensor with ESP32 using Arduino IDE. We&#8217;ll show you how to wire the sensor, install the required libraries, and write the &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-ds18b20-temperature-arduino-ide\/#more-86576\" aria-label=\"Read more about ESP32 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":87101,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-86576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/86576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=86576"}],"version-history":[{"count":4,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/86576\/revisions"}],"predecessor-version":[{"id":168004,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/86576\/revisions\/168004"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/87101"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=86576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=86576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=86576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}