{"id":66986,"date":"2019-07-14T10:17:12","date_gmt":"2019-07-14T10:17:12","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=66986"},"modified":"2021-07-23T15:40:12","modified_gmt":"2021-07-23T15:40:12","slug":"esp32-multiple-ds18b20-temperature-sensors","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-multiple-ds18b20-temperature-sensors\/","title":{"rendered":"ESP32 with Multiple DS18B20 Temperature Sensors"},"content":{"rendered":"\n<p>This guide shows how to read temperature from multiple DS18B20 temperature sensors with the ESP32 using Arduino IDE. We\u2019ll show you how to wire the sensors on the same data bus to the ESP32, install the needed libraries, and a sketch example you can use in your own projects. This tutorial is also compatible with the ESP8266 and the Arduino boards.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" 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=\"\" 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\" \/><\/figure><\/div>\n\n\n\n<p>If you would like to build a web server displaying readings from multiple sensors, follow the next tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-plot-readings-charts-multiple\/\">ESP32 Plot Sensor Readings in Charts (Multiple DS18B20 Sensors)<\/a><\/p>\n\n\n\n<p>You might also like reading other DS18B20 guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-ds18b20-temperature-arduino-ide\/\">ESP32 DS18B20 Temperature Sensor with Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide\/\">ESP8266 DS18B20 Temperature Sensor with Arduino IDE<\/a><\/li><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><li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-ds18b20-temperature-sensor-with-arduino\/\">DS18B20 Temperature Sensor with Arduino<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing the DS18B20 Temperature Sensor<\/h2>\n\n\n\n<p>The DS18B20 temperature sensor is a 1-wire digital temperature sensor. Each sensor has a unique 64-bit serial number, which means you can use many sensors on the same data bus (this means many sensors connected to the same GPIO). This is specially useful for data logging and temperature control projects. The DS18B20 is a great sensor because it is cheap, accurate and very easy to use.<\/p>\n\n\n\n<p>The following figure shows the DS18B20 temperature.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"525\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/ds18b20.jpg?resize=750%2C525&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-67112\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/ds18b20.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/ds18b20.jpg?resize=300%2C210&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>Note: there&#8217;s also a <a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">waterproof version of the DS18B20 temperature sensor<\/a>.<\/p>\n\n\n\n<p>Here&#8217;s the main specifications of the DS18B20 temperature sensor:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Comunicates over 1-wire bus communication<\/li><li>Operating range temperature: -55\u00baC to 125\u00baC<\/li><li>Accuracy +\/-0.5 \u00baC (between the range -10\u00baC to 85\u00baC)<\/li><\/ul>\n\n\n\n<p>From left to right: the first pin is <span class=\"rnthl rntcblack\">GND<\/span>, the second is data, and the rightmost pin is <span class=\"rnthl rntcred\">VCC<\/span>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Where to Buy the DS18B20 Temperature Sensor?<\/h3>\n\n\n\n<p>Check the links below to compare the DS18B20 temperature sensor price on different stores:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">DS18B20 digital temperature sensor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">DS18B20 digital temperature sensor (waterproof version)<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Wiring Multiple DS18B20 Sensors<\/h2>\n\n\n\n<p>Here&#8217;s a list of the parts you need to follow this example:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"373\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/parts_required.jpg?resize=750%2C373&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-67118\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/parts_required.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/parts_required.jpg?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 DOIT DEVKIT V1 Board<\/a>&nbsp;\u2013&nbsp;<a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\">read ESP32 Development Boards Review and Comparison<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">DS18B20 temperature sensor<\/a>&nbsp;(we&#8217;re using 3 sensors in this example)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\">4.7k Ohm resistor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li><\/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<p>When wiring the DS18B20 temperature sensor you need to add a 4.7k Ohm resistor between VCC and the data line. The following schematic shows an example for three sensors (you can add more sensors if needed).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"886\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/multiple_ds18b20_esp32_bb.png?resize=834%2C886&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-67067\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/multiple_ds18b20_esp32_bb.png?w=834&amp;quality=100&amp;strip=all&amp;ssl=1 834w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/multiple_ds18b20_esp32_bb.png?resize=282%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 282w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/multiple_ds18b20_esp32_bb.png?resize=768%2C816&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 834px) 100vw, 834px\" \/><\/figure><\/div>\n\n\n\n<p>In the previous schematic, the round side of the sensor is facing backwards. The flat part is facing forward.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing the Arduino IDE<\/h2>\n\n\n\n<p>There\u2019s an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language. Follow one of the next tutorials to prepare your Arduino IDE to work with the ESP32, if you haven\u2019t already.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\"><strong>Windows<\/strong>&nbsp;instructions \u2013 ESP32 Board in Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-mac-and-linux-instructions\/\"><strong>Mac and Linux<\/strong>&nbsp;instructions \u2013 ESP32 Board in Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Libraries<\/h3>\n\n\n\n<p>Before uploading the code, you need to install two libraries in your Arduino IDE. The&nbsp;<a href=\"https:\/\/github.com\/PaulStoffregen\/OneWire\" target=\"_blank\" rel=\"noreferrer noopener\">OneWire library by Paul Stoffregen<\/a>&nbsp;and the&nbsp;<a href=\"https:\/\/github.com\/milesburton\/Arduino-Temperature-Control-Library\" target=\"_blank\" rel=\"noreferrer noopener\">Dallas Temperature library<\/a>. Follow the next steps to install those libraries.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>OneWire library<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/PaulStoffregen\/OneWire\/archive\/master.zip\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to download the OneWire&nbsp;library<\/a>. You should have a .zip folder in your Downloads<\/li><li>Unzip the&nbsp;<em>.zip<\/em>&nbsp;folder and you should get&nbsp;<strong>OneWire-master&nbsp;<\/strong>folder<\/li><li>Rename your folder from&nbsp;<del><strong>OneWire-master<\/strong><\/del>&nbsp;to<strong>&nbsp;OneWire<\/strong><\/li><li>Move the&nbsp;<strong>OneWire&nbsp;<\/strong>folder to your Arduino IDE installation&nbsp;<strong>libraries&nbsp;<\/strong>folder<\/li><li>Finally, re-open your Arduino IDE<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Dallas Temperature library<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/milesburton\/Arduino-Temperature-Control-Library\/archive\/master.zip\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to download the DallasTemperature&nbsp;library<\/a>. You should have a .zip folder in your Downloads<\/li><li>Unzip the&nbsp;<em>.zip<\/em>&nbsp;folder and you should get&nbsp;<strong>Arduino-Temperature-Control-Library-master&nbsp;<\/strong>folder<\/li><li>Rename your folder from&nbsp;<del><strong>Arduino-Temperature-Control-Library-master<\/strong><\/del>&nbsp;to&nbsp;<strong>DallasTemperature<\/strong><\/li><li>Move the&nbsp;<strong>DallasTemperature<\/strong>folder to your Arduino IDE installation&nbsp;<strong>libraries&nbsp;<\/strong>folder<\/li><li>Finally, re-open your Arduino IDE<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Getting the DS18B20 Sensor Address<\/h2>\n\n\n\n<p>Each DS18B20 temperature sensor has a serial number assigned to it. First, you need to find that number to label each sensor accordingly. You need to do this, so that later you know from which sensor you&#8217;re reading the temperature from.<\/p>\n\n\n\n<p>Upload the following code to the ESP32. Make sure you have the right board and COM port selected.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * Rui Santos \n * Complete Project Details https:\/\/randomnerdtutorials.com\n *\/\n\n#include &lt;OneWire.h&gt;\n\n\/\/ Based on the OneWire library example\n\nOneWire ds(4);  \/\/data wire connected to GPIO 4\n\nvoid setup(void) {\n  Serial.begin(115200);\n}\n\nvoid loop(void) {\n  byte i;\n  byte addr[8];\n  \n  if (!ds.search(addr)) {\n    Serial.println(&quot; No more addresses.&quot;);\n    Serial.println();\n    ds.reset_search();\n    delay(250);\n    return;\n  }\n  Serial.print(&quot; ROM =&quot;);\n  for (i = 0; i &lt; 8; i++) {\n    Serial.write(' ');\n    Serial.print(addr[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\/ESP32\/Find_DS18B20_Addresses.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Wire just one sensor at a time to find its address (or successively add a new sensor) so that you&#8217;re able to identify each one by its address. Then, you can add a physical label to each sensor. Open the Serial Monitor at a baud rate of 9600 and you should get something as follows (but with different addresses):<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"785\" height=\"374\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/find_addresses_ds18b20.png?resize=785%2C374&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-67005\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/find_addresses_ds18b20.png?w=785&amp;quality=100&amp;strip=all&amp;ssl=1 785w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/find_addresses_ds18b20.png?resize=300%2C143&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/find_addresses_ds18b20.png?resize=768%2C366&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 785px) 100vw, 785px\" \/><\/figure><\/div>\n\n\n\n<p>Untick the &#8220;<strong>Autoscroll<\/strong>&#8221; option so that you&#8217;re able to copy the addresses. In our case we&#8217;ve got the following addresses:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Sensor 1: <strong>28 FF 77 62 40 17 4 31<\/strong><\/li><li>Sensor 2: <strong>28 FF B4 6 33 17 3 4B<\/strong><\/li><li>Sensor 3: <strong>28 FF A0 11 33 17 3 96<\/strong><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Temperature From Multiple Sensors<\/h2>\n\n\n\n<p>Getting the temperature from multiple sensors on the same common data bus is very straightforward.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"480\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/circuit.jpg?resize=750%2C480&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-67119\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/circuit.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/circuit.jpg?resize=300%2C192&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>The example code below reads temperature in Celsius and Fahrenheit from each sensor and prints the results in the 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 https:\/\/randomnerdtutorials.com\n *\/\n\n\/\/ Include the libraries we need\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ Data wire is connected to GPIO15\n#define ONE_WIRE_BUS 15\n\/\/ Setup a oneWire instance to communicate with a OneWire device\nOneWire oneWire(ONE_WIRE_BUS);\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);\n\nDeviceAddress sensor1 = { 0x28, 0xFF, 0x77, 0x62, 0x40, 0x17, 0x4, 0x31 };\nDeviceAddress sensor2 = { 0x28, 0xFF, 0xB4, 0x6, 0x33, 0x17, 0x3, 0x4B };\nDeviceAddress sensor3= { 0x28, 0xFF, 0xA0, 0x11, 0x33, 0x17, 0x3, 0x96 };\n\nvoid setup(void){\n  Serial.begin(115200);\n  sensors.begin();\n}\n\nvoid loop(void){ \n  Serial.print(&quot;Requesting temperatures...&quot;);\n  sensors.requestTemperatures(); \/\/ Send the command to get temperatures\n  Serial.println(&quot;DONE&quot;);\n  \n  Serial.print(&quot;Sensor 1(*C): &quot;);\n  Serial.print(sensors.getTempC(sensor1)); \n  Serial.print(&quot; Sensor 1(*F): &quot;);\n  Serial.println(sensors.getTempF(sensor1)); \n \n  Serial.print(&quot;Sensor 2(*C): &quot;);\n  Serial.print(sensors.getTempC(sensor2)); \n  Serial.print(&quot; Sensor 2(*F): &quot;);\n  Serial.println(sensors.getTempF(sensor2)); \n  \n  Serial.print(&quot;Sensor 3(*C): &quot;);\n  Serial.print(sensors.getTempC(sensor3)); \n  Serial.print(&quot; Sensor 3(*F): &quot;);\n  Serial.println(sensors.getTempF(sensor3)); \n  \n  delay(2000);\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\/ESP32\/ESP32_Multiple_DS18B20.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200 and you should get something similar.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"370\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/temperature_multiple_DS18B20.png?resize=624%2C370&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-67016\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/temperature_multiple_DS18B20.png?w=624&amp;quality=100&amp;strip=all&amp;ssl=1 624w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/07\/temperature_multiple_DS18B20.png?resize=300%2C178&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>First, include the needed 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 rntcblue\">GPIO 15<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Data wire is connected to ESP32 GPIO15\n#define ONE_WIRE_BUS 15\n\/\/ Setup a oneWire instance to communicate with a OneWire device\nOneWire oneWire(ONE_WIRE_BUS);\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);<\/code><\/pre>\n\n\n\n<p>Start the Dallas temperature library for the DS18B20 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.begin();<\/code><\/pre>\n\n\n\n<p>Then, enter the addresses you&#8217;ve found previously for each temperature sensor. In our case, we have the following:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DeviceAddress sensor1 = { 0x28, 0xFF, 0x77, 0x62, 0x40, 0x17, 0x4, 0x31 };\nDeviceAddress sensor2 = { 0x28, 0xFF, 0xB4, 0x6, 0x33, 0x17, 0x3, 0x4B };\nDeviceAddress sensor3= { 0x28, 0xFF, 0xA0, 0x11, 0x33, 0x17, 0x3, 0x96 };<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize a Serial communication and start the Dallas temperature library for the DS18B20 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void setup(void){\n  Serial.begin(115200);\n  sensors.begin();\n}<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, request the temperatures both in Celsius and Fahrenheit and print the results on the Serial Monitor.<\/p>\n\n\n\n<p>First, you need to request the temperatures using the following line of code:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.requestTemperatures(); \/\/ Send the command to get temperatures<\/code><\/pre>\n\n\n\n<p>Then, you can request the temperature by using the sensors address:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"rnthl rntliteral\">sensors.getTempC(SENSOR_ADDRESS)<\/span> &#8211; requests the temperature in Celsius<\/li><li><span class=\"rnthl rntliteral\">sensors.getTempF(SENSOR_ADDRESS)<\/span> &#8211; requests the temperature in Fahrenheit<\/li><\/ul>\n\n\n\n<p>For example, to request temperature in Celsius for sensor 1, you use:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.getTempC(sensor1)<\/code><\/pre>\n\n\n\n<p>In which <span class=\"rnthl rntliteral\">sensor1<\/span> is a variable that holds the address of the first sensor.<\/p>\n\n\n\n<p>This is just a simple sketch example to show you how to get temperature from multiple DS18B20 sensors using the ESP32. This code is also compatible with the ESP8266 and Arduino boards.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Taking It Further<\/h2>\n\n\n\n<p>Getting temperature from multiple DS18B20 temperature sensors is specially useful in monitoring and temperature control projects and data logging. Learn how to log the collected data to a microSD card:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-data-logging-temperature-to-microsd-card\/\">ESP32 Data Logging Temperature to MicroSD Card<\/a><\/li><\/ul>\n\n\n\n<p>You can also publish your readings via MQTT to Node-RED and display your data in charts. We have a tutorial about that subject in the link below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-mqtt-publish-subscribe-arduino-ide\/\">ESP32 MQTT \u2013 Publish and Subscribe with Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful.&nbsp;If you like ESP32 and you want to learn more, we recommend enrolling in&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\"><strong>Learn ESP32 with Arduino IDE<\/strong> course<\/a>.<\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to read temperature from multiple DS18B20 temperature sensors with the ESP32 using Arduino IDE. We\u2019ll show you how to wire the sensors on the same data &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 with Multiple DS18B20 Temperature Sensors\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-multiple-ds18b20-temperature-sensors\/#more-66986\" aria-label=\"Read more about ESP32 with Multiple DS18B20 Temperature Sensors\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":67144,"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-66986","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\/2018\/07\/multiple-ds18b20-temp-sensor_1280.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\/66986","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=66986"}],"version-history":[{"count":1,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/66986\/revisions"}],"predecessor-version":[{"id":105428,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/66986\/revisions\/105428"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/67144"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=66986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=66986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=66986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}