7

I will be getting my Raspberry pi pico here in a few days and want to connect a DS18B20 and use MicroPython. I have a project the uses RPi zero w and Python3.9. Since obviously, the pico does not use an os, is there a way to read a 1-wire device? Or do I need to switch to a different method for reading temperature? I know that the ESP32 port has a class for it. Any help would be appreciated. Thank you in advance.

Kelby Criswell
  • 320
  • 2
  • 9

2 Answers2

8

Yes, you can use a DS18B20 with MicrPython on the Pico. All the required modules are included. The following is based on this MicroPython tutorial from Random Nerd Tutorials.

Hardware

To wire the sensor to your Pico:

  • Connect the sensor's ground to one of the ground pins on the Pico.
  • Connect the sensor's VCC pin to the Pico's 3.3V(out) pin.
  • Connect the Sensor's data pin to the Pico's GPIO4

Raspberry Pi Pico Pinout

Software

  1. Create a new file in Thonny.
  2. Paste the following code into your new file:

    import machine, onewire, ds18x20, time
ds_pin = machine.Pin(4)
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)

  1. Save the file to your pico as ds18b20.py.
  2. Click the run button in Thonny and you should see temperature readings appear in Thonny's shell every 5 seconds.
Steve Robillard
  • 34,988
  • 18
  • 106
  • 110
0

It does work, but there's no error checking. Remove the sensor lead ( or put it in the wrong GPIO slot ) and you get a python error in convert temp. I'd rather print a warning like "Plug in the sensor, dummy!" and then stop before it gets to that point.