0

I have one DS18B20 connected on a breadboard to a pi pico with a 4.7K pull-up. Using the example code:

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)
    tempC = ds_sensor.read_temp(rom)
    tempF = tempC * (9/5) +32
    print('temperature (ºC):', "{:.2f}".format(tempC))
    print('temperature (ºF):', "{:.2f}".format(tempF))
    print()
  time.sleep(5)

It works and returns the correct temperature but the ROM found by the scan doesn't make sense to me.

Found DS devices:  [bytearray(b'(\xf6i\x9b\x00\x00\x00I')]

I expected 64 bits formatted in some standard manner? Reducing the pull-up to 2.35K makes no difference. Am I missing something?

1
  • What exactly is your question? This is simply 0x28f6699b00000049
    – Milliways
    Commented Feb 8 at 6:00

2 Answers 2

0

There's a few confusing things here that are byproducts of the perhaps somewhat wacky way python formats bytearrays.1 At first glance they look essentially like hex strings:

bytearray(b'(\xf6i\x9b\x00\x00\x00I')
            ^    ^                ^

But in fact they are a mixture of hex notation and ASCII characters, the latter taking precedence when a value is in the printable range: decimal 32 / hex 20 / ascii ' ' (space) to decimal 126 / hex 7e / ascii '~', or escape printable values (eg. '\n' = 10 = \x0a).

Notice those parentheses aren't balanced because the second ( is actually the first byte of the array (also notice it is inside the quotes), an ascii character with value decimal 40 / hex 28. The other two ascii bytes are i and I, plus the five hex bytes = 64 bits.

From a linux console you can get a chart of ASCII values via ascii, though by default it doesn't show the escapable characters as such (but man ascii is sort of interesting). There are also such charts online.


  1. It's not unprecedented, there are various tools for manipulating "binary" data (where every byte has the full 8-bit 0-255 potential) that print things this way. This makes it easy to spot clumps of embedded intelligible text.
0

It is unclear what you are planning to do with the value read.

As goldilicks explained this a an ASCII representation (designed for communication links) which are (almost) unreadable by a human.
I always convert if I need to examine them.

It is simple to convert (the following prints as hex).

BA=bytearray(b'(\xf6i\x9b\x00\x00\x00I')
print(bytes(BA).hex())

You can also access each element of the array.

print(BA[0])
40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.