this is a really basic tutorial for using the Dallas DS18b20 temperature sensor
on the esp8266 port of micropython
i used the lolin esp8266 D1 mini board and a cheap ds18b20 breakout shield
.
the ds18b20 sensors are often sold in a waterproof sealed unit with just the wires exposed. You will need to pull up the data line (normally yellow or white) to VCC with a 4.7kOhm resistor. I am not covering this aspect in this tutorial but this is explained well elsewhere
-
OneWire and ds18x20 micropython library are both in the standard esp8266 port of micropython so it is really easy to get started.
-
The data line on this particular shield is connected to D2 on the Wemos (GPIO 4)
-
create a dat object to specify the Pin the one wire data is supplied through
-
pass the dat object into a onewire , and pass that into a ds18x20
-
each ds18b20 has a 64bit address fixed at manufacture which you access by the scan function, you need this address to run the read_temp function
-
a convert_temp function is called every time a reading is taken
-
then you can run read_temp function which returns temp in Celcius
dat = machine.Pin(4)
ds = ds18x20.DS18X20(onewire.OneWire(dat))
roms = ds.scan() #this should return a single entry in a byte array
while True:
ds.convert_temp()
time.sleep_ms(750) #temp conversion takes 750ms from datasheet
temp = ds.read_temp(roms[0])
print(temp)
time.sleep(5)