DS18B20 with Raspberry Pi Pico using MicroPython

DS18B20 with Raspberry Pi Pico using MicroPython - Robocraze

Summary

DS18B20 is a 1-wire programmable temperature sensor which requires only one data line for communication. Today we will highlight the important facts on DS18B20. Also, we would learn how to use DS18B20 with Raspberry Pi Pico using a micro python. So, let's begin!

Introduction

The Raspberry Pi Pico is a microcontroller board which, when combined with the DS18B20 temperature sensor from Maxim Integrated, provides users an easy and reliable way to measure temperatures. With this combination of products, it's now possible for cost-effective monitoring or even smart home automation projects. The Raspberry Pico itself offers robust performance features such as built-in USB support and native MicroPython programming environment so that you can implement your project quickly without having to learn too much about coding languages at the moment.

Furthermore, due to its higher GPIO speeds than previous versions of the RPi boards - up to 50 MHz - more complex applications become achievable while conserving power in comparison between other alternatives available on market today. All these great qualities make Raspberry Pi Pico coupled with DS18b20 ideal choice for measuring temperatures accurately yet reliably saving energy resources in long run!

What is DS18B20? 

The DS18B20 is a 1-wire programmable temperature sensor from Maxim integrated that requires only one data line for communication with a central microprocessor. Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area.

Applications  

  1. Thermostatic Controls  
  2. Industrial Systems  
  3. Consumer Products  
  4. Thermometers  
  5. Thermally Sensitive Systems 

Material Required is as follows: 

  1. Raspberry Pi Pico 
  2. DS18B20 

Circuit Diagram 

raspberry pi pico ds18b20

  1. Connect the sensor's ground to one of the ground pins on the Pico.
  2. Connect the sensor's VCC pin to the Pico's 3.3V(out) pin.

Code and Library

Setup library
  1. For DS18B20 we need OneWire and DS18X20 libraries. Download from Github
  2. Visit the link for the respective library.
  3. Click on the file present inside the downloaded folder and then copy the contents of the entire file .
  4. Click on the “New” button on the Thonny IDE to open a blank script and paste the copied code.
  5. Click on the save button and when prompted to choose
  6. When asked for save location, choose Raspberry Pi Pico.4
  7. Once the dialog box opens to choose the save location on the Pico, double click on the “lib” folder present already to save the file inside it. If the lib folder does not exist, create it and then save the file inside it.
  8. Enter the same name for the file as on the GitHub repo and press the Ok button. To add the downloaded library, open Thonny and navigate to the following directory in the Pico.

Thonny > File > Open > Raspberry Pi Pico > lib

  1. Copy the library code and save the file with .py extension.
 Code
 
import machine, onewire, ds18x20, time

ds_pin = machine.Pin(22)

ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))

roms = ds_sensor.scan()

print('Found DS devices: ', roms)

while True:

  ds_sensor.convert_temp()

  time.sleep_ms(750)

  for rom in roms:

    print(rom)

    print(ds_sensor.read_temp(rom))

  time.sleep(5)

Copy the above code into thonny ide and run the code.

ds18b20 raspberry pi pico

You can see output into Thonny Shell

Troubleshooting
After executing the program and error still occurs, like the one below.

pi pico ds18b20

Solution: Check your connections, make sure the connections are correct and not loose. Also, check-in the code if the pin number is correct.

Components and Supplies

    You may also like to read

    Frequently Asked Questions

    Back to blog

    Leave a comment

    Please note, comments need to be approved before they are published.

    Components and Supplies

      You may also like to read