The Blackdrop Diaries

Chicken Door

An automatic chicken door based on the luminosity...

Categories: [Electronic]
Tags: [Arduino], [DIY]

Description

Automatic chicken door with opened and closed switch and a continous servomotor.

The luminosity can be adjusted via small potentiometer on the side of the box.

There is a security delay to close or open the door to avoid damaging the motor.

Schematic

schemapoule

Arduino Pro Mini 5V 16Mhz Code

/**
 * @ Name : Chicken Door
 * @Platform : Pro Mini 328P 5V
 * @Author : Sam
 * @Date : 18/03/2022
 * @description : Automatic chicken door opener with a servomotor and a photodiode
 */

#include <Servo.h>

Servo servo;

#define OPENED 1
#define CLOSED 0

#define PIN_PHOTO A0
#define PIN_SENSOR_OPENED 5 // YELLOW WIRE
#define PIN_SENSOR_CLOSED 4 // ORANGE WIRE
#define PIN_SERVO 9

/* --- LIMITS ---*/
/*
 * The analog value is between 0 (night) and 1024 (day)
 * The potentiometer can modify the analog value CW (value goes high) and  CCW (value go down)
 * Works with hysteresis :
 *  door opens if closed at LIMIT_DAY
 *  door close if opened at LIMIT_NIGHT
*/
#define LIMIT_DAY 300 // Minimum limit of lux to consider that the sun is up (what is up !!)
#define LIMIT_NIGHT 150 // Maximum limit to consider that the night is on ! 
#define SLEEP_TIME 1000 // sleep time in ms

/* --- SERVO ---*/
#define STOP 90
#define UP 85
#define DOWN 95

#define DELAY_MOVE 5000

boolean door;

void setup() {
  pinMode(PIN_SENSOR_CLOSED, INPUT_PULLUP);
  pinMode(PIN_SENSOR_OPENED, INPUT_PULLUP);
  servo.attach(PIN_SERVO);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  servo.write(STOP);
  Serial.begin(9600);
  Serial.println("starting Chickendoor");

  // at startup, by default, we open the door
  openDoor();

}

int pos = 0;

int lux = 0;

void loop() {
  lux = getLux();
  Serial.print("door : ");
  if(door) Serial.print("OPENED");
  if(!door) Serial.print("CLOSED");
  Serial.print(" lux : ");
  Serial.print(lux);
  Serial.print(" limit day : ");
  Serial.print(LIMIT_DAY);
  Serial.print(" limit night : ");
  Serial.print(LIMIT_NIGHT);
  Serial.print(" sensor opened : ");
  Serial.print(digitalRead(PIN_SENSOR_OPENED));
  Serial.print(" sensor closed : ");
  Serial.println(digitalRead(PIN_SENSOR_CLOSED));

  if(door == CLOSED){
    // check if we open the door to let the chickens out !! out !! out !!  
    if(lux > LIMIT_DAY) openDoor();
  }else{
    // the door is opened
    // Lets check if we should close it
    if(lux < LIMIT_NIGHT) closeDoor();
  }

  // go to sleep for 10 minutes
  delay(SLEEP_TIME);
}

int getLux(){
  return analogRead(PIN_PHOTO);
}

void openDoor(){
  Serial.println("opening door");
  servo.write(UP);
  // wait for the sensor high to be grounded
  waitForSwitch(PIN_SENSOR_OPENED);
  servo.write(STOP);
  door = OPENED; 
  digitalWrite(LED_BUILTIN, LOW);
}

void closeDoor(){
  Serial.println("closing door");
  servo.write(DOWN);
  waitForSwitch(PIN_SENSOR_CLOSED);
  servo.write(STOP);
  door = CLOSED;
  digitalWrite(LED_BUILTIN, HIGH);
}

void waitForSwitch(int pin){
  unsigned int s = millis();
  while(digitalRead(pin)){
    // if after a delay nothing happens we shut the motor anyway as security
    if(millis() - s > DELAY_MOVE) break;
  }
}

IMG_20220428_092635

Next »