from machine import Pin, ADC, PWM, I2C
import time
from ssd1306 import SSD1306_I2C
# Setup I2C for OLED Display
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = SSD1306_I2C(128, 64, i2c)
# Setup Potentiometer (ADC to read analog value)
pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB) # Range 0-3.3V
# Setup LDR (Light Dependent Resistor)
ldr = ADC(Pin(35))
ldr.atten(ADC.ATTN_11DB)
# Setup Buttons
btn_stop = Pin(32, Pin.IN, Pin.PULL_UP) # Turn off the relay (waterpump) and turn on the buzzer
btn_start = Pin(33, Pin.IN, Pin.PULL_UP) # Button to manually start system
btn_reset = Pin(25, Pin.IN, Pin.PULL_UP) # Button to reset program
btn_extra = Pin(26, Pin.IN, Pin.PULL_UP) # Additional button
# Setup Relays
relay1 = Pin(27, Pin.OUT) # Relay 1 (controlled by potentiometer)
relay2 = Pin(14, Pin.OUT) # Relay 2 (controlled by start button)
relay_extra = Pin(12, Pin.OUT) # Additional relay
# Setup Buzzer with PWM
buzzer = PWM(Pin(13))
buzzer.duty(0) # Start with buzzer off
# Setup LED (light for night)
led_night = Pin(15, Pin.OUT)
# Setup additional LED indicator
led_status = Pin(2, Pin.OUT)
# Variables
program_running = False
system_started = False
relay1_state = False
relay2_state = False
relay_extra_state = False
print("Smart Garden Automation Started!")
print("=" * 40)
# Display function
def display_message(line1="", line2="", line3="", line4=""):
"""Display message on OLED (4 lines)"""
oled.fill(0)
oled.text(line1, 0, 0)
oled.text(line2, 0, 16)
oled.text(line3, 0, 32)
oled.text(line4, 0, 48)
oled.show()
def read_pot():
"""Read potentiometer value (0-4095)"""
return pot.read()
def read_ldr():
"""Read LDR value (0-4095), low value = dark"""
return ldr.read()
def activate_buzzer(duration=0.5, frequency=1000):
"""Sound the buzzer with specific frequency"""
buzzer.freq(frequency)
buzzer.duty(512)
time.sleep(duration)
buzzer.duty(0)
def check_buttons():
"""Check all button states"""
global relay1_state, relay2_state, relay_extra_state, program_running, system_started
# Start Button: Start system (only works when system is off)
if btn_start.value() == 0 and not system_started:
print("START button pressed! SYSTEM ON")
system_started = True
program_running = True
relay2.on() # Turn ON relay2 when system starts
relay2_state = True
display_message("SYSTEM ON", "", "Garden Auto", "Started!")
activate_buzzer(0.2, 1000)
time.sleep(1)
time.sleep(0.3) # Debounce
# Stop Button: Turn off relay1 + sound buzzer
if btn_stop.value() == 0:
print("STOP button pressed!")
relay1.off()
relay1_state = False
activate_buzzer(0.3, 2000)
display_message("WARNING!", "", "WATER LOW", "Pump OFF")
time.sleep(0.3) # Debounce
# Reset Button: Stop all programs
if btn_reset.value() == 0:
print("RESET button pressed! Program stopped!")
program_running = False
system_started = False
# Turn off all outputs
relay1.off()
relay2.off()
relay_extra.off()
led_night.off()
led_status.off()
activate_buzzer(0.1, 1500)
time.sleep(0.1)
activate_buzzer(0.1, 1500)
display_message("SYSTEM OFF", "", "Press START", "to restart")
time.sleep(0.3)
# Extra Button: Toggle additional relay
if btn_extra.value() == 0:
relay_extra_state = not relay_extra_state
if relay_extra_state:
relay_extra.on()
led_status.on()
print("EXTRA button pressed! Extra Relay ON")
else:
relay_extra.off()
led_status.off()
print("EXTRA button pressed! Extra Relay OFF")
time.sleep(0.3) # Debounce
def control_relay_by_pot():
"""Control relay1 using potentiometer"""
global relay1_state
pot_value = read_pot()
# Threshold: if potentiometer > 2000, turn on relay
if pot_value > 2000 and not relay1_state:
relay1.on()
relay1_state = True
activate_buzzer(0.2, 1000)
print(f"Potentiometer: {pot_value} - Relay 1 ON")
elif pot_value <= 2000 and relay1_state:
relay1.off()
relay1_state = False
print(f"Potentiometer: {pot_value} - Relay 1 OFF")
def control_light_by_ldr():
"""Control light using LDR (turns on at night)"""
ldr_value = read_ldr()
# Threshold: if LDR < 1500 (dark), turn on light
if ldr_value < 1500:
led_night.on()
else:
led_night.off()
def update_display():
"""Update display with sensor status"""
pot_value = read_pot()
ldr_value = read_ldr()
# Line 1: Soil status
if pot_value > 2000:
line1 = "SOIL DRY"
else:
line1 = "SOIL MOIST"
# Line 2: Pump status
if relay1_state:
line2 = "WATER PUMP ON"
else:
line2 = "Pump: OFF"
# Line 3: Light status
if ldr_value < 1500:
line3 = "LAMP ON"
else:
line3 = "Lamp: OFF"
# Line 4: Sensor values
line4 = f"P:{pot_value} L:{ldr_value}"
display_message(line1, line2, line3, line4)
# Initial display
display_message("SMART GARDEN", "AUTOMATION", "", "Press START")
# Main Loop
try:
while True:
if program_running and system_started:
# System is ON - run all functions
control_relay_by_pot()
control_light_by_ldr()
check_buttons()
update_display()
time.sleep(0.1)
else:
# System is OFF - only check for START button
check_buttons()
time.sleep(0.1)
except KeyboardInterrupt:
print("\nProgram stopped by user")
relay1.off()
relay2.off()
relay_extra.off()
led_night.off()
led_status.off()
buzzer.duty(0)
display_message("SYSTEM OFF", "", "Program", "Terminated")