print("This is the irrigation system program")
print("Create by: mnafan")
print("Date: 29/4/2024")
# Simplified MicroPython code for soil moisture reading
from machine import Pin, ADC, SoftI2C, PWM
from utime import sleep
import oled_lib, servo_lib, ultrasonic_lib
#Pin declaration
potentiometer_pin = Pin(27, Pin.IN)
oled_pin = SoftI2C(scl=Pin(22), sda=Pin(21))
red_led = Pin(4, Pin.OUT)
blue_led = Pin(2, Pin.OUT)
buzzer = PWM(Pin(18, Pin.OUT))
servo_pin = Pin(19, Pin.OUT)
TRIG = Pin(12)
ECHO = Pin(14)
#Object name
display = oled_lib.SSD1306_I2C(width=128, height=64, i2c=oled_pin, external_vcc=False)
water = servo_lib.Servo(pin=servo_pin)
volume = ultrasonic_lib.HCSR04(trigger_pin=TRIG, echo_pin=ECHO, echo_timeout_us=500*2*30)
soil_sensor = ADC(potentiometer_pin)
threshold_moisture = 32000
while True:
moisture_level = soil_sensor.read_u16()
distance_mm = volume.distance_mm()
distance_cm = volume.distance_cm()
red_led.off()
blue_led.off()
buzzer.init(freq=1, duty=0)
print(moisture_level)
if (moisture_level < threshold_moisture)&(distance_cm < 300):
red_led.on()
buzzer.init(freq=2500, duty=200)
sleep(1)
red_led.off()
buzzer.init(freq=1, duty=0)
sleep(1)
water.move(angle=0)
print("Soil is dry. Water your plant!")
display.fill(1)
display.text("Soil is dry", x=5, y=10, col=0)
display.text("Water your", x=5, y=30, col=0)
display.text("plant", x=5, y=50, col=0)
display.show()
else:
blue_led.on()
water.move(angle=90)
print("Soil moisture is optimal.")
display.fill(1)
display.text("Soil moisture", x=5, y=10, col=0)
display.text("is optimal", x=5, y=30, col=0)
display.show()
sleep(3)