Just thought I would post my code that I had AI write for my auto chicken door opener.
I have a light sensor, relay, Pi pico, an actuator that is signaled 0-12 volts. 12 volts is normally open.
#homestead #hack #program #python #pi #pico #chickens.
'''
import machine
from utime import sleep_ms, ticks_diff
# Define constants
DOOR_PIN = 8 # GP8 (PWM pin for relay/servo control)
LED_PIN = 25 # GP13 (On-board LED)
LIGHT_SENSOR_PIN = "GP26" # Analog input (photoresistor)
THRESHOLD_DARK = 2000 # Minimum sensor reading to trigger night mode
FAST_BLINK_DELAY = 6550 # Fast blink delay in ms
SLOW_BLINK_DELAY = 13000 # Slow blink delay in ms
# Define functions
def fast_blink():
led.value(1)
sleep_ms(FAST_BLINK_DELAY)
led.value(0)
sleep_ms(FAST_BLINK_DELAY)
def slow_blink():
led.value(1)
sleep_ms(SLOW_BLINK_DELAY)
led.value(0)
sleep_ms(SLOW_BLINK_DELAY)
# Initialize hardware
led = machine.Pin(LED_PIN, machine.Pin.OUT) # On-board LED
door_relay = machine.PWM(machine.Pin(DOOR_PIN)) # GP8 for relay/servo
adc = machine.ADC(machine.Pin(LIGHT_SENSOR_PIN))
# Main loop (continuous monitoring)
while True:
sensor_reading = adc.read_u16()
if sensor_reading < THRESHOLD_DARK: # Night mode detected (light levels low)
door_relay.duty_u16(32768) # Close door (relay/servo to open position is typically ~65K, close = ~0)
print("Door closed. Sleeping until light.")
slow_blink() # Indicate night mode with slow LED blink
else: # Daylight detected
door_relay.duty_u16(65535) # Open door (relay/servo to close position is typically ~0, open = ~65K)
print("Door open. Chickens free to roam.")
fast_blink() # Indicate day mode with fast LED blink
'''
Login to reply