A Raspberry PI controlled DIY Coop Door (with Python code!)

This article describes how I built an automatic chicken coop door using a DC motor and a raspberry PI control it.
By oguzakyuz · Jul 9, 2018 · Updated Jul 12, 2018 · ·
Rating:
5/5,
  1. oguzakyuz
    Automatic coop doors are lifesavers. Unless you relish waking up like a chicken every day to let your birds out, you will want an automatic coop door. For some of us getting an automatic door is as easy as ordering it online. But for others like me no such option exists (at least without an humongous international shipping fee). Or you may like to DIY just for fun and for the experience.

    In this article, I would like to share how I built an automatic door for my coop. The construction involves three types of components, one involving the mechanics, the other electronics, and the other software. I will explain each component in turn but first let me show a picture showing the finished project.

    IMG_1601_small.jpg

    Mechanical Components

    As you can see in the picture the primary mechanical components are:
    • A simple plank for the door
    • Drawer rails
    • A frame on which the door will slide
    • A small piece of wood attached to the top of the plank
    • A small L-shaped wood knob to activate the limit switches
    • A thin but sturdy rope
    I've learned by experience that you will want to provide minimal freedom of movement for your coop door other than its intended direction of motion. For instance, if you want your door to go up and down, you just want that and avoid any other type of motion. For this purpose using drawer rails works perfectly. Simply install one part of the rails to the frame attached to the coop and the other on the door plank. Once you make this, you should be able to slide the door up and down manually with your hands. Make sure that the motion is smooth and has no resistance. Also make sure that the door can go all the way down and can go up enough to allow a chicken to enter. A gap of 30 cm (or 12 inches) should be sufficient for most chickens to go in. If the motion of the rails is hard, you may want to use a bit of WD-40 for reduced resistance.

    Electrical Components

    The electrical (and electro-mechanical) components include the following:
    For the adapter, cables, and the breadboard something like this can be used: https://www.amazon.com/HJ-Garden-El...F8&qid=1531073989&sr=1-13&keywords=breadboard

    As for the DC motor any simple motor (e.g. 12 V or a 24 V should work) would suffice as long as it can generate sufficient torque to lift the door. Typically motors come with a D-shaft on which you are expected to install a reel. A thin but sturdy rope with one end connected to the door will be wound around this reel. Torque is defined as the vertical force multiplied by the distance from a pivot point around which rotation occurs. If you use a large reel, this would increase the torque requirements (the weight of the door will remain the same but the distance will have increased). So try to use the smallest reel around which the rope can be comfortably wound. The voltage output of the adapter should ideally match the voltage requirement of your motor.

    I use a raspberry pi computer to control opening and closing of the door. In practice a simpler and cheaper device such as an ardunio microcontroller would work. However, I had a raspberry pi lying around with no use so I wanted to put it to work.

    The operation of the motor works as follows:
    1. A background process running on raspberry pi decides when to start a Python (a popular programming language) program which is responsible for managing the motor.
    2. This python program sends voltage through one of the GPIO (general purpose input output) ports of the raspberry pi to the motor controller unit.
    3. The motor controller unit, upon receiving this signal, forwards the DC voltage coming from the adapter to the motor. This triggers the motor to turn, winding the rope around the reel. When the door is lifted high enough a small wooden knob at the top of the door touches the limit switch.
    4. The Python program (see below) is continuously checking the signal from the limit switch. When the limit switch is closed by the knob, the Python program detects this and tells the motor controller unit to stop forwarding voltage to the motor.
    5. The closing of the door is pretty much the same, except that the Python program requests the motor controller to send voltage with opposite polarity to the motor, causing it to turn in the reverse direction. When the bottom switch is closed, the Python program again tells the motor controller unit to stop forwarding the voltage to the motor.
    The important point to make sure is that your adapter should not provide more current than the motor controller unit (L293D) can support. A current of 1 A should be fine. Otherwise, if the motor is stalled for some reason (by stalled I mean if the motor is trying to turn but it cannot due to being stuck), it can draw too much current over the motor controller unit, causing it to overheat and even burn. In extreme cases, this may melt your breadboard, raspberry pi, and even cause a fire hazard. So if you are using L293D I would make sure that your adapter cannot provide more than 1 A.

    How you are going to assemble the raspberry PI, motor controller unit, and the motor itself on the breadboard may depend on the specific models of each of these devices. There are many tutorials online, so I do not want to duplicate them here. The one that I followed can be found here: https://business.tutsplus.com/tutor...s-using-python-with-a-raspberry-pi--cms-20051.

    At this point I would like to show another picture on which the main components described above are marked:

    IMG_1601_small_text.jpg

    Software Components:

    As mentioned above, the door will be controlled by a small Python program. This program will be executed by a daemon program, that continuously runs in the background, at specific times of the day. In Raspbian (which is the operating system running on raspberry pi), this daemon program is known as cron. We can request any task to be executed by installing a crontab entry. I have the following entries to control opening and closing of the door:

    PHP:
    # Open the coop door at 6:30 every morning
    30 6 * * * python /home/pi/workspace/mcontrol/openLimit.py

    # Close the coop door at 20:00 every night
    0 20 * * * python /home/pi/workspace/mcontrol/closeLimit.py
    As you can see above, everyday at 6:30 in the morning "openLimit.py" is executed, which will open the door. Similarly at 20:00 each night, "closeLimit.py" is executed closing the door. As the daylight hours depend on the season, you may want to add different entries for different months by adjusting the time according to daylight hours (making this work with a light sensor is left as an exercise to the reader).

    Let us now turn our attention to the Python scripts starting with "openLimit.py". Below is the full source code, heavily commented for clarity:

    PHP:
    import RPi.GPIO as GPIO   # We need this library to access GPIO pins
    from time import sleep

    GPIO
    .setmode(GPIO.BOARD)

    Motor1A 16  # These are the GPIO pin numbers that we will use
    Motor1B 18
    Motor1E 
    22

    LimitSwitchUp 
    12 # The limit switch is connected to this pin

    GPIO.setup(Motor1AGPIO.OUT)       # OUT means we will output to this pin
    GPIO.setup(Motor1BGPIO.OUT)
    GPIO.setup(Motor1EGPIO.OUT)
    GPIO.setup(LimitSwitchUpGPIO.IN)  # IN means we will listen to this pin

    print "Turning motor on"

    GPIO.output(Motor1AGPIO.LOW)      # Send low voltage to 1A
    GPIO.output(Motor1BGPIO.HIGH)     # Send high voltage to 1B
    GPIO.output(Motor1EGPIO.HIGH)     # Activate the motor

    maxDur 150
    curDur 
    0
    pollInterval 
    0.001
    roomToGo 
    GPIO.input(LimitSwitchUp)

    # We are now listening the status of the limit switch for
    # 150 seconds, checking it every 0.001 seconds.
    while roomToGo == and curDur <= maxDur:
        
    sleep(pollInterval)
        
    curDur curDur pollInterval
        roomToGo 
    GPIO.input(LimitSwitchUp)

    # Roll back down a little to release the switch
    while GPIO.input(LimitSwitchUp) == 0:
        
    GPIO.output(Motor1AGPIO.HIGH)
        
    GPIO.output(Motor1BGPIO.LOW)
        
    sleep(pollInterval)

    print 
    "Stopping motor"

    GPIO.output(Motor1EGPIO.LOW# Deactivate the motor
    GPIO.cleanup()
    The "closeLimit.py" script is similar in spirit with a minor but important difference. I've found by experience that as the door slides down the rails occasionally it may get stuck due to friction. When this happens the motor keeps rotating and the rope becomes loose starting to be wound back on the reel from the opposite direction. In other words, the door starts to open up again. To avoid this problem I decided to check both the top and bottom limit switches. Normally, the motor is stopped when the bottom switch is closed. But if the top switch is triggered due to the reason explained above, I reverse the direction of the motor giving it one more chance to close. You may avoid running into this problem by using a heavier door (which must be matched with a stronger motor) and/or using high quality drawer rails that have a very smooth motion. The full code is below:

    PHP:
    import RPi.GPIO as GPIO
    from time import sleep

    GPIO
    .setmode(GPIO.BOARD)

    Motor1A 16
    Motor1B 
    18
    Motor1E 
    22

    LimitSwitchDown 
    11
    LimitSwitchUp 
    12

    GPIO
    .setup(Motor1AGPIO.OUT)
    GPIO.setup(Motor1BGPIO.OUT)
    GPIO.setup(Motor1EGPIO.OUT)
    GPIO.setup(LimitSwitchDownGPIO.IN# Note that we are listening to both limit switches
    GPIO.setup(LimitSwitchUpGPIO.IN)

    print 
    "Turning motor on"

    GPIO.output(Motor1AGPIO.HIGH# Note that the polarity is reversed with respect to "openLimit.py"
    GPIO.output(Motor1BGPIO.LOW)
    GPIO.output(Motor1EGPIO.HIGH# Activate the motor

    maxDur 150
    curDur 
    0
    pollInterval 
    0.001

    # Again loop for a maximum of 150 seconds by checking the status the switches
    while curDur <= maxDur:
        
    roomToGoDown GPIO.input(LimitSwitchDown)
        
    roomToGoUp GPIO.input(LimitSwitchUp)

        if 
    roomToGoUp == 0# The top is wound back on the reel, so we must reverse
            # Reverse direction
            
    GPIO.output(Motor1AGPIO.LOW)
            
    GPIO.output(Motor1BGPIO.HIGH)
            
    sleep(1# allow time for the switch to be released
            
    curDur 0# reset timer to give another 150 seconds

        
    if roomToGoDown == 0# The door is closed
            
    break

        
    sleep(pollInterval)
        
    curDur curDur pollInterval

    print "Stopping motor"

    GPIO.output(Motor1EGPIO.LOW# Deactivate the motor
    GPIO.cleanup()
    Note that the door is opened and closed slowly. I give a maximum duration of 150 seconds for both operations but it takes around a minute in my case. This way you won't risk guillotining your chickens. The speed of the opening and closing is determined by the RPM of the motor, the diameter of the reel, and the voltage you supply to it. In my case I use a 40 rpm motor with a reel of about an inch in diameter.

    EDIT: Here is a wiring diagram for the circuit. Together with the codes above, I sincerely hope that this turns out be useful for a DIYer out there.

    coop_wiring.png

    Caveats:

    Making a DIY coop door is a lot of fun but requires some engineering skills. You don't have to be an engineer but must be an experienced DIYer. Knowing about programming and the basics of electricity and circuits is a big plus. You must make sure that you understand the potential risks of this process. The biggest risk is using an adapter with a too strong current output matched with a motor with a high stall current, which may result in overheating your circuit elements. I've melted my breadboard once almost destroying my raspberry pi. In the extreme case, this may cause a fire hazard. Another risk is something going wrong to prevent your coop door from opening/closing. Both happened to me several times. In this case, you must understand what caused the malfunction, fix it, and hope that it won't happen again. Installing a USB WIFI adapter (and connecting it to your home network) to raspberry pi is very important. This way you can connect to it remotely and modify the scripts with ease. It would also ensure that the date/time of the device is always correct even after power outages.

    Share This Article

Recent User Reviews

  1. ronott1
    "Great Article!"
    5/5, 5 out of 5, reviewed Jul 25, 2018
    I read about the little computer on a board like this one uses several years ago. It is great to see one being use for raising chickens!

    Can you add a video of the door in action?
    casportpony likes this.
  2. Chickencountryuk
    "Very good"
    5/5, 5 out of 5, reviewed Jul 25, 2018
    Great automatic door opener. Its abit past my skills to programme but it works well and all the info you need to build is included in the article.
    casportpony likes this.
  3. N F C
    "Well Detailed Info"
    5/5, 5 out of 5, reviewed Jul 22, 2018
    casportpony and oguzakyuz like this.

Comments

To make a comment simply sign up and become a member!
  1. casportpony
    Any chance you could include the schematics and a wiring diagram for this?
      KikisGirls and Pater est Pullis like this.
    1. oguzakyuz
      I just did. Thanks for the feedback.

BackYard Chickens is proudly sponsored by: