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.
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.
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
- Create a new file in Thonny.
- 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)
- Save the file to your pico as ds18b20.py.
- Click the run button in Thonny and you should see temperature readings appear in Thonny's shell every 5 seconds.