I am running pico 2 w and have a ds18b20 connected. I am using Thonny. This is the code i am using:
import time
import machine
import onewire, ds18x20
# the device is on GPIO12
dat = machine.Pin(22)
# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))
print('ds: ', ds)
# scan for devices on the bus
roms = ds.scan()
print('found devices:', roms)
# loop 10 times and print all temperatures
for i in range(10):
print('temperatures:', end=' ')
ds.convert_temp()
time.sleep_ms(1500)
for rom in roms:
print(ds.read_temp(rom), end=' ')
print()
And this is what i am getting:
MPY: soft reboot
ds: <DS18X20 object at 20012630>
found devices: []
temperatures: Traceback (most recent call last):
File "<stdin>", line 19, in <module>
File "ds18x20.py", line 1, in convert_temp
File "onewire.py", line 1, in reset
OneWireError:
I have a 4.7k connected between data and VCC.
As you can see the ‘object’ is detected, so where am i going wrong?
To help us look into this further, could you please provide photos of your wiring and the sensor, along with your complete code and the full error message? We’ll see whether we can reproduce your issue.
Sorry, from the photo you attached we can’t make out exactly how the DS18B20 probe is wired, so you may want to check with the DS18B20 seller whether this wiring is correct.
One more thing to note: your code has the DS18B20 connected to GP22, but from your photo, nothing is connected to GP22 on the Pico 2 W. Your DS18B20 probe runs to the 4.7K resistor board, and the 4.7K resistor board is then connected to GP0 and GP9 on the Pico 2 W — we’re not sure why it’s wired this way, but it isn’t connected to GP22 or GP12, so please double-check your wiring.
I also have a Pico 2 W here, so I compared it against your photo. With the Pico 2 W in the same orientation, GP22 is on the opposite side — and the yellow wire in your photo is actually connected to GP9, not GP22 as the code specifies. That is most likely why scan() returns an empty list and throws OneWireError: the pin in the code does not match the actual wiring.
Please fix it in either of these two ways:
Move the yellow data wire from GP9 to GP22, keeping the code as machine.Pin(22); or
Leave the wiring as it is and change the code to machine.Pin(9).
Thanks again for your patience and cooperation!
Thank you very much for coming back and sharing the final cause — we’re really glad the problem is solved!
Your diagnosis is spot on: if the GPIO header on the Pico 2 W is soldered on the other side of the board (pins pointing down), then counting the pins from the usual picture (pins pointing up) gives the wrong numbering. The pin you actually wired to then doesn’t match machine.Pin(22) in your code, so scan() returns an empty list and throws OneWireError. That is exactly what happened in your case, and it has nothing to do with the DS18B20 probe itself.
You mentioned that this might help others — we couldn’t agree more. If you’re willing, feel free to add your troubleshooting process and a photo of the header orientation to the original thread; we’re sure it will help others who run into a similar situation.
Thanks again for your patience and cooperation, and we wish you a speedy recovery. If you have any further questions, just let us know.