from machine import ADC, Pin, I2C
import utime
from pico_i2c_lcd import I2cLcd
import tm1637
# Initialize I2C using GPIO 26 (SDA) and GPIO 27 (SCL)
i2c = I2C(id=0, scl=Pin(9), sda=Pin(8), freq=100000)
# Initialize the LCD
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Sensor and SetPoint Inputs
AnalogIn = ADC(0) # GP26 - Sensor input (TMP36 or LM35)
SetPoint = ADC(1) # GP27 - Setpoint adjustment (potentiometer)
# Conversion Constants
Conv = 3300 / 65535
Conv1 = 150 / 3300 # Conversion factor
# Output Pins
LED = Pin(15, Pin.OUT)
Relay = Pin(16, Pin.OUT)
LED.value(0)
Relay.value(0)
while True:
# Read and convert setpoint value
ValorPot = SetPoint.read_u16()
TempSP = Conv * ValorPot
SetTemp = TempSP * Conv1
# Read and convert sensor value
V = AnalogIn.read_u16()
mV = V * Conv
RoomTemp = (mV - 500.0) / 10.0 # For TMP36, T = (Vo - 500) / 10
# Display values on the LCD
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr('Temp: {:.2f} C'.format(RoomTemp))
lcd.move_to(0, 1)
lcd.putstr('Set: {:.2f} %'.format(SetTemp))
# Control Relay and LED based on temperature comparison
if RoomTemp < SetTemp:
Relay.value(1)
LED.value(1)
else:
Relay.value(0)
LED.value(0)
utime.sleep(1)