Chickens roam around the yard during the day, they instinctively go to bed in the chicken coop at night. After sunrise they come back out again. Rinse & repeat. The coop door needs to be closed every night to avoid possums from eating the chickens’ food and the foxes from eating the chickens. Some nights I forget to close the door, this is really bad. Automating the opening/closing of the chicken coop door will solve the problem.


  1. Device mounted inside chicken coop (Arduino and RasPi) will connect to the internet and know what time to open and close the door.
  2. Mechanical arm (servo) on the device (Arduino + RasPi) opens and closes the door at the right times.
  3. This device (RasPi) talks to a web server (CentOS Linux on local network), giving it status information and waiting for commands.
  4. Home owner connects to the web server to monitor status of the door (and when it is going to open/close), and can also send a command to control the door.

This is the second part of the 3 part Coopener (Chicken Coop Door Opener) series.
Click here for part 1
Click here for part 3

The purpose of the Automated Chicken Enclosure isn’t only to open and close a door. It’s equally important second purpose is to build a reliable framework for all automation tasks around the house. To this end, you will find this project is decidedly complicated for something as simple as controlling a door. Now you know the reason why.

A project of this magnitude is best handled by breaking down the tasks, and the best way to do this is to follow the data flow and make logical separation. The data flow currently looks like this.

parts of project

Anticipated Data Flow (UPDATE: 31/10/16 – The above flow has been modified and no longer represents what the code does, see part 3 for updated flow diagram.

The project can then be broken down in to the following stages, in order of completion.

  1. Build servo mechanism to open/close the door and retrofit the battery-powered hardware for initial testing.
  2. Install Raspbian on Raspberry Pi and set up NTP, WiFi, and connect to existing network.
  3. Build Python code to control door through serial interface with Arduino.
  4. Provide mains power to the coop
  5. Install hardware and test (at this point the machine should be fully automated and functioning).
  6. Build Python network socket listener on server and configure for communication with RasPi.
  7. Build CGI scripts and HTML web front end (I am not much of a front-end developer, so I am on the look out for an existing package which I can use for this. Worst case I will need to build it myself).

Step 1. Build hardware and retrofit

Before we commit to a specific design for the door mechanism we need to test it. This involves building some basic Arduino code which cycles through opening and closing the door, installing the hardware, and powering it by battery.

This was installed and run until the battery died (~4 days). Once I was convinced that the mechanism will continue to work I then went on to step 2.

Further information can be found here

The wiring of the Arduino and Raspberry Pi is below.

When powering the RasPi you would be better off using a barrel connector (or similar) wired to the header pins for this. This is how I have done the wiring for the power as seen in below diagrams and image further down this post.

The 5V regulator in the RasPi is bypassed when powering the device this way.

Be sure you are giving it a clean 5V signal.

Use the complete Arduino code and enable the TESTING mode variable for this step.

Step 2. Initial configuration of Raspberry Pi

This won’t be covered in my blog. There are plenty of instructions on other websites for this. By default NTP should be enabled, be sure that it is successfully picking up the correct time/date. Also, change the timezone options in the raspi-config to match your local one.

Step 3. Python code to control Arduino

This is the bulk of this post. The Python code is run daily (before sunrise) and ends execution after sunset.
At sunset chickens naturally put themselves to bed, so closing the door a little after sunset (civil twilight) ensures they stay inside overnight and no other animals can get in. At sunrise, chickens are up and want to get back out again. A little before sunrise (civil twilight) the door will open.
This script will grab the civil twilight hours from the web, parse this data and use it to open and close the door. Because we can’t be certain that internet will always be available, it also saves these times in a file and they are used again the next day in case there is no web access.

The flow diagram below illustrates the function.


UPDATE (31/10/16): The above image no longer represents how the code is running on the RasPi. I have left the image here for posterity as it illustrates the basic logic, for the new and updated flow diagram see Part 3 of this project.

See Part 3 of this project for links to the testing code.

Step 4. Provide mains power

Providing power to the coop was a little trickier than I thought. Running the cable was the easy part. Luckily, I had run 240V (AC) mains along with two low-power (DC) cables under the lawn many years ago. I will be using one of the low-power DC wires to power the Coopener. The other one will be used for fairy lights when I get a chance to install them…. one day.

However, because the cable is so long (about 20m in one direction) the voltage was dropping just enough not to show up on my multimeter and was able to run the RasPi, but under a little bit more load it would reboot itself. Connectivity by WiFi was also sketchy.

I was seeing many of these lines in /var/log/messages

Indeed it is in host mode hprt0 = 00021501


The coopener.log file was also showing the script was restarting. Time stamp matches to /var/log/messages was showing reboots.


Coopener.log: Reboots were infrequent, but problematic

The solution at the end was to use a 12V/5A power adapter along 20m length of cable, and then in the chicken coop use a regulator to drop the 12V to 5V/3A. This worked perfectly.

The RasPi was being powered via the USB plug. But to make things neater (and less finicky) I decided to use the 5V & GND pins (GPIO header pins 4 and 6) to connect to a standard barrel connector for power.


Pins 4 and 6 used for powering the RasPi

This project is continued in Coopener – Part 3.