# import from libraries
from machine import Pin, ADC, I2C
import utime
import math
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# temperature sensor input pin
TSENSOR_INPUT_PIN = 26
# converting the sensor reading to temperature in celsius
def adc2celsius(x):
BETA = 3950
KELVIN = 273.15
return (1 / (math.log(1 / (65535 / x - 1)) / BETA + 1 / 298.15) - KELVIN)
def main():
# Inisialisasi ADC untuk sensor suhu
tempsensor = ADC(TSENSOR_INPUT_PIN)
# Inisialisasi pin untuk relay dan LED
relay_pin = Pin(16, Pin.OUT)
led_pin = Pin(17, Pin.OUT) # Ganti dengan pin yang berbeda
# Inisialisasi I2C untuk LCD
I2C_ADDR = 0x27 # Ganti dengan alamat I2C yang sesuai untuk LCD Anda
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
i2c = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000) # Sesuaikan pin SDA dan SCL
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
# Fungsi untuk menghidupkan relay dan LED
def turn_on():
relay_pin.high() # Set the relay pin high
led_pin.high() # Set the LED pin high
# Fungsi untuk mematikan relay dan LED
def turn_off():
relay_pin.low() # Set the relay pin low
led_pin.low() # Set the LED pin low
# Menampilkan pesan awal di LCD
lcd.backlight_on()
lcd.putstr("Suhu: C")
while True:
reading = tempsensor.read_u16()
celsius = adc2celsius(reading)
print(f"{celsius:.1f}".replace('.', ',')) # Menampilkan suhu dengan satu angka desimal
# Menampilkan suhu pada LCD
lcd.move_to(0, 1) # Pindah ke baris kedua
lcd.putstr(f"{celsius:.1f} C") # Menampilkan suhu di LCD
utime.sleep(1.0)
if celsius > 32: # Ganti dengan nilai ambang batas yang sesuai
print("higher temp")
turn_on() # Hidupkan relay dan LED
else:
turn_off() # Matikan relay dan LED
if __name__ == "__main__":
main()