from machine import Pin, I2C, ADC, Timer
from pico_i2c_lcd import I2cLcd
from math import log
sda = Pin(0)
scl = Pin(1)
i2c = I2C(0, sda=sda, scl=scl, freq=40000)
devices = i2c.scan()
if len(devices) <= 0:
print("No devices found!")
else:
print(hex(devices[0]))
i2c_addr = devices[0]
lcd = I2cLcd(i2c, i2c_addr, 2, 16)
green = Pin(2, Pin.OUT)
yellow = Pin(3, Pin.OUT)
red = Pin(16, Pin.OUT)
adc = ADC(0)
BETA = 3950
cnt = 0
def fn(timer):
lcd.clear()
raw_value = adc.read_u16()
temp_celcius = (1/ (log(1/(65535/raw_value -1))/BETA + (1/298.15))) - 273.15
global cnt
if temp_celcius >= 10 and temp_celcius <= 33:
red.low()
lcd.putstr(str(temp_celcius))
green.toggle()
cnt = 0
else:
if cnt <= 5:
green.low()
yellow.toggle()
cnt += cnt + 1
else:
yellow.low()
red.toggle()
lcd.putstr("DANGER")
timer = Timer(freq=1, mode=Timer.PERIODIC, callback=fn)