Chicken Fever

Posted by:

Dave and Rich

Dave and Rich, The Dude Chicken.

My family caught chicken fever… so we started out on an adventure in raising chicks and building out a custom chicken coop in the Spring of 2012.

We wanted all different types of eggs, so we raised all kinds of chicks

The coop, “El Pollo Palace,” had to be awesome, made from recycled materials, efficient, green as possible and technologically advanced beyond anything reasonable. I think I’m on my way to having just that.

David Naves

About the Author:

Hi, I'm David Naves. I'm a daddy, hubby and currently live in Auburn California with (1) wife, (2) children, (1) dog, (1) cat, and (6) chickens. I play drums, am an avid fly fisherman a proud chicken nerd, as well as an Arduino enthusiast. I also enjoy using my general contractors license from time to time... even if just to over-build my Arduino Automated Chicken Coop As far as name-dropping and shameless self-promotion, I have over 20 years experience in the web industry and have helped build out,, and to name a few. I've co-founded an Emmy Award Winning Music Editing Firm in Los Angeles (Milky Music) and I hold a Bachelors of Science Degree in Business Administration. I'm passionate about getting Small Business America in to the "Big Game" of SEO and ecommerce, so I founded™ to do just that. My favorite web host is Acenet, love my Chromebook, and am passionate about learning via You can typically find me making noise out on Google+, Twitter and Facebook
  Related Posts


  1. Mike  April 11, 2014

    Hi Dave, Great job on your Coop Controller. I’m inspired! We are just getting into chickens here and I’ve been tinkering with Arduino for a year or so. Naturally I googled auto door and arduino and found you. I was looking at your code as I’m about to start my build and think maybe I’ve found why Chip’s motor is clicking…When you ‘doCoopDoor’ and call either ‘close’ or ‘openCoopDoorMotorB’, the first thing those processes do is start the motor then it checks to see if the switch is closed and calls stopCoopDoorMotorB. I think that the if statement that checks the reed switch pin value should be first so that if the door is already open/closed the motor is sent ‘stop’ else open/close. This is what I’m going to experiment with when i finally get around to building it. What do you guys think?

    • David Naves
      David Naves  April 19, 2014

      hi mike,

      really appreciate your nice words and comments about the code. so i think i hear what you’re saying, but maybe i’m misunderstanding… the way i wrote the function doCoopDoor in order was:

      read the photocel, read and debounce the switches, then operate the motor

      but you’re thinking that the actual open or close functions might help the clicking if:

      void openCoopDoorMotorB() {
      if (topSwitchPinVal == 0) { // if top reed switch circuit is closed
      Serial.print(” Coop Door open – danger!”);
      digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
      digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction
      analogWrite(enableCoopDoorMotorB, 255); // enable motor, full speed


      it’s making sense to me… i can’t really test since my motor isn’t clicking but maybe your idea can help chip. if so, he owes you!! =) chip, if you’re still checking the post, try’er out and keep us posted.

      thanks again,

  2. Chip  March 17, 2014

    Hey Dave – thanks for the code and schematic – I altered both to use an Arduino Motor Shield to drive a cordless screwdriver that opens and shuts a horizontal door on my coop. The code works great with one exception, I am hearing a clicking noise from the Arduino to the cordless screwdriver after the coop door has either opened or closed. I am ASSUMING that this is the reed switch debouncing, because it “clicks” at the same cadence set in the code, and I changed the delay in that section of the code and it clicks to whatever I set the delay rate to.

    How do I stop this? I think this constant debouncing of the switches will wear any battery/motor/arduino out over extended periods of time…maybe not?

    It does NOT happen if I get a ‘TWILIGHT’ reading, only after the coop door opens or closes.

    Any help greaty appreciated – I am an Arduino newbie. Below is my altered code. Thanks!

    // libraries

    #include // load the SimpleTimer library to make timers, instead of delays & too many millis statements

    * Copyright 2013, David Naves (,
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 3
    * of the License, or (at your option) any later version.
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * GNU General Public License for more details.
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, see


    * I’m hoping that if you use/modify this code, you will share your
    * coop project with me and the world (pictures, whatever)
    * I’m big on sharing.
    * Cheers,
    * //D

    const int photocellPin = A0; // photocell is connected to analog 0
    const int closeSwitchPin = 6; // close reed switch is connected to pin 6
    const int openSwitchPin = 7; // open reed switch is connected to pin 7

    // vars

    // photocell
    int photocellReading; // analog reading of the photocell
    int photocellReadingLevel; // photocell reading levels (dark, twilight, light)

    // reed switches open and close with coop door

    // open switch
    int openSwitchPinVal; // open switch var for reading the pin status
    int openSwitchPinVal2; // open switch var for reading the pin delay/debounce status
    int openSwitchState; // open switch var for to hold the switch state

    // close switch
    int closeSwitchPinVal; // close switch var for reading the pin status
    int closeSwitchPinVal2; // close switch var for reading the pin delay/debounce status
    int closeSwitchState; // close switch var for to hold the switch state

    // just need 1 SimpleTimer object
    SimpleTimer coopTimer;
    // ************************************** the setup **************************************

    void setup(void) {

    // welcome message
    Serial.println(” Checking doCoopDoor: every 1 minute for light levels to open or close door”);

    // coop door

    // coop door motor
    pinMode (12, OUTPUT); // Initiates motor pin for channel A
    pinMode (9, OUTPUT); // Initiates break pin for channel A

    // disengage the motor brakes
    digitalWrite(9, LOW); // Disengage the Brake for Channel A

    // coop door switches
    // close switch
    pinMode(closeSwitchPin, INPUT); // set close switch pin as input
    digitalWrite(closeSwitchPin, HIGH); // activate close switch resistor

    // open switch
    pinMode(openSwitchPin, INPUT); // set open switch pin as input
    digitalWrite(openSwitchPin, HIGH); // activate open switch resistor

    // timed actions setup
    coopTimer.setInterval(60000, readPhotoCell); // read the photocell every 1 minute


    // functions

    // operate the coop door

    // photocell to read levels of exterior light

    void readPhotoCell() { // function to be called repeatedly – per cooptimer set in setup

    photocellReading = analogRead(photocellPin);
    Serial.print(” Photocell Analog Reading = “);

    // set photocell threshholds
    if (photocellReading >= 0 && photocellReading = 15 && photocellReading = 25 ) {
    photocellReadingLevel = ‘3’;
    Serial.print(” Photocell Reading Level:”);
    Serial.println(” – Light”);

    //debounce close reed switch

    void debounceCloseReedSwitch() {

    //debounce close reed switch
    closeSwitchPinVal = digitalRead(closeSwitchPin); // read input value and store it in val
    closeSwitchPinVal2 = digitalRead(closeSwitchPin); // read input value again to check or bounce

    if (closeSwitchPinVal == closeSwitchPinVal2) { // make sure we got 2 consistant readings!
    if (closeSwitchPinVal != closeSwitchState) { // the switch state has changed!
    closeSwitchState = closeSwitchPinVal;
    Serial.print (” Close Switch Value: “); // display “Close Switch Value:”
    Serial.println(digitalRead(closeSwitchPin)); // display current value of close switch;

    // debounce open reed switch
    void debounceOpenReedSwitch() {

    openSwitchPinVal = digitalRead(openSwitchPin); // read input value and store it in val
    openSwitchPinVal2 = digitalRead(openSwitchPin); // read input value again to check or bounce

    if (openSwitchPinVal == openSwitchPinVal2) { // make sure we got 2 consistant readings!
    if (openSwitchPinVal != openSwitchState) { // the switch state has changed!
    openSwitchState = openSwitchPinVal;
    Serial.print (” Open Switch Value: “); // display “Open Switch Value:”
    Serial.println(digitalRead(openSwitchPin)); // display current value of close switch;

    // stop the coop door motor
    void stopCoopDoor(){
    digitalWrite (9, HIGH); // engages brake
    analogWrite (3,0); // motor is on zero speed

    // close the coop door motor (motor dir close = counter-clockwise)
    void closeCoopDoor() {
    digitalWrite (9, LOW); // disengages brake
    digitalWrite (12, HIGH); // turn on motor close direction
    analogWrite (3, 255); // enable motor, full speed
    if (closeSwitchPinVal == 1) { // if close reed switch circuit is closed
    Serial.print(” Coop Door CLOSED “);

    // open the coop door (motor dir open = clockwise)
    void openCoopDoor() {
    digitalWrite (9, LOW); // disengages brake
    digitalWrite(12, LOW); // turn on motor open direction
    analogWrite(3, 255); // enable motor, full speed
    if (openSwitchPinVal == 1) { // if open reed switch circuit is closed
    Serial.print(” Coop Door OPEN”);

    void doCoopDoor(){

    if (photocellReadingLevel == ‘1’) { // if it’s dark
    if (photocellReadingLevel != ‘2’) { // if it’s not twilight
    if (photocellReadingLevel != ‘3’) { // if it’s not light
    debounceOpenReedSwitch(); // read and debounce the switches
    closeCoopDoor(); // close the door
    if (photocellReadingLevel == ‘3’) { // if it’s light
    if (photocellReadingLevel != ‘2’) { // if it’s not twilight
    if (photocellReadingLevel != ‘1’) { // if it’s not dark
    debounceOpenReedSwitch(); // read and debounce the switches
    openCoopDoor(); // Open the door

    // ************************************** the loop **************************************
    void loop() {
    // polling occurs;

    • David Naves
      David Naves  March 21, 2014

      hi chip,

      thanks for writing in…

      hmmm, clicking… are you sure it’s the arduino making the noise? i would think maybe the motor? (or maybe that’s what you’re saying) but i’m not sure why it would click. is the motor actually moving? i kept that delay to 10ms to not bother the tons of other functions i have for the main coop, but to be honest, it has been bothering me. ( i was just getting tired of testing and left it) =) i’m wondering now if the debounce should be included within another function so that it only debounces during a very specific period of time.i probably should write another ref to simpleTimer for that debounce to get rid of that delay. (maybe a 1x only timer at dark or light)


      as far as wearing anything out, the only thing to worry about would be the battery. (which i never even considered, since i have a ac power down at my coop)

      also, i see you’re checking the photocell every minute… any problems with your motor misfiring (back and forth) because of inconsistent light readings? i check every 10 mins to avoid that. (just curious)

      i’ll post back when i come up with something… you can also check with the pros at the ardiuno forums if i’m too pokey with a solution.

      thanks again for writing in. post some pics of your project when you can. would love to see your coop.

      • Chip  March 31, 2014

        Hi Dave – thanks for the reply. I will eventually have my door mechanics mounted to a solar panel and deep cycle marine battery for power (I live in AZ so that’s a no brainer). I don’t know if the clicks would drain a deep cycle marine battery – but I would rather get rid of the arduino pinging the bounce/debounce.

        Hooking up the breadboard and arduino to the serial monitor, after I get a successful open or close door function, the serial monitor just continues to print:
        Close Switch Value: 0
        Coop Door OPEN Open Switch Value: 1
        and continues to print these two states on the switch values coinciding with every ‘click’ I hear on my DC motor.
        Just wondering if there is a way to stop this, if your serial monitor does this, and how I might be able to stop it…

        I’ll send pics once I get everything installed.

        Thanks for thinking!

        • Chip  March 31, 2014

          oh – and its the motor clicking, not the arduino

          • David Naves
            David Naves  April 3, 2014

            figured… not exactly sure why. i took a close listen to my motor after you mentioning that and mine is silent as it should be using my code:
            // stop the coop door motor
            void stopCoopDoorMotorB(){
            digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
            digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn on motor open direction
            analogWrite (enableCoopDoorMotorB, 0); // enable motor, 0 speed

        • David Naves
          David Naves  April 3, 2014


          and for me, i need the arduino to continue reading the switch values, b/c i have other functions triggering based on the value (the visual aid led light and a new electric fence, among other things) i keep going back to thinking that using simpleTimer would do the trick for you. (like create another timer to deal with shutting off debounce once the door has bully opened or closed. read this and look for “once” withing the content… you’ll see some examples of what i’m referring to.

          and yes, please do send some pics… looking forward to seeing what you’re doing.



Add a Comment