Quelud
Published © GPL3+

Automated door for chicken house

Automatic opening/closing of the front door of a chicken coop based on the day light measured by a LDR

BeginnerProtip7,995
Automated door for chicken house

Things used in this project

Hardware components

LDR, 1 Mohm
LDR, 1 Mohm
×1
Arduino UNO
Arduino UNO
×1
Development Board, Motor Control Shield
Development Board, Motor Control Shield
×1
Switch Actuator, Square D Type C Heavy Duty Limit Switches
Switch Actuator, Square D Type C Heavy Duty Limit Switches
×2
Rechargeable Battery, 12 V
Rechargeable Battery, 12 V
×1
Geared DC Motor, 12 V
Geared DC Motor, 12 V
×1
Limit switch
×2
Sodler board
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Schematics

Schematics_Fritzinig

https://fritzing.org/

Code

Code

Arduino
How to adapt:
- DC Motor rotational direction: lines 204 et 210 (to be updated depending on how it is mounted).
- hysteresis threshold duration: line 31 (time needed at a certain brightness threshold before the engine starts).
- Brightness threshold: lines 25 and 26 (normalized brightness in % from when it will open/close the door, to be adjusted for your case depending on your sensor voltage output as well as the position (more or less exposed to shadow)
// DC Motor shield uses chanels D8 - D11 - D13 - A1 => do not use for other I/O!

/////////////////////////////////////////////////////////
//----------------------VARIABLES----------------------//
/////////////////////////////////////////////////////////

// Pins
const int brightnessPin = A4; //BasDroit Rouge Noir LDR
const int limitSwitchDownPin = 2; // HautDroit Jaune Blanc
const int limitSwitchUpPin = 9;// HautDroit Rouge Noir
const int switchPin = 10; //BasDroit Jaune Blanc

// Intitialize switches
int switchState = 0;
int limitSwitchUpState = 0;
int limitSwitchDownState = 0;

//Calibrate brightness  - Max voltage corresponds to min brightness of LDR
float brightnessMinVoltage = 5;
float brightnessMaxVoltage = 0;
float brightnessMin = 0.0;
float brightnessMax = 100.0;

//Threshold brightness to open/close door
float brightnessLowThreshold = 1.5; //in %
float brightnessHighThreshold = 10; //in %

//Hysteresis variables to avoid abrut incessant opening/closing of the door
int hysteresis_opening; //Compteur pour éviter ouverture brusque 
int hysteresis_closing; //Compteur pour éviter fermeture brusque 
float hysteresisThreshold = 100; // *0,1 in s

int previousSwitchState;
int currentSwitchState = 0;


/////////////////////////////////////////////////////////
//------------------------SETUP------------------------//
/////////////////////////////////////////////////////////

void setup() {
  
  Serial.begin(9600); // open a serial connection to your computer

  //Switches 
  pinMode(switchPin, INPUT);
  pinMode(limitSwitchUpPin, INPUT);
  pinMode(limitSwitchDownPin, INPUT);

  //Motor (Setup on Channel B of the shield)
  pinMode(13, OUTPUT); //Initiates Motor Channel B pin
  pinMode(8, OUTPUT); //Initiates Brake Channel B pin

}


/////////////////////////////////////////////////////////
//-------------------------LOOP------------------------//
/////////////////////////////////////////////////////////

void loop() {

  //Read state of the switches
  switchState = digitalRead(switchPin);
  limitSwitchUpState = digitalRead(limitSwitchUpPin);
  limitSwitchDownState = digitalRead(limitSwitchDownPin);  

  //When switch is pressed, store the info into difference btw below variables
  if (switchState == HIGH) { 
    previousSwitchState=currentSwitchState;
    currentSwitchState++;  
  }


  ///////////////////////////
  //      INITIALIZE       //
  ///////////////////////////

  //When turning on the arduino, always open the door first
  if (millis() < 1000) { 
      while (limitSwitchUpState != HIGH) {
          // Open door
          openDoor();
          limitSwitchUpState = digitalRead(limitSwitchUpPin);
          limitSwitchDownState = digitalRead(limitSwitchDownPin);  
      }
  }

  ///////////////////////////
  //   MESURE BRIGHTNESS   //
  ///////////////////////////
  
  //Get voltage in analogic
  int brightnessAnalog = analogRead(brightnessPin);
  //Convert to V
  float brightnessVoltage = (brightnessAnalog / 1024.0) * 5.0;
  //Convert to %brighntess
  float brightness = (brightnessMax - brightnessMin) / (brightnessMinVoltage - brightnessMaxVoltage) * brightnessVoltage
               + (brightnessMax * brightnessMaxVoltage - brightnessMin * brightnessMinVoltage) / (brightnessMaxVoltage - brightnessMinVoltage);


  ///////////////////////////
  //         DEBUG         //
  ///////////////////////////

  Serial.print("BP bas: ");
  Serial.print(limitSwitchDownState);
  Serial.print("\t BP haut: ");
  Serial.print(limitSwitchUpState);
  Serial.print("\t brightness %: ");
  Serial.print(brightness);
//  Serial.print(" \t brightness analog: ");
//  Serial.print(brightnessAnalog);
//  Serial.print(" \t brightness voltage: ");
//  Serial.print(brightnessVoltage);
  Serial.print(" \t hyst_open: ");
  Serial.print(hysteresis_opening);
  Serial.print(" \t hyst_clos: ");
  Serial.print(hysteresis_closing);
//  Serial.print(" \t switch: ");
//  Serial.print(switchState);
  Serial.print(" \t curr_switch: ");
  Serial.print(currentSwitchState);
  Serial.print(" \t prev_switch: ");
  Serial.println(previousSwitchState);


  ///////////////////////////
  //          DOOR         //
  ///////////////////////////

  // CLOSING - if low brightness
  if (brightness < brightnessLowThreshold && limitSwitchDownState == LOW) {
      hysteresis_closing++;

      delay(100);

      // Do only if brighntess remains below the threshold for a certain time
      if(hysteresis_closing > hysteresisThreshold){ 
          // Close door
          closeDoor();
      }
  }

  // OPENING - if high brightness
  else if (brightness > brightnessHighThreshold && limitSwitchUpState == LOW) {
      hysteresis_opening++;
      delay(100);
    
      // Do only if brighntess remains below the threshold for a certain time
      if(hysteresis_opening > hysteresisThreshold){ 
          // Open door
          openDoor();
      }
  }
 
  else {
      analogWrite(11, 0);   //Stop motor
  
      //initialize hysteresis for LDR activation
      hysteresis_opening=0;
      hysteresis_closing=0;
  }

  // CLOSING - if switch button pressed
  if (currentSwitchState != previousSwitchState && limitSwitchUpState == HIGH) {
      while (limitSwitchDownState != HIGH) {
        
          // Close door
          closeDoor();
          limitSwitchUpState = digitalRead(limitSwitchUpPin);
          limitSwitchDownState = digitalRead(limitSwitchDownPin);  

      }
      previousSwitchState = currentSwitchState;
      analogWrite(11, 0);   //Stop motor
   }

  // OPENING - if switch button pressed
  if (currentSwitchState != previousSwitchState && limitSwitchDownState == HIGH) {
      while (limitSwitchUpState != HIGH) {
        
          // Open door
          openDoor();
          limitSwitchUpState = digitalRead(limitSwitchUpPin);
          limitSwitchDownState = digitalRead(limitSwitchDownPin);  
          
      }
      previousSwitchState = currentSwitchState;
      analogWrite(11, 0);   //Stop motor
   }
}


/////////////////////////////////////////////////////////
//----------------------FUNCTIONS----------------------//
/////////////////////////////////////////////////////////

// Function to open or close the door
// To reverse engine rotation, juste change the line in the functions:
// digitalWrite(13, HIGH); // to digitalWrite(13, LOW); or reverse

void closeDoor () {
        digitalWrite(13, HIGH); //Establishes backward direction of Channel B
        digitalWrite(8, LOW);   //Disengage the Brake for Channel B
        analogWrite(11, 255);   //Spins the motor on Channel B at full speed
}

void openDoor () {
        digitalWrite(13, LOW); //Establishes forward direction of Channel B
        digitalWrite(8, LOW);   //Disengage the Brake for Channel B
        analogWrite(11, 255);   //Spins the motor on Channel B at full speed
}

Credits

Quelud
1 project • 1 follower
Contact

Comments

Please log in or sign up to comment.