{"id":24955,"date":"2019-07-02T09:23:35","date_gmt":"2019-07-02T09:23:35","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=24955"},"modified":"2019-07-16T09:31:28","modified_gmt":"2019-07-16T09:31:28","slug":"guide-for-ds18b20-temperature-sensor-with-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/guide-for-ds18b20-temperature-sensor-with-arduino\/","title":{"rendered":"Guide for DS18B20 Temperature Sensor with Arduino"},"content":{"rendered":"\n<p>This guide shows how to use the DS18B20 temperature sensor with the Arduino board. You&#8217;ll learn how to wire the sensor, install the required libraries and get temperature from one or multiple DS18B20 sensors.<\/p>\n\n\n\n<div class=\"wp-block-image\"><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\/DS18B20-arduino-guide.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Guide for DS18B20 Temperature Sensor with Arduino\" class=\"wp-image-86706\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-arduino-guide.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-arduino-guide.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\/DS18B20-arduino-guide.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\/DS18B20-arduino-guide.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\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\/esp32-multiple-ds18b20-temperature-sensors\/\">ESP32 with Multiple DS18B20 Temperature Sensors<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing DS18B20 Temperature Sensor<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">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 the Arduino.<\/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 \u201cparasite mode\u201d), which eliminates the need for an external power supply.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><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 Diagram\" 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>\n\n\n\n<p>The following table shows how you should wire the DS18B20 sensor to your Arduino board:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><strong>DS18B20<\/strong><\/td><td><strong>Arduino<\/strong><\/td><\/tr><tr><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><td>GND<\/td><\/tr><tr><td><span class=\"rnthl rntcyellow\">DQ<\/span><\/td><td>Any digital pin (with 4.7k Ohm pull-up resistor)<\/td><\/tr><tr><td><span class=\"rnthl rntcred\">VDD<\/span><\/td><td>5V (normal mode) or GND (parasite mode)<\/td><\/tr><\/tbody><\/table>\n\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 Arduino digital pin.<\/p>\n\n\n\n<p>The DS18B20 temperature sensor is also available in <strong><a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"waterproof version (opens in a new tab)\">waterproof version<\/a><\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><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\n<p>Here\u2019s a summary of the most relevant specs of the DS18B20 temperature sensor:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Communicates over one-wire bus communication<\/li><li>Power supply range: 3.0V to 5.5V<\/li><li>Operating temperature range: -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>For more information consult the&nbsp;<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 show you how the sensor works, we&#8217;ll build a simple example that reads the temperature from the DS18B20 sensor with the Arduino and displays the values on the Arduino Serial Monitor. <\/p>\n\n\n\n<p>Here&#8217;s a list of parts you need to complete this tutorial<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\">Arduino UNO<\/a>&nbsp;\u2013 read&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\">Best Arduino Starter Kits<\/a><\/li><li><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> (one or multiple sensors) \u2013 <a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">waterproof version<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">4.7k Ohm resistor<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/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<h2 class=\"wp-block-heading\">Schematic<\/h2>\n\n\n\n<p>The sensor can operate in two modes:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Normal mode:<\/strong> 3-wire connection is needed. You provide power to the VDD pin. Here\u2019s the schematic you need to follow:<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"462\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_single_normal.png?resize=750%2C462&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 temperature sensor schematic wiring diagram normal mode\" class=\"wp-image-86707\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_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\/07\/ds18b20_arduino_single_normal.png?resize=300%2C185&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><strong>Parasite mode:<\/strong> You only need data and GND. The sensor derives its power from the data line. In this case, here\u2019s the schematic you need to follow:<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"462\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_single_parasite.png?resize=750%2C462&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 temperature sensor schematic wiring diagram parasite mode\" class=\"wp-image-86708\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_single_parasite.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_single_parasite.png?resize=300%2C185&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>You can read the temperature of more than one sensor at the same time using just one  Arduino digital pin. For that, you just need to wire together all the sensors data pins&nbsp;to an Arduino digital pin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Upload Code &#8211; Single DS18B20 <\/h2>\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<h3 class=\"wp-block-heading\">Installing Libraries<\/h3>\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 the OneWire library by Paul Stoffregen.<\/p>\n\n\n\n<div class=\"wp-block-image\"><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=\"Installing OneWire library 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\n<p>3. Then, search for &#8220;<strong>Dallas<\/strong>&#8221; and install the Dallas Temperature library by Miles Burton.<\/p>\n\n\n\n<div class=\"wp-block-image\"><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=\"Installing Dallas temperature DS18B20 library 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\n<p>After installing the needed libraries, upload the following code to your Arduino board. This sketch is based on an example from the Dallas Temperature 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 http:\/\/randomnerdtutorials.com  \n  Based on the Dallas Temperature Library example\n*********\/\n\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ Data wire is conntec to the Arduino digital pin 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\nvoid setup(void)\n{\n  \/\/ Start serial communication for debugging purposes\n  Serial.begin(9600);\n  \/\/ Start up the library\n  sensors.begin();\n}\n\nvoid loop(void){ \n  \/\/ Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus\n  sensors.requestTemperatures(); \n  \n  Serial.print(&quot;Celsius temperature: &quot;);\n  \/\/ Why &quot;byIndex&quot;? You can have more than one IC on the same bus. 0 refers to the first IC on the wire\n  Serial.print(sensors.getTempCByIndex(0)); \n  Serial.print(&quot; - Fahrenheit temperature: &quot;);\n  Serial.println(sensors.getTempFByIndex(0));\n  delay(1000);\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\/DS18B20_Temperature_Sensor_Arduino.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. If you&#8217;re using just one single sensor, this is one of the easiest and simplest ways.<\/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=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/arduino-ds18b20-single.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino with DS18b20 Temperature Sensor\" class=\"wp-image-86709\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/arduino-ds18b20-single.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/arduino-ds18b20-single.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<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\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>\n#include &lt;DallasTemperature.h><\/code><\/pre>\n\n\n\n<p>Create the instances needed for the temperature sensor. The temperature sensor is connected to&nbsp;<span class=\"rnthl rntcblue\">Pin 4<\/span>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Data wire is conntec to the Arduino digital pin 4\nconst int oneWireBus = 4;          \n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\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 9600.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(9600);<\/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>In the <span class=\"rnthl rntliteral\">loop()<\/span> is where you&#8217;ll get the temperature.  You need to call the <span class=\"rnthl rntliteral\">requestTemperatures()<\/span> method before getting the actual temperature value.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.requestTemperatures(); <\/code><\/pre>\n\n\n\n<p>Then, get and print the temperature in Celsius. To get the temperature in Celsius, use the <span class=\"rnthl rntliteral\">getTempCByIndex()<\/span> method :<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(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>Serial.println(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 have more than one sensor, you use index 0 for the first sensor, index 1 for the second sensor, and so on.<\/p>\n\n\n\n<p>New temperature readings are requested every second.<\/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, open the Arduino IDE Serial Monitor&nbsp;at a 9600 baud rate. You should get the temperature displayed in both Celsius and Fahrenheit:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"509\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=723%2C509&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Testing DS18b20 Temperature Sensor with Arduino IDE Serial Monitor\" class=\"wp-image-24981\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?w=723&amp;quality=100&amp;strip=all&amp;ssl=1 723w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=150%2C106&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=300%2C211&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=80%2C56&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=220%2C155&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=142%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 142w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=213%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 213w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=338%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 338w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=589%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 589w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/08\/serial-monitor-1.png?resize=692%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 692w\" sizes=\"(max-width: 723px) 100vw, 723px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Temperature from Multiple DS18B20 Sensors<\/h2>\n\n\n\n<div class=\"wp-block-image\"><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\/arduino-ds18b20-multiple-sensors.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Getting Temperature from Multiple DS18B20 Sensors\" class=\"wp-image-86711\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/arduino-ds18b20-multiple-sensors.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/arduino-ds18b20-multiple-sensors.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>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 Arduino digital Pin. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic<\/h3>\n\n\n\n<p>To read the temperature from multiple sensors, you just need to wire all data lines together as shown in the following schematic diagram: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"774\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_multiple.png?resize=1200%2C774&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino Multiple DS18B20 Temperature sensors wiring schematic diagram\" class=\"wp-image-86712\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_multiple.png?w=1460&amp;quality=100&amp;strip=all&amp;ssl=1 1460w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_multiple.png?resize=300%2C194&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_multiple.png?resize=768%2C496&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_arduino_multiple.png?resize=1024%2C661&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Upload Code &#8211; Multiple DS18B20<\/h3>\n\n\n\n<p>Then, upload the following code. It scans for all devices on Pin 4 and prints the temperature for each one. This sketch is based on the 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 http:\/\/randomnerdtutorials.com\n*\/\n\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ Data wire is plugged into port 4 on the Arduino\n#define ONE_WIRE_BUS 4\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\nint numberOfDevices; \/\/ Number of temperature devices found\n\nDeviceAddress tempDeviceAddress; \/\/ We'll use this variable to store a found device address\n\nvoid setup(void) {\n  \/\/ start serial port\n  Serial.begin(9600);\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\t\t} else {\n\t\t  Serial.print(&quot;Found ghost device at &quot;);\n\t\t  Serial.print(i, DEC);\n\t\t  Serial.print(&quot; but could not detect address. Check power and cabling&quot;);\n\t\t}\n  }\n}\n\nvoid loop(void) { \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\t\t\n\t\t\/\/ Output the device ID\n\t\tSerial.print(&quot;Temperature for device: &quot;);\n\t\tSerial.println(i,DEC);\n\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    } \t\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\/Arduino\/Arduino_Multiple_DS18B20_Sensors.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>The code uses several useful methods to handle multiple DS18B20 sensors.<\/p>\n\n\n\n<p>You use the <span class=\"rnthl rntliteral\">getDeviceCount()<\/span> method to get the number of DS18B20 sensors on the data line.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>numberOfDevices = sensors.getDeviceCount();<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">getAddress()<\/span> method finds the sensors addresses:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(sensors.getAddress(tempDeviceAddress, i)){<\/code><\/pre>\n\n\n\n<p>The address is unique for each sensor. So each sensor can be identified by its address.<\/p>\n\n\n\n<p>Then, you use the <span class=\"rnthl rntliteral\">getTempC()<\/span> method that accepts as argument the device address. With this method you can get the temperature from a specific sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float tempC = sensors.getTempC(tempDeviceAddress);<\/code><\/pre>\n\n\n\n<p>To get the temperature in Fahrenheit degrees, you can use the <span class=\"rnthl rntliteral\">getTemF()<\/span>. Alternatively, you can convert the temperature in Celsius to Fahrenheit as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DallasTemperature::toFahrenheit(tempC)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>The DS18B20 temperature sensor is a one-wire digital sensor. To use this sensor with the Arduino, you need the OneWire and the DallasTemperature libraries. You can use one sensor or multiple sensors on the same data line because you can identify each sensor by its unique address.<\/p>\n\n\n\n<p>Now, you can take this project further and display your <a href=\"https:\/\/randomnerdtutorials.com\/guide-for-oled-display-with-arduino\/\">sensor readings in an OLED display<\/a>, for example. <\/p>\n\n\n\n<p>We have more tutorials for other Arduino compatible sensors that you may find useful:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/complete-guide-for-dht11dht22-humidity-and-temperature-sensor-with-arduino\/\">DHT11\/DHT22 Humidity and Temperature Sensor With Arduino<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/bme280-sensor-arduino-pressure-temperature-humidity\/\">BME280 (Temperature, Humidity, Pressure) with Arduino<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-relay-module-with-arduino\/\">Relay Module with Arduino<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/complete-guide-for-ultrasonic-sensor-hc-sr04\/\">Ultrasonic Sensor HC-SR04 with Arduino<\/a> <\/li><\/ul>\n\n\n\n<p>We hope you&#8217;ve found this guide useful.<\/p>\n\n\n\n<p>If you want to learn more about Arduino, take a look at our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\">Arduino Step-by-step projects course<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-arduino\/\">Free Arduino Projects and Tutorials<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-mini-course\/\">Arduino Mini Course<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading!<\/p>\n\n\n\n<p><em>Updated July 2, 2019<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to use the DS18B20 temperature sensor with the Arduino board. You&#8217;ll learn how to wire the sensor, install the required libraries and get temperature from one &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Guide for DS18B20 Temperature Sensor with Arduino\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/guide-for-ds18b20-temperature-sensor-with-arduino\/#more-24955\" aria-label=\"Read more about Guide for DS18B20 Temperature Sensor with Arduino\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":86706,"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":[267,2,302,269,264,215],"tags":[],"class_list":["post-24955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-project","category-arduino","category-0-guide","category-guide-project","category-project","category-sensors"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-arduino-guide.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\/24955","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=24955"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/24955\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/86706"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=24955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=24955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=24955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}