from machine import Pin, ADC, I2C
from time import sleep
from math import log
import ssd1306
led = Pin(22, Pin.OUT)
button = Pin(12, Pin.IN, Pin.PULL_DOWN)
sensor = ADC(32)
i2c = I2C(0)
display = ssd1306.SSD1306_I2C(128, 64, i2c)
BETA=3950
T0 = 25
def init_display():
display.fill(0)
display.text('Temperature', 20, 0, 1)
display.text('Sensor', 40, 8, 1)
display.text('_______________________', 1, 16, 1)
display.text('Push the button', 5, 32, 1)
while True:
button_state = button.value()
if button_state == 1:
led.value(1)
val_d = sensor.read()
val_a = val_d *3.3/4095
k=val_a/(3.3-val_a)
T = 1 / (log(1 / (4095. / val_d - 1)) / BETA + 1.0 / (T0+273.15)) - 273.15
#Possiamo scrivere T in funzione della tensione misurata (che compare nella costante k)
#T = 1 / (log(k) / BETA + 1.0 / (T0+273.15)) - 273.15;
#Calcolo della T a partire da una calibrazione statica
T2=(2.495-val_a)/0.034
print('ADCout=', val_d, '\tVm=', round(val_a,2),' V', '\tT = ', round(T,2), '°C', '\tT2=', round(T2,2), '°C')
init_display()
display.text('T = '+str(round(T,2))+' C', 25, 50, 1)
display.show()
led.value(0)
sleep(1)
else:
led.value(0)
init_display()
display.show()