# Imports
from machine import Pin, ADC, I2C
from pico_i2c_lcd import I2cLcd
from time import sleep
from math import log
def main():
# a tiny sleep to allow the first print to be displayed
print('Program starting')
# code below here
# Variables
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)
address = 0x27
rows = 4
columns = 20
BETA = 3950
string1 = ""
prev_string = ""
# Pins
relay = Pin(13, Pin.OUT)
ntc = ADC(Pin(26))
pot = ADC(Pin(27))
lcd = I2cLcd(i2c, address, rows, columns)
# LCD Reset
lcd.clear()
# Main Loop
while True:
# Start Delay
sleep(0.02)
# Readings
temp = ntc.read_u16()
humi = pot.read_u16()
# Temperature Conversion
celsius = round(1 / (log(1 / (65535. / temp - 1)) / BETA + 1.0 / 298.15) - 273.15, 2)
# Humidity conversion to percentage
humi_percent = round((humi / 65535) * 100, 1)
# LCD Displaying
lcd.move_to(0, 0)
lcd.putstr("Smart Irrigation")
lcd.move_to(0, 1)
lcd.putstr(f"Temperature: {celsius}{chr(223)}C")
lcd.move_to(0, 2)
lcd.putstr(f"Humidity: {humi_percent}%")
lcd.move_to(0, 3)
if humi_percent < 30.0:
relay.value(1)
string1 = "Pump: Irrigating"
else:
relay.value(0)
string1 = "Pump: Idle "
if string1 != prev_string:
lcd.move_to(0, 3)
lcd.putstr(string1)
prev_string = string1
sleep(5)
if __name__ == "__main__":
main()