Build an ESP32 Automatic Chicken Coop Door: Light Sensing, Motor Control, and Predator-Safe Timing
- ESP32 Dev Board (ESP32-WROOM-32)
- 12V Linear Actuator with built-in limit switches, 100-150mm stroke
- BTS7960 43A Motor Driver Module
- Photoresistor (LDR) Light Sensor Module
- BH1750 Digital Light Sensor Module (I2C)
- 12V 5A Weatherproof Power Supply
- IP65 Project Enclosure Box
- Magnetic Door Sensor / Reed Switch (for open/closed confirmation)
- Outdoor-Rated Wire, 18 AWG, Multi-Strand
- Cable Glands, Assorted Sizes
- 12V Battery (SLA) + Solar Charge Controller (for off-grid coops)
- 3D-Printed or Plywood Door Track and Slide Hardware
A chicken coop door that opens and closes on a timer or by ambient light is one of the most practical automation projects a backyard homesteader can build, and it's a surprisingly good fit for an ESP32: the logic is simple, the duty cycle is low, and the payoff (not getting up at 5:30am to let the flock out, and not losing birds to a predator because the door was left open after dusk) is immediate. This guide walks through a complete build: a linear-actuator-driven vertical door, dual light sensing for reliability, limit-switch feedback so the controller always knows the door's real position, and a predator-safe timing model that treats "it's getting dark" as more urgent than "it's time to open."
The design deliberately avoids two common failure modes seen in off-the-shelf automatic coop doors: relying on a single light sensor that can be fooled by shade, storm clouds, or a bird sitting on it, and relying purely on a clock, which gets the birds shut out or exposed as the seasons shift. This build cross-checks a photoresistor against a digital lux sensor and falls back to astronomical sunrise/sunset times (fetched over WiFi, or dead-reckoned locally if the network is down) so the door never does something a chicken keeper wouldn't want.
How the control logic works
The ESP32 polls two light sources every 30 seconds: a cheap LDR voltage divider for fast response, and a BH1750 digital lux sensor over I2C for an actual calibrated reading. The two are compared, not averaged — if either sensor alone reports "dark enough to close" for three consecutive readings, the door closes. If both disagree for an extended period, the controller falls back to the sunrise/sunset table and logs a sensor-disagreement warning (visible in Home Assistant if you're integrating it, or via the onboard status LED if running standalone).
Closing is treated as the time-critical operation. A chicken coop door that fails to close at dusk is a predator problem; one that fails to open a bit late is an inconvenience. The firmware reflects that asymmetry: it attempts a close retry every 2 minutes if the limit switch doesn't confirm a closed state, and it will not re-open an already-closed door until a configurable "minimum closed duration" has elapsed, even if the light sensors glitch bright (e.g., someone shines a flashlight on the coop at 2am).
StateTrigger conditionActionSafety behavior OpenLux > threshold for 3 consecutive reads AND past minimum-closed timer AND not manually overriddenActuator extends until top limit switch closesRetries every 60s if limit switch doesn't confirm within 20s of travel ClosedLux < threshold for 3 consecutive reads OR manual close command OR watchdog timeout with no sensor dataActuator retracts until bottom limit switch closesRetries every 120s; sends alert after 3 failed attempts FaultNeither limit switch reached within 45s of actuator travelActuator stops, alarm LED/notification firesNo further automatic movement until acknowledged Manual overridePhysical button or app commandExecutes requested motion, bypasses light logic for one cycleAuto-resumes light-based logic after 24 hoursWiring the actuator and sensors
The BTS7960 driver handles the actuator's current draw (most 12V linear actuators in this stroke range pull 2-5A under load, well within the module's 43A rating with huge headroom for inrush) and gives you PWM speed control if you want the door to ease into its end stops rather than slam. Wire the actuator's two leads to the OUT1/OUT2 terminals, and tie the driver's RPWM/LPWM and R_EN/L_EN pins to four ESP32 GPIOs. The limit switches built into most actuator-and-bracket kits wire as simple normally-open contacts to ground, read on two more GPIOs with internal pull-ups enabled in firmware — no external resistors needed.
Mount the BH1750 where it can see ambient sky light but is sheltered from direct rain and from the coop's own interior light spilling onto it; a short length of PVC pipe angled downward like a tiny awning works well and keeps birds from perching directly on the sensor. Run all sensor and actuator wiring through cable glands into the IP65 enclosure housing the ESP32 and driver board — moisture ingress into the driver board is the single most common field failure in outdoor coop controllers, so don't skip the glands even for a "just in the barn" installation.
Power and off-grid operation
For coops with mains power nearby, a single 12V 5A supply feeding both the ESP32 (through a buck converter to 5V/3.3V) and the actuator driver is the simplest path. For remote coops, pair a 12V sealed lead-acid battery with a small solar panel and PWM or MPPT charge controller; the ESP32's own draw is negligible (under 100mA average with WiFi sleep enabled between polls), and the actuator only draws real current for the few seconds of travel twice a day, so a 20-30W panel and a modest battery will run the system through weeks of poor sun.
If you're running off-grid, add a low-voltage cutoff in firmware: below a configurable battery voltage (read through a simple voltage divider into an ADC pin), suppress non-essential WiFi reporting and widen the sensor polling interval to conserve power, while still guaranteeing the closing behavior — a dead controller that fails to close the door is the exact failure this project exists to prevent.
Integrating with Home Assistant
Expose door state, battery voltage, and manual open/close controls over MQTT so the coop door shows up as a normal entity in Home Assistant alongside your other automation. This also gives you a free audit trail: graphing "door closed" timestamps against sunset times over a season is a good way to catch a drifting light sensor or a shading tree branch before it becomes a problem. A simple automation that sends a push notification if the door hasn't closed within 30 minutes of local sunset is worth setting up on day one.
Safety and animal welfare considerations
Linear actuators have real pinch and crush force — enough to injure or kill a bird caught in the door's path. Use a vertical sliding door design rather than a swinging door where possible, fit a mesh or rail guide so the door can't bind sideways, and consider an infrared break-beam or simple current-sense cutoff (most cheap BTS7960 setups can detect a stalled motor by current spike) that stops and reverses the actuator if it encounters unexpected resistance. Test this obstruction-detection behavior deliberately, with a broom handle, before the first bird ever uses the coop. Keep all mains and 12V wiring fully enclosed and away from nesting material, and ground any metal enclosure if it's exposed to the elements.
Once it's running, this build tends to disappear into the background in the best possible way: the flock gets a consistent schedule, you stop losing sleep to dawn feedings, and the Home Assistant log quietly confirms every evening that the door did its job. It's also a good gateway project into broader farm and garden automation — the same light-sensing and actuator-driving pattern shown here extends directly to greenhouse vents, rain barrel valves, or a solar-tracking panel mount.