DS18B20 code to set Resolution and return Temperature
Posted: 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