# Import the necessary libraries
import time, math
from machine import ADC, Pin, SoftI2C
from i2c_lcd import LiquidCrystalI2C
# Define the beta value of the thermistor, typically provided in the datasheet
beta = 3950
adc_pin = 35
LCD_COLS = 16
LCD_ROWS = 2
LED = Pin(4, Pin.OUT)
# Intantiate the lcd object from the I2C class
i2c = SoftI2C(sda=Pin(21), scl=Pin(22))
lcd = LiquidCrystalI2C(i2c, 0x27, LCD_COLS, LCD_ROWS)
# Create an ADC object (thermistor)
thermistor = ADC(Pin(adc_pin, Pin.IN))
# Set the attenuation and bit width
thermistor.atten(thermistor.ATTN_11DB)
thermistor.width(thermistor.WIDTH_12BIT)
# Start an infinite loop to continuously monitor the temperature
while True:
# Read the adc values
Vr = thermistor.read()
# Calculate the resistance of the thermistor based on the measured voltage
Rt = Vr/4
# Use the beta parameter and resistance value to calculate the temperature in Celsius
temp_Cel = 1 / (math.log(1 / (1023/ Rt - 1)) / beta + 1.0 / 298.15) - 273.15
# Convert to Fahrenheit
temp_Fah = temp_Cel * 1.8 + 32
if(temp_Cel>70):
LED.value(1)
elif(temp_Cel>=65 and temp_Cel<66):
LED.value(1)
time.sleep(0.5)
LED.value(0)
elif(temp_Cel>=66 and temp_Cel<67):
LED.value(1)
time.sleep(0.4)
LED.value(0)
elif(temp_Cel>=67 and temp_Cel<68):
LED.value(1)
time.sleep(0.3)
LED.value(0)
elif(temp_Cel>=68 and temp_Cel<69) :
LED.value(1)
time.sleep(0.2)
LED.value(0)
elif(temp_Cel>=69 and temp_Cel<70):
LED.value(1)
time.sleep(0.1)
LED.value(0)
else:
LED.value(0)
# Display the temperature values in both Celsius and Fahrenheit
lcd.backlight()
lcd.set_cursor(0, 0)
lcd.print('Cel: %.2f \xDFC \n' % temp_Cel)
lcd.set_cursor(0, 1)
lcd.print('Fah: %.2f \xDFF' % temp_Fah)
lcd.display()
time.sleep(.1)