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.
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
- Introducing DS18B20 Temperature Sensor
- Raspberry Pi Pico with DS18B20 (single) Wiring
- Installing the microDS18B20 Library
- Getting DS18B20 Temperature Readings (single) – MicroPython Code
- Raspberry Pi Pico with Multiple DS18B20 Sensors
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.
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.
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:
- Raspberry Pi Pico
- DS18B20 temperature sensor (one or multiple sensors) – waterproof version
- 4.7k Ohm resistor
- Jumper wires
- Breadboard
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.
If you’re using the waterproof version, you can identify each pin by its color:
- black: GND
- red: VDD
- yellow: data line
DS18B20 | Raspberry Pi Pico |
GND | GND |
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 |
VDD | 3V3(OUT) |
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)
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.
With the code copied to the file, click on the Save icon. Then, select Raspberry Pi Pico.
Save the file with the following name: main.py.
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.
New temperature readings will be published on the shell approximately every five seconds.
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.
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:
Getting Temperature from Multiple DS18B20 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)
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.
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.
Learn more about the Raspberry Pi Pico with our exclusive eBook:
If you like the Raspberry Pi Pico, make sure you take a look at some of the other tutorials on our blog:
Why wasn’t the standard Temp36 used?
Can I you use both or is there a difference?
Are you referring to the internal sensor of the board?
Regards,
Sara
When I wired up a single waterproof DS18B20 as diagrammed, the code you provided failed to find any ds_sensor devices – roms returned []
I removed the 4.7 k resistor and it worked fine.