DS18B20 code to set Resolution and return Temperature

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
pmulvey
Posts: 45
Joined: Sun Jul 29, 2018 8:12 am
Location: Athlone, Ireland

DS18B20 code to set Resolution and return Temperature

Post by pmulvey » Mon Aug 13, 2018 11:31 am

I have rejigged Arduino and micropython code to produce a Function that returns the temperature with input parameters of Pin No and Resolution (9-12 bits).

Code: Select all

def Temperature(resolution, dsPin):
    dat = machine.Pin(dsPin)
    ds = DS18X20(onewire.OneWire(dat))
    roms = ds.scan()
    for rom in roms:
        if resolution == 9:
            config = b'\x00\x00\x1f'
        if resolution == 10:
            config = b'\x00\x00\x3f'
        if resolution == 11:
            config = b'\x00\x00\x5f'
        if resolution == 12:
            config = b'\x00\x00\x7f'
        ds.write_scratch(rom, config)
    ds.convert_temp()
    time.sleep_ms(int(750 / (2** (12 - resolution))))
    t = 0
    for rom in roms:
        t = ds.read_temp(rom)
    return t
Paul Mulvey

kpg141260
Posts: 1
Joined: Mon Feb 07, 2022 1:49 am

Re: DS18B20 code to set Resolution and return Temperature

Post by kpg141260 » Mon Feb 07, 2022 2:03 am

Hi Paul,

thanks for posting this example. I found it useful.

Pete

Post Reply