Problem reading temp with ds18b20

Hi

First time poster.

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?

Any help much appreciated!

1 Like

Your comment and your actual GPIO disagree

You say:

# the device is on GPIO12

dat = machine.Pin(22)

Your code is actually using GPIO22, not GPIO12.

If the DS18B20 data wire is physically connected to GPIO12, change it to:

dat = machine.Pin(12)

So sorry! My bad. It is on pin 22, I just forgot to change the comment as I copied and pasted the code from somewhere else.

BTW: is there a way to test the probe without it being attached to anything?

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.

Hi

Sorry about the delay in replying. I tore my Achilles tendon!

Here are the images. My lcd display is working fine. Hope you can see all the wires for the temperature sensor

Many thanks..

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.

Hi yes, sorry, not good pictures! Have removed the lcd wiring and the power cable to the pico and taken a new image.

So, I have white to ground, red to power and yellow to gpio 22.

I think this is right.

Many thanks.

Thank you for following up, and for the photos.

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!

AHHHHH !!!

There is the problem! The gpio pins on my pico have been soldered on the other side!

I was following the gpio pins from the picture below, expecting the pins to point up and not down.

I have a number of rpi A, B+ and Zero 2 w and have never had this problem.

Thank you for all your help and maybe this will help someone else!!

Hi George,

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.