Automatic Chicken Coop Door at Sunrise and Sunset
A 12 V linear actuator opens the coop at sunrise and closes it after sunset, calculated for your exact location.
Predators strike at dusk. This build calculates sunrise and sunset every day with the astral library and drives a linear actuator through a two-relay H-bridge, with a reed switch to confirm the door really closed.
Make sure the door cannot trap an animal: use an actuator with modest force, a light door and a clear path. Check the coop manually until you trust it.
Video tutorial

Video by FloweringElbow, embedded from YouTube. The written steps below are our own and match the parts list.
Mission steps
Build the door
A sliding vertical door in a guide rail, connected to a 12 V linear actuator with built-in limit switches.
Wire the H-bridge
Two relays reverse the actuator's polarity: one for open, one for close. Never energize both — the code enforces it.
Add the reed switch
Mount it so the magnet meets it only when the door is fully closed.
Install astral
pip install astral in a venv and set your latitude, longitude and time zone.
Alert on failure
If the reed switch does not confirm closed one minute after the close command, send a phone notification.
Code
import datetime, time
from zoneinfo import ZoneInfo
from astral import LocationInfo
from astral.sun import sun
from gpiozero import OutputDevice, Button
TZ = ZoneInfo("America/New_York")
home = LocationInfo("Coop", "US", "America/New_York", 27.27, -80.35) # set your own lat/lon
up = OutputDevice(17, active_high=False); down = OutputDevice(27, active_high=False)
closed = Button(22)
def move(relay, other, secs=25):
other.off(); time.sleep(0.2); relay.on(); time.sleep(secs); relay.off()
while True:
now = datetime.datetime.now(TZ)
s = sun(home.observer, date=now.date(), tzinfo=TZ)
should_open = s["sunrise"] <= now < s["sunset"] + datetime.timedelta(minutes=30)
if should_open and closed.is_pressed:
move(up, down)
elif not should_open and not closed.is_pressed:
move(down, up)
time.sleep(60)Tip: test every script on the bench before installing it in a moving machine.
Questions
Can I use a microcontroller instead?
Yes — an ESP32 with a real-time clock and a sunrise table works great and uses far less power, which matters on solar.