from machine import Pin, ADC, I2C
from pico_i2c_lcd.py import I2cLcd
import time
# I2C Configuration
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
# LCD Configuration
lcd = I2cLcd(i2c, 0x27, 2, 16) # Set the I2C address and LCD dimensions
# Moisture sensor configuration
moisture_sensor_pin = 26
adc = ADC(Pin(moisture_sensor_pin))
def read_moisture():
moisture_value = adc.read()
moisture_percentage = (moisture_value - 100) / 900 * 100
moisture_percentage = max(0, min(100, moisture_percentage))
return moisture_percentage
def display_moisture(moisture):
lcd.move_to(0, 0)
lcd.putstr("Moisture: {:.2f}%".format(moisture))
def main():
try:
while True:
moisture_level = read_moisture()
display_moisture(moisture_level)
time.sleep(2)
except KeyboardInterrupt:
print("Program terminated by user.")
if __name__ == "__main__":
main()