{"id":133323,"date":"2023-09-14T11:13:31","date_gmt":"2023-09-14T11:13:31","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=133323"},"modified":"2024-05-06T10:54:49","modified_gmt":"2024-05-06T10:54:49","slug":"raspberry-pi-pico-ds18b20-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-ds18b20-micropython\/","title":{"rendered":"Raspberry Pi Pico: DS18B20 Temperature Sensor (MicroPython) &#8211; Single and Multiple"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to interface the Raspberry Pi Pico with the DS18B20 temperature sensor and how to get temperature readings by programming your board using MicroPython firmware. The DS18B20 is an addressable temperature sensor, which means you can connect multiple sensors to the same GPIO. We&#8217;ll also show you how to get temperature from multiple DS18B20 sensors all at once.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DS18B20-MicroPython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with DS18B20 Temperature Sensor MicroPython Single and Multiple\" class=\"wp-image-133333\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DS18B20-MicroPython.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DS18B20-MicroPython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DS18B20-MicroPython.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DS18B20-MicroPython.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\">Do you prefer to program the Raspberry Pi Pico using Arduino IDE? Check this tutorial instead: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-ds18b20-arduino\/\" title=\"\">Raspberry Pi Pico: DS18B20 Temperature Sensor (Arduino IDE) \u2013 Single and Multiple<\/a>.<\/p>\n\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#ds18b20-intro\" title=\"\">Introducing DS18B20 Temperature Sensor<\/a><\/li>\n\n\n\n<li><a href=\"#rpi-pico-ds18b20-wiring\" title=\"\">Raspberry Pi Pico with DS18B20 (single) Wiring<\/a><\/li>\n\n\n\n<li><a href=\"#microds18b20-library\" title=\"\">Installing the microDS18B20 Library<\/a><\/li>\n\n\n\n<li><a href=\"#ds18b20-micropython-code\" title=\"\">Getting DS18B20 Temperature Readings (single) &#8211; MicroPython Code<\/a><\/li>\n\n\n\n<li><a href=\"#multiple-ds18b20\" title=\"\">Raspberry Pi Pico with Multiple DS18B20 Sensors<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#ds18b20-address\" title=\"\">Getting DS18B20 Sensors&#8217; Individual Address<\/a><\/li>\n\n\n\n<li><a href=\"#Rpi-Pico-multiple-ds18b20-sensors-wiring\" title=\"\">Wiring Multiple DS18B20 Sensors with Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"#multiple-ds18b20-raspberry-pi-pico-micropython\" title=\"\">Getting Temperature from Multiple DS18B20 Sensors &#8211; MicroPython Code<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites &#8211; MicroPython Firmware<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board. <\/p>\n\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\" title=\"\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ds18b20-intro\">Introducing DS18B20 Temperature Sensor<\/h2>\n\n\n\n<p>The <a aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">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 Raspberry Pi Pico.<\/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 size-full\"><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\/\" target=\"_blank\" rel=\"noopener\" title=\"\">waterproof version<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><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 aria-label=\" (opens in a new tab)\" href=\"https:\/\/datasheets.maximintegrated.com\/en\/ds\/DS18B20.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">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<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20-temperature-sensor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor dallas version\" class=\"wp-image-133302\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20-temperature-sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20-temperature-sensor.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<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\">DS18B20 temperature sensor<\/a> (one or multiple sensors) \u2013&nbsp;<a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\">waterproof version<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\"4.7k Ohm resistor\u2028 (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">4.7k Ohm resistor<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\"Jumper wires (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\"Breadboard (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">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\" id=\"rpi-pico-ds18b20-wiring\">Wiring the DS18B20 Temperature Sensor to the Raspberry Pi Pico<\/h2>\n\n\n\n<p>The DS18B20 temperature sensor comes with three pins: GND, data, and VCC. 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. We prefer using the normal mode.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" 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\" style=\"width:747px;height:488px\" 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>If you&#8217;re using the waterproof version, you can identify each pin by its color:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntcblack\">black<\/span>: GND<\/li>\n\n\n\n<li><span class=\"rnthl rntcred\">red<\/span>: VDD<\/li>\n\n\n\n<li><span class=\"rnthl rntcyellow\">yellow<\/span>: data line<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>DS18B20<\/strong><\/td><td><strong>Raspberry Pi Pico<\/strong><\/td><\/tr><tr><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><td>GND<\/td><\/tr><tr><td>Data (<span class=\"rnthl rntcyellow\">DQ<\/span>)<\/td><td>Any digital GPIO (we&#8217;ll be using GPIO22); <br>you also need to connect a 4.7KOhm resistor between the data line and VCC<\/td><\/tr><tr><td><span class=\"rnthl rntcred\">VDD<\/span><\/td><td>3V3(OUT)<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">You can also take a look at the following schematic diagram:<\/figcaption><\/figure>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"772\" height=\"566\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-ds18b20-wiring_bb.png?resize=772%2C566&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico DS18B20 Single Wiring\" class=\"wp-image-133318\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-ds18b20-wiring_bb.png?w=772&amp;quality=100&amp;strip=all&amp;ssl=1 772w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-ds18b20-wiring_bb.png?resize=300%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-ds18b20-wiring_bb.png?resize=768%2C563&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 772px) 100vw, 772px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"ds18b20-micropython-code\">Raspberry Pi Pico with DS18B20 &#8211; Get Temperature (MicroPython Code)<\/h2>\n\n\n\n<p>The following code reads the temperature from the DS18B20 temperature sensor and prints the readings on the MicroPython shell. There is a built-in module called <span class=\"rnthl rntliteral\">ds18x20<\/span> on the MicroPython firmware, so you don&#8217;t need to install any library.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-ds18b20-micropython\/\n\nimport machine, onewire, ds18x20, time\n\nds_pin = machine.Pin(22)\nds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))\n\nroms = ds_sensor.scan()\nprint('Found DS devices: ', roms)\n\nwhile True:\n  ds_sensor.convert_temp()\n  time.sleep_ms(750)\n  for rom in roms:\n    print(rom)\n    tempC = ds_sensor.read_temp(rom)\n    tempF = tempC * (9\/5) +32\n    print('temperature (\u00baC):', &quot;{:.2f}&quot;.format(tempC))\n    print('temperature (\u00baF):', &quot;{:.2f}&quot;.format(tempF))\n    print()\n  time.sleep(5)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/main_DS18B20_Single.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Continue reading to learn how the code works, or skip to the <a href=\"#demonstration\" title=\"\">Demonstration <\/a>section.<\/p>\n\n\n\n<p>Start by including the required modules. The <span class=\"rnthl rntliteral\">machine<\/span> module for accessing the hardware, the <span class=\"rnthl rntliteral\">onewire<\/span> for communicating with the DS18B20 sensor(s), the <span class=\"rnthl rntliteral\">ds18x20<\/span> for reading temperature, and <span class=\"rnthl rntliteral\">time<\/span> for time-related functions.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import machine, onewire, ds18x20, time<\/code><\/pre>\n\n\n\n<p>Define the GPIO pin that the DS18B20 sensor is connected to. We&#8217;re using <strong>GPIO22<\/strong>. Modify the code if you&#8217;re using a different pin.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ds_pin = machine.Pin(22)<\/code><\/pre>\n\n\n\n<p>Next, initialize the DS18B20 sensor by creating a <span class=\"rnthl rntliteral\">ds18x20<\/span> object called <span class=\"rnthl rntliteral\">ds_sensor<\/span> on the <span class=\"rnthl rntliteral\">ds_pin<\/span> defined earlier<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))<\/code><\/pre>\n\n\n\n<p>The DS18B20 communicates via one-wire communication protocol and each sensor has a unique 64-bit serial code. This means you can read several temperature sensors wired on the same GPIO. <\/p>\n\n\n\n<p>The following line uses the <span class=\"rnthl rntliteral\">scan()<\/span> function to scan for DS18B20 sensors. The addresses found are saved on the <span class=\"rnthl rntliteral\">roms<\/span> variable (the <span class=\"rnthl rntliteral\">roms<\/span> variable is of type list).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>roms = ds_sensor.scan()<\/code><\/pre>\n\n\n\n<p>Print the address of each sensor to the shell:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print('Found DS devices: ', roms)<\/code><\/pre>\n\n\n\n<p>Then, there&#8217;s a <span class=\"rnthl rntliteral\">while<\/span> loop that gets the temperature from the DS18B20 sensor(s) every 5 seconds.<\/p>\n\n\n\n<p>You need to call the <span class=\"rnthl rntliteral\">convert_temp()<\/span> function on the <span class=\"rnthl rntliteral\">ds_sensor<\/span> object each time you want to sample temperature.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ds_sensor.convert_temp()<\/code><\/pre>\n\n\n\n<p>Add a delay of 750 ms to give enough time to convert the temperature:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>time.sleep_ms(750)<\/code><\/pre>\n\n\n\n<p>After that, we can read the temperature on the addresses found earlier by using the <span class=\"rnthl rntliteral\">read_temp()<\/span> method and passing the address as an argument as shown in the following line of code.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>tempC = ds_sensor.read_temp(rom)<\/code><\/pre>\n\n\n\n<p>By default, it returns the temperature in Celsius degrees. The following line converts it to Fahrenheit.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>tempF = tempC * (9\/5) +32<\/code><\/pre>\n\n\n\n<p>Because you can add multiple sensors to the same data line, we have a <span class=\"rnthl rntliteral\">for<\/span> loop that goes through all the addresses and prints the temperature for each of them (in this case, it&#8217;s just one sensor):<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>for rom in roms:\n  print(rom)<\/code><\/pre>\n\n\n\n<p>In each loop, we print the temperature in Celsius degrees and in Fahrenheit degrees. The result will have two decimal places <span class=\"rnthl rntliteral\">{:.2f}<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print('temperature (\u00baC):', \"{:.2f}\".format(tempC))\nprint('temperature (\u00baF):', \"{:.2f}\".format(tempF))<\/code><\/pre>\n\n\n\n<p>The loop repeats every five seconds:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>time.sleep(5)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>Save the code to your Raspberry Pi Pico board using <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\">Thonny IDE<\/a> or any other MicroPython IDE of your choice.<\/p>\n\n\n\n<p>Copy the code provided to a new file on Thonny IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"731\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20-single-micropython-raspberry-pi-pico.png?resize=669%2C731&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE Micropython DS18B20 Code Raspberry Pi Pico\" class=\"wp-image-133328\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20-single-micropython-raspberry-pi-pico.png?w=669&amp;quality=100&amp;strip=all&amp;ssl=1 669w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20-single-micropython-raspberry-pi-pico.png?resize=275%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 275w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/figure><\/div>\n\n\n<p>With the code copied to the file, click on the&nbsp;<strong>Save&nbsp;<\/strong>icon. Then, select&nbsp;<strong>Raspberry Pi Pico<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"214\" height=\"203\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Save-file-to-Raspberry-Pi-Pico.png?resize=214%2C203&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Saving file to Raspberry Pi Pico MicroPython IDE\" class=\"wp-image-130899\"\/><\/figure><\/div>\n\n\n<p>Save the file with the following name:&nbsp;<strong>main.py<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/micropython-main-py-file.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Micropython saving main.py file Thonny IDE\" class=\"wp-image-132452\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/micropython-main-py-file.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/micropython-main-py-file.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<p><strong>Note<\/strong>: When you name a file <span class=\"rnthl rntliteral\">main.py<\/span>, the Raspberry Pi Pico will run that file automatically on boot. If you call it a different name, it will still be saved on the board filesystem, but it will not run automatically on boot.<\/p>\n\n\n\n<p>Reset your board (unplug and plug it into your computer). Click the little green button &#8220;Run Current Script&#8221; or press F5.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"78\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/thonny-ide-run-code.png?resize=450%2C78&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"run script on thonny ide\" class=\"wp-image-132505\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/thonny-ide-run-code.png?w=450&amp;quality=100&amp;strip=all&amp;ssl=1 450w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/thonny-ide-run-code.png?resize=300%2C52&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/figure><\/div>\n\n\n<p>New temperature readings will be published on the shell approximately every five seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"679\" height=\"305\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20-temperature-sensor-raspberry-pi-pico-micropython-demonstration.png?resize=679%2C305&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"testing script on thonny ide raspberry pi pico ds18b20\" class=\"wp-image-133329\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20-temperature-sensor-raspberry-pi-pico-micropython-demonstration.png?w=679&amp;quality=100&amp;strip=all&amp;ssl=1 679w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20-temperature-sensor-raspberry-pi-pico-micropython-demonstration.png?resize=300%2C135&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 679px) 100vw, 679px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"multiple-ds18b20\">Raspberry Pi Pico with Multiple Temperature Sensors (MicroPython)<\/h2>\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 GPIO. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-multiple-ds18b20-sensors.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with Multiple Temperature Sensors (Arduino IDE)\" class=\"wp-image-133300\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-multiple-ds18b20-sensors.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-multiple-ds18b20-sensors.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<h3 class=\"wp-block-heading\" id=\"Rpi-Pico-multiple-ds18b20-sensors-wiring\">Wiring Multiple Temperature Sensors to the Raspberry Pi Pico<\/h3>\n\n\n\n<p>To read temperature from multiple sensors on the same one-wire bus, you just need to wire all data lines together as shown in the following schematic diagram (as an example we&#8217;re using four sensors, but you can use two, three, or more) just make sure you have a 4.7k Ohm resistor on the data line:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"716\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-multiple-ds18b20-wiring_bb.png?resize=850%2C716&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Wiring Multiple DS18B20 Sensors\" class=\"wp-image-133319\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-multiple-ds18b20-wiring_bb.png?w=850&amp;quality=100&amp;strip=all&amp;ssl=1 850w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-multiple-ds18b20-wiring_bb.png?resize=300%2C253&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/raspberry-pi-pico-multiple-ds18b20-wiring_bb.png?resize=768%2C647&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"multiple-ds18b20-raspberry-pi-pico-micropython\">Getting Temperature from Multiple DS18B20 Sensors<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/multiple-ds18b20-sensors.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"multiple DS18B20 temperature sensors\" class=\"wp-image-133316\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/multiple-ds18b20-sensors.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/multiple-ds18b20-sensors.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 get temperature from multiple DS18B20 temperature sensors, you can use the same previous script. You just need to wire more DS18B20 sensors. These sensors share the same data line \u2013 in this case, all sensors are wired to&nbsp;GPIO22. We just added two lines of code to print the number of found sensors, the remainder of the code is the same.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-ds18b20-micropython\/\n\nimport machine, onewire, ds18x20, time\n\nds_pin = machine.Pin(22)\nds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))\n\nroms = ds_sensor.scan()\nnumber_devices = len(roms)\nprint('Number of found devices: ', number_devices)\nprint('Adresses: ', roms)\n\nwhile True:\n  ds_sensor.convert_temp()\n  time.sleep_ms(750)\n  for rom in roms:\n    print(rom)\n    \n    tempC = ds_sensor.read_temp(rom)\n    tempF = tempC * (9\/5) +32\n    \n    print('temperature (\u00baC):', &quot;{:.2f}&quot;.format(tempC))\n    print('temperature (\u00baF):', &quot;{:.2f}&quot;.format(tempF))\n    print()\n    \n  time.sleep(5)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/main_DS18B20_Multiple_Sensors_Addresses.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After wiring the circuit, and uploading and running the code on the Raspberry Pi Pico, it will start printing the temperature for each sensor. <\/p>\n\n\n\n<p>First, it will display the address of each sensor and then the corresponding temperature.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20_micropython_raspberry_pi_pico_multiple_sensors.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"876\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20_micropython_raspberry_pi_pico_multiple_sensors.png?resize=768%2C876&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with DS18B20 Temperature Sensor MicroPython Single and Multiple demonstration\" class=\"wp-image-133331\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20_micropython_raspberry_pi_pico_multiple_sensors.png?w=768&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ds18b20_micropython_raspberry_pi_pico_multiple_sensors.png?resize=263%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 263w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to interface the DS18B20 temperature sensor with the Raspberry Pi Pico and how to get temperature readings using MicroPython firmware. The DS18B20 is an addressable temperature sensor. This means you can wire multiple sensors on the same GPIO and get temperature readings from all sensors all at once. We also covered that in our tutorial.<\/p>\n\n\n\n<p>Learn more about the Raspberry Pi Pico with our exclusive eBook:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\"><strong>Learn Raspberry Pi Pico with MicroPython (eBook)<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<p>If you like the Raspberry Pi Pico, make sure you take a look at some of the other tutorials on our blog:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\">Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-outputs-inputs-micropython\/\">Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-pwm-micropython\/\">Raspberry Pi Pico: PWM Fading an LED (MicroPython)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-micropython\/\">Raspberry Pi Pico: DHT11\/DHT22 Temperature and Humidity Sensor (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\">Check out all our Raspberry Pi Pico Guides \u00bb<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to interface the Raspberry Pi Pico with the DS18B20 temperature sensor and how to get temperature readings by programming your board using MicroPython firmware. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: DS18B20 Temperature Sensor (MicroPython) &#8211; Single and Multiple\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-ds18b20-micropython\/#more-133323\" aria-label=\"Read more about Raspberry Pi Pico: DS18B20 Temperature Sensor (MicroPython) &#8211; Single and Multiple\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":133333,"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":[324,326],"tags":[],"class_list":["post-133323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Raspberry-Pi-Pico-DS18B20-MicroPython.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\/133323","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=133323"}],"version-history":[{"count":15,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/133323\/revisions"}],"predecessor-version":[{"id":154871,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/133323\/revisions\/154871"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/133333"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=133323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=133323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=133323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}