Raspberry Pi Pico: DS18B20 Temperature Sensor (MicroPython) – Single and Multiple

In this guide, you’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’ll also show you how to get temperature from multiple DS18B20 sensors all at once.

Raspberry Pi Pico with DS18B20 Temperature Sensor MicroPython Single and Multiple

Do you prefer to program the Raspberry Pi Pico using Arduino IDE? Check this tutorial instead: Raspberry Pi Pico: DS18B20 Temperature Sensor (Arduino IDE) – Single and Multiple.

Table of Contents

Prerequisites – MicroPython Firmware

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.

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.

Introducing DS18B20 Temperature Sensor

The DS18B20 temperature sensor 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.

It can be powered by an external power supply or it can derive power from the data line (called “parasite mode”), which eliminates the need for an external power supply.

DS18B20 Temperature Sensor Pinout Pins

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.

The DS18B20 temperature sensor is also available in waterproof version.

DS18B20 Temperature Sensor Waterproof version

Here’s a summary of the most relevant specs of the DS18B20 temperature sensor:

  • Communicates over one-wire bus communication
  • Power supply range: 3.0V to 5.5V
  • Operating temperature range: -55ºC to +125ºC
  • Accuracy +/-0.5 ºC (between the range -10ºC to 85ºC)

For more information consult the DS18B20 datasheet.

Parts Required

To follow this tutorial you need the following parts:

DS18B20 Temperature Sensor dallas version

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Wiring the DS18B20 Temperature Sensor to the Raspberry Pi Pico

The DS18B20 temperature sensor comes with three pins: GND, data, and VCC. the DS18B20 temperature sensor can be powered through the VDD pin (normal mode), or it can derive its power from the data line (parasite mode). You can choose either mode. We prefer using the normal mode.

DS18B20 Temperature Sensor Pinout Pins

If you’re using the waterproof version, you can identify each pin by its color:

  • black: GND
  • red: VDD
  • yellow: data line
DS18B20Raspberry Pi Pico
GNDGND
Data (DQ)Any digital GPIO (we’ll be using GPIO22);
you also need to connect a 4.7KOhm resistor between the data line and VCC
VDD3V3(OUT)
You can also take a look at the following schematic diagram:
Raspberry Pi Pico DS18B20 Single Wiring

Raspberry Pi Pico with DS18B20 – Get Temperature (MicroPython Code)

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 ds18x20 on the MicroPython firmware, so you don’t need to install any library.

# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-ds18b20-micropython/

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)
    tempC = ds_sensor.read_temp(rom)
    tempF = tempC * (9/5) +32
    print('temperature (ºC):', "{:.2f}".format(tempC))
    print('temperature (ºF):', "{:.2f}".format(tempF))
    print()
  time.sleep(5)

View raw code

Continue reading to learn how the code works, or skip to the Demonstration section.

Start by including the required modules. The machine module for accessing the hardware, the onewire for communicating with the DS18B20 sensor(s), the ds18x20 for reading temperature, and time for time-related functions.

import machine, onewire, ds18x20, time

Define the GPIO pin that the DS18B20 sensor is connected to. We’re using GPIO22. Modify the code if you’re using a different pin.

ds_pin = machine.Pin(22)

Next, initialize the DS18B20 sensor by creating a ds18x20 object called ds_sensor on the ds_pin defined earlier

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

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.

The following line uses the scan() function to scan for DS18B20 sensors. The addresses found are saved on the roms variable (the roms variable is of type list).

roms = ds_sensor.scan()

Print the address of each sensor to the shell:

print('Found DS devices: ', roms)

Then, there’s a while loop that gets the temperature from the DS18B20 sensor(s) every 5 seconds.

You need to call the convert_temp() function on the ds_sensor object each time you want to sample temperature.

ds_sensor.convert_temp()

Add a delay of 750 ms to give enough time to convert the temperature:

time.sleep_ms(750)

After that, we can read the temperature on the addresses found earlier by using the read_temp() method and passing the address as an argument as shown in the following line of code.

tempC = ds_sensor.read_temp(rom)

By default, it returns the temperature in Celsius degrees. The following line converts it to Fahrenheit.

tempF = tempC * (9/5) +32

Because you can add multiple sensors to the same data line, we have a for loop that goes through all the addresses and prints the temperature for each of them (in this case, it’s just one sensor):

for rom in roms:
  print(rom)

In each loop, we print the temperature in Celsius degrees and in Fahrenheit degrees. The result will have two decimal places {:.2f}.

print('temperature (ºC):', "{:.2f}".format(tempC))
print('temperature (ºF):', "{:.2f}".format(tempF))

The loop repeats every five seconds:

time.sleep(5)

Demonstration

Save the code to your Raspberry Pi Pico board using Thonny IDE or any other MicroPython IDE of your choice.

Copy the code provided to a new file on Thonny IDE.

Thonny IDE Micropython DS18B20 Code Raspberry Pi Pico

With the code copied to the file, click on the Save icon. Then, select Raspberry Pi Pico.

Saving file to Raspberry Pi Pico MicroPython IDE

Save the file with the following name: main.py.

Micropython saving main.py file Thonny IDE

Note: When you name a file main.py, 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.

Reset your board (unplug and plug it into your computer). Click the little green button “Run Current Script” or press F5.

run script on thonny ide

New temperature readings will be published on the shell approximately every five seconds.

testing script on thonny ide raspberry pi pico ds18b20

Raspberry Pi Pico with Multiple Temperature Sensors (MicroPython)

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.

Raspberry Pi Pico with Multiple Temperature Sensors (Arduino IDE)

Wiring Multiple Temperature Sensors to the Raspberry Pi Pico

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’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:

Raspberry Pi Pico Wiring Multiple DS18B20 Sensors

Getting Temperature from Multiple DS18B20 Sensors

multiple DS18B20 temperature sensors

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 – in this case, all sensors are wired to GPIO22. We just added two lines of code to print the number of found sensors, the remainder of the code is the same.

# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-ds18b20-micropython/

import machine, onewire, ds18x20, time

ds_pin = machine.Pin(22)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))

roms = ds_sensor.scan()
number_devices = len(roms)
print('Number of found devices: ', number_devices)
print('Adresses: ', roms)

while True:
  ds_sensor.convert_temp()
  time.sleep_ms(750)
  for rom in roms:
    print(rom)
    
    tempC = ds_sensor.read_temp(rom)
    tempF = tempC * (9/5) +32
    
    print('temperature (ºC):', "{:.2f}".format(tempC))
    print('temperature (ºF):', "{:.2f}".format(tempF))
    print()
    
  time.sleep(5)

View raw code

Demonstration

After wiring the circuit, and uploading and running the code on the Raspberry Pi Pico, it will start printing the temperature for each sensor.

First, it will display the address of each sensor and then the corresponding temperature.

Raspberry Pi Pico with DS18B20 Temperature Sensor MicroPython Single and Multiple demonstration

Wrapping Up

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.

If you like the Raspberry Pi Pico, make sure you take a look at some of the other tutorials on our blog:

Check out all our Raspberry Pi Pico Guides »



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

2 thoughts on “Raspberry Pi Pico: DS18B20 Temperature Sensor (MicroPython) – Single and Multiple”

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.