from machine import Pin, PWM, ADC
import utime
import math
# Sensor Connections
thermistor_pin = machine.Pin(26)
adc = machine.ADC(thermistor_pin)
led = machine.Pin(5, machine.Pin.OUT)
buzz = PWM(Pin(6))
# LCD Connections
rs = machine.Pin(8, machine.Pin.OUT)
e = machine.Pin(9, machine.Pin.OUT)
d4 = machine.Pin(10, machine.Pin.OUT)
d5 = machine.Pin(11, machine.Pin.OUT)
d6 = machine.Pin(12, machine.Pin.OUT)
d7 = machine.Pin(13, machine.Pin.OUT)
def pulseE():
e.value(1)
utime.sleep_us(40)
e.value(0)
utime.sleep_us(40)
def send2LCD4(BinNum):
d4.value((BinNum & 0b00000001) >> 0)
d5.value((BinNum & 0b00000010) >> 1)
d6.value((BinNum & 0b00000100) >> 2)
d7.value((BinNum & 0b00001000) >> 3)
pulseE()
def send2LCD8(BinNum):
d4.value((BinNum & 0b00010000) >> 4)
d5.value((BinNum & 0b00100000) >> 5)
d6.value((BinNum & 0b01000000) >> 6)
d7.value((BinNum & 0b10000000) >> 7)
pulseE()
d4.value((BinNum & 0b00000001) >> 0)
d5.value((BinNum & 0b00000010) >> 1)
d6.value((BinNum & 0b00000100) >> 2)
d7.value((BinNum & 0b00001000) >> 3)
pulseE()
def setUpLCD():
rs.value(0)
send2LCD4(0b0011)
send2LCD4(0b0011)
send2LCD4(0b0011)
send2LCD4(0b0010)
send2LCD8(0b00101000)
send2LCD8(0b00001100)
send2LCD8(0b00000110)
send2LCD8(0b00000010)
utime.sleep_ms(2)
def send_string_to_lcd(string):
rs.value(1)
for char in string:
send2LCD8(ord(char))
def read_temperature():
beta = 3950
series_resistor = 10000
adc_value = adc.read_u16()
resistance = series_resistor / (65535 / adc_value - 1)
# Using the Steinhart-Hart equation to calculate the temperature
steinhart = resistance / 10000 # (R/Ro)
steinhart = math.log(steinhart) # ln(R/Ro)
steinhart /= beta # 1/B * ln(R/Ro)
steinhart += 1.0 / (25 + 273.15) # + (1/To)
steinhart = 1.0 / steinhart # Invert
steinhart -= 273.15 # Convert to Celsius
return steinhart
# Main loop
setUpLCD()
#send_string_to_lcd(" DANGER!")
while True:
temperature = read_temperature()
print("Temperature:", temperature, "°C")
if temperature > 45:
led.on()
#send_string_to_lcd("Temperature>45C")
send_string_to_lcd(" DANGER!")
buzz.duty_u16(3000)
buzz.freq(3000)
utime.sleep(2)
buzz.duty_u16(0)
utime.sleep(temperature / 1000)
utime.sleep(1)
if temperature < 45:
led.off()
send_string_to_lcd("SAFE")
utime.sleep(2)