Raspberry Pi Pico - Temperature Sensor

This guide shows you how to use the DS18B20 1-wire temperature sensor with the Raspberry Pi Pico. We will cover:

Raspberry Pi Pico temperature sensor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of One Wire Temperature Sensor DS18B20

Pinout

The DS18B20 temperature sensor has three pins.

  • GND pin: Attach it to GND (0 volts).
  • VCC pin: Attach it to VCC (either 5 volts or 3.3 volts).
  • DATA pin: This is used for 1-Wire Data. Connect it to a digital pin on the Raspberry Pi Pico.

There are two kinds of sensors: the TO-92 package, which resembles a transistor, and the waterproof probe. In this guide, we will use the waterproof probe.

DS18B20 temperature sensor Pinout

To connect a DS18B20 temperature sensor to a Raspberry Pi Pico, you need a pull-up resistor. This can be complex. However, some manufacturers offer a wiring adapter that includes a pull-up resistor and a screw terminal block, making the setup easier.

Wiring Diagram

  • Diagram of Breadboard Connections
The wiring diagram between Raspberry Pi and Pico Temperature Sensor

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram and adapter instructions
The wiring diagram between Raspberry Pi and Pico DS18B20

This image is created using Fritzing. Click to enlarge image

We suggest getting a DS18B20 sensor with a wiring adapter. This adapter helps you easily connect the sensor as it already has a resistor included, so you do not need another one.

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-temperature-sensor """ import machine import onewire import ds18x20 import time ds_pin = machine.Pin(26) # The Raspberry Pi Pico pin GP26 connected to the DS18B20 sensor # Create the onewire object OneWire = onewire.OneWire(ds_pin) # Create the DS18X20 object DS18B20 = ds18x20.DS18X20(OneWire) # Scan for devices on the bus sensor_addresses = OneWire.scan() print('Found DS18B20 devices: ', sensor_addresses) # Main loop to read and print the temperature every second while True: DS18B20.convert_temp() time.sleep_ms(750) for address in sensor_addresses: temperature = DS18B20.read_temp(address) print('Temperature: {:.2f} °C'.format(temperature)) time.sleep(1)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Follow the given diagram to attach the DS18B20 1-wire temperature sensor to the Raspberry Pi Pico.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Put the sensor in hot and cold water, or hold it in your hand.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot Found DS18B20 devices: [bytearray(b'(\xffP\x05.\x04\x00\x07')] Temperature: 27.44 °C Temperature: 27.44 °C Temperature: 27.44 °C Temperature: 27.44 °C Temperature: 27.50 °C Temperature: 28.25 °C Temperature: 29.06 °C Temperature: 29.69 °C Temperature: 30.13 °C Temperature: 30.50 °C Temperature: 30.69 °C Temperature: 30.75 °C
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!