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
Components and supplies
Switch Actuator, Square D Type C Heavy Duty Limit Switches
Arduino UNO
Development Board, Motor Control Shield
Rechargeable Battery, 12 V
Sodler board
LDR, 1 Mohm
Limit switch
Geared DC Motor, 12 V
Tools and machines
Soldering iron (generic)
Solder Wire, Lead Free
Apps and platforms
Arduino IDE
Project description
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)
1// DC Motor shield uses chanels D8 - D11 - D13 - A1 => do not use for other I/O! 2 3///////////////////////////////////////////////////////// 4//----------------------VARIABLES----------------------// 5///////////////////////////////////////////////////////// 6 7// Pins 8const int brightnessPin = A4; //BasDroit Rouge Noir LDR 9const int limitSwitchDownPin = 2; // HautDroit Jaune Blanc 10const int limitSwitchUpPin = 9;// HautDroit Rouge Noir 11const int switchPin = 10; //BasDroit Jaune Blanc 12 13// Intitialize switches 14int switchState = 0; 15int limitSwitchUpState = 0; 16int limitSwitchDownState = 0; 17 18//Calibrate brightness - Max voltage corresponds to min brightness of LDR 19float brightnessMinVoltage = 5; 20float brightnessMaxVoltage = 0; 21float brightnessMin = 0.0; 22float brightnessMax = 100.0; 23 24//Threshold brightness to open/close door 25float brightnessLowThreshold = 1.5; //in % 26float brightnessHighThreshold = 10; //in % 27 28//Hysteresis variables to avoid abrut incessant opening/closing of the door 29int hysteresis_opening; //Compteur pour éviter ouverture brusque 30int hysteresis_closing; //Compteur pour éviter fermeture brusque 31float hysteresisThreshold = 100; // *0,1 in s 32 33int previousSwitchState; 34int currentSwitchState = 0; 35 36 37///////////////////////////////////////////////////////// 38//------------------------SETUP------------------------// 39///////////////////////////////////////////////////////// 40 41void setup() { 42 43 Serial.begin(9600); // open a serial connection to your computer 44 45 //Switches 46 pinMode(switchPin, INPUT); 47 pinMode(limitSwitchUpPin, INPUT); 48 pinMode(limitSwitchDownPin, INPUT); 49 50 //Motor (Setup on Channel B of the shield) 51 pinMode(13, OUTPUT); //Initiates Motor Channel B pin 52 pinMode(8, OUTPUT); //Initiates Brake Channel B pin 53 54} 55 56 57///////////////////////////////////////////////////////// 58//-------------------------LOOP------------------------// 59///////////////////////////////////////////////////////// 60 61void loop() { 62 63 //Read state of the switches 64 switchState = digitalRead(switchPin); 65 limitSwitchUpState = digitalRead(limitSwitchUpPin); 66 limitSwitchDownState = digitalRead(limitSwitchDownPin); 67 68 //When switch is pressed, store the info into difference btw below variables 69 if (switchState == HIGH) { 70 previousSwitchState=currentSwitchState; 71 currentSwitchState++; 72 } 73 74 75 /////////////////////////// 76 // INITIALIZE // 77 /////////////////////////// 78 79 //When turning on the arduino, always open the door first 80 if (millis() < 1000) { 81 while (limitSwitchUpState != HIGH) { 82 // Open door 83 openDoor(); 84 limitSwitchUpState = digitalRead(limitSwitchUpPin); 85 limitSwitchDownState = digitalRead(limitSwitchDownPin); 86 } 87 } 88 89 /////////////////////////// 90 // MESURE BRIGHTNESS // 91 /////////////////////////// 92 93 //Get voltage in analogic 94 int brightnessAnalog = analogRead(brightnessPin); 95 //Convert to V 96 float brightnessVoltage = (brightnessAnalog / 1024.0) * 5.0; 97 //Convert to %brighntess 98 float brightness = (brightnessMax - brightnessMin) / (brightnessMinVoltage - brightnessMaxVoltage) * brightnessVoltage 99 + (brightnessMax * brightnessMaxVoltage - brightnessMin * brightnessMinVoltage) / (brightnessMaxVoltage - brightnessMinVoltage); 100 101 102 /////////////////////////// 103 // DEBUG // 104 /////////////////////////// 105 106 Serial.print("BP bas: "); 107 Serial.print(limitSwitchDownState); 108 Serial.print("\ BP haut: "); 109 Serial.print(limitSwitchUpState); 110 Serial.print("\ brightness %: "); 111 Serial.print(brightness); 112// Serial.print(" \ brightness analog: "); 113// Serial.print(brightnessAnalog); 114// Serial.print(" \ brightness voltage: "); 115// Serial.print(brightnessVoltage); 116 Serial.print(" \ hyst_open: "); 117 Serial.print(hysteresis_opening); 118 Serial.print(" \ hyst_clos: "); 119 Serial.print(hysteresis_closing); 120// Serial.print(" \ switch: "); 121// Serial.print(switchState); 122 Serial.print(" \ curr_switch: "); 123 Serial.print(currentSwitchState); 124 Serial.print(" \ prev_switch: "); 125 Serial.println(previousSwitchState); 126 127 128 /////////////////////////// 129 // DOOR // 130 /////////////////////////// 131 132 // CLOSING - if low brightness 133 if (brightness < brightnessLowThreshold && limitSwitchDownState == LOW) { 134 hysteresis_closing++; 135 136 delay(100); 137 138 // Do only if brighntess remains below the threshold for a certain time 139 if(hysteresis_closing > hysteresisThreshold){ 140 // Close door 141 closeDoor(); 142 } 143 } 144 145 // OPENING - if high brightness 146 else if (brightness > brightnessHighThreshold && limitSwitchUpState == LOW) { 147 hysteresis_opening++; 148 delay(100); 149 150 // Do only if brighntess remains below the threshold for a certain time 151 if(hysteresis_opening > hysteresisThreshold){ 152 // Open door 153 openDoor(); 154 } 155 } 156 157 else { 158 analogWrite(11, 0); //Stop motor 159 160 //initialize hysteresis for LDR activation 161 hysteresis_opening=0; 162 hysteresis_closing=0; 163 } 164 165 // CLOSING - if switch button pressed 166 if (currentSwitchState != previousSwitchState && limitSwitchUpState == HIGH) { 167 while (limitSwitchDownState != HIGH) { 168 169 // Close door 170 closeDoor(); 171 limitSwitchUpState = digitalRead(limitSwitchUpPin); 172 limitSwitchDownState = digitalRead(limitSwitchDownPin); 173 174 } 175 previousSwitchState = currentSwitchState; 176 analogWrite(11, 0); //Stop motor 177 } 178 179 // OPENING - if switch button pressed 180 if (currentSwitchState != previousSwitchState && limitSwitchDownState == HIGH) { 181 while (limitSwitchUpState != HIGH) { 182 183 // Open door 184 openDoor(); 185 limitSwitchUpState = digitalRead(limitSwitchUpPin); 186 limitSwitchDownState = digitalRead(limitSwitchDownPin); 187 188 } 189 previousSwitchState = currentSwitchState; 190 analogWrite(11, 0); //Stop motor 191 } 192} 193 194 195///////////////////////////////////////////////////////// 196//----------------------FUNCTIONS----------------------// 197///////////////////////////////////////////////////////// 198 199// Function to open or close the door 200// To reverse engine rotation, juste change the line in the functions: 201// digitalWrite(13, HIGH); // to digitalWrite(13, LOW); or reverse 202 203void closeDoor () { 204 digitalWrite(13, HIGH); //Establishes backward direction of Channel B 205 digitalWrite(8, LOW); //Disengage the Brake for Channel B 206 analogWrite(11, 255); //Spins the motor on Channel B at full speed 207} 208 209void openDoor () { 210 digitalWrite(13, LOW); //Establishes forward direction of Channel B 211 digitalWrite(8, LOW); //Disengage the Brake for Channel B 212 analogWrite(11, 255); //Spins the motor on Channel B at full speed 213}
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)
1// DC Motor shield uses chanels D8 - D11 - D13 - A1 => do not use for 2 other I/O! 3 4///////////////////////////////////////////////////////// 5//----------------------VARIABLES----------------------// 6///////////////////////////////////////////////////////// 7 8// 9 Pins 10const int brightnessPin = A4; //BasDroit Rouge Noir LDR 11const int limitSwitchDownPin 12 = 2; // HautDroit Jaune Blanc 13const int limitSwitchUpPin = 9;// HautDroit Rouge 14 Noir 15const int switchPin = 10; //BasDroit Jaune Blanc 16 17// Intitialize switches 18int 19 switchState = 0; 20int limitSwitchUpState = 0; 21int limitSwitchDownState = 0; 22 23//Calibrate 24 brightness - Max voltage corresponds to min brightness of LDR 25float brightnessMinVoltage 26 = 5; 27float brightnessMaxVoltage = 0; 28float brightnessMin = 0.0; 29float brightnessMax 30 = 100.0; 31 32//Threshold brightness to open/close door 33float brightnessLowThreshold 34 = 1.5; //in % 35float brightnessHighThreshold = 10; //in % 36 37//Hysteresis 38 variables to avoid abrut incessant opening/closing of the door 39int hysteresis_opening; 40 //Compteur pour éviter ouverture brusque 41int hysteresis_closing; //Compteur 42 pour éviter fermeture brusque 43float hysteresisThreshold = 100; // *0,1 in s 44 45int 46 previousSwitchState; 47int currentSwitchState = 0; 48 49 50///////////////////////////////////////////////////////// 51//------------------------SETUP------------------------// 52///////////////////////////////////////////////////////// 53 54void 55 setup() { 56 57 Serial.begin(9600); // open a serial connection to your computer 58 59 60 //Switches 61 pinMode(switchPin, INPUT); 62 pinMode(limitSwitchUpPin, INPUT); 63 64 pinMode(limitSwitchDownPin, INPUT); 65 66 //Motor (Setup on Channel B of the 67 shield) 68 pinMode(13, OUTPUT); //Initiates Motor Channel B pin 69 pinMode(8, 70 OUTPUT); //Initiates Brake Channel B pin 71 72} 73 74 75///////////////////////////////////////////////////////// 76//-------------------------LOOP------------------------// 77///////////////////////////////////////////////////////// 78 79void 80 loop() { 81 82 //Read state of the switches 83 switchState = digitalRead(switchPin); 84 85 limitSwitchUpState = digitalRead(limitSwitchUpPin); 86 limitSwitchDownState 87 = digitalRead(limitSwitchDownPin); 88 89 //When switch is pressed, store the 90 info into difference btw below variables 91 if (switchState == HIGH) { 92 previousSwitchState=currentSwitchState; 93 94 currentSwitchState++; 95 } 96 97 98 /////////////////////////// 99 100 // INITIALIZE // 101 /////////////////////////// 102 103 //When turning 104 on the arduino, always open the door first 105 if (millis() < 1000) { 106 while 107 (limitSwitchUpState != HIGH) { 108 // Open door 109 openDoor(); 110 111 limitSwitchUpState = digitalRead(limitSwitchUpPin); 112 limitSwitchDownState 113 = digitalRead(limitSwitchDownPin); 114 } 115 } 116 117 /////////////////////////// 118 119 // MESURE BRIGHTNESS // 120 /////////////////////////// 121 122 //Get 123 voltage in analogic 124 int brightnessAnalog = analogRead(brightnessPin); 125 //Convert 126 to V 127 float brightnessVoltage = (brightnessAnalog / 1024.0) * 5.0; 128 //Convert 129 to %brighntess 130 float brightness = (brightnessMax - brightnessMin) / (brightnessMinVoltage 131 - brightnessMaxVoltage) * brightnessVoltage 132 + (brightnessMax * 133 brightnessMaxVoltage - brightnessMin * brightnessMinVoltage) / (brightnessMaxVoltage 134 - brightnessMinVoltage); 135 136 137 /////////////////////////// 138 // DEBUG 139 // 140 /////////////////////////// 141 142 Serial.print("BP bas: "); 143 144 Serial.print(limitSwitchDownState); 145 Serial.print("\ BP haut: "); 146 147 Serial.print(limitSwitchUpState); 148 Serial.print("\ brightness %: "); 149 150 Serial.print(brightness); 151// Serial.print(" \ brightness analog: "); 152// 153 Serial.print(brightnessAnalog); 154// Serial.print(" \ brightness voltage: 155 "); 156// Serial.print(brightnessVoltage); 157 Serial.print(" \ hyst_open: 158 "); 159 Serial.print(hysteresis_opening); 160 Serial.print(" \ hyst_clos: 161 "); 162 Serial.print(hysteresis_closing); 163// Serial.print(" \ switch: "); 164// 165 Serial.print(switchState); 166 Serial.print(" \ curr_switch: "); 167 Serial.print(currentSwitchState); 168 169 Serial.print(" \ prev_switch: "); 170 Serial.println(previousSwitchState); 171 172 173 174 /////////////////////////// 175 // DOOR // 176 /////////////////////////// 177 178 179 // CLOSING - if low brightness 180 if (brightness < brightnessLowThreshold && 181 limitSwitchDownState == LOW) { 182 hysteresis_closing++; 183 184 delay(100); 185 186 187 // Do only if brighntess remains below the threshold for a certain time 188 189 if(hysteresis_closing > hysteresisThreshold){ 190 // Close door 191 192 closeDoor(); 193 } 194 } 195 196 // OPENING - if high brightness 197 198 else if (brightness > brightnessHighThreshold && limitSwitchUpState == LOW) { 199 200 hysteresis_opening++; 201 delay(100); 202 203 // Do only if 204 brighntess remains below the threshold for a certain time 205 if(hysteresis_opening 206 > hysteresisThreshold){ 207 // Open door 208 openDoor(); 209 210 } 211 } 212 213 else { 214 analogWrite(11, 0); //Stop motor 215 216 217 //initialize hysteresis for LDR activation 218 hysteresis_opening=0; 219 220 hysteresis_closing=0; 221 } 222 223 // CLOSING - if switch button pressed 224 225 if (currentSwitchState != previousSwitchState && limitSwitchUpState == HIGH) { 226 227 while (limitSwitchDownState != HIGH) { 228 229 // Close door 230 231 closeDoor(); 232 limitSwitchUpState = digitalRead(limitSwitchUpPin); 233 234 limitSwitchDownState = digitalRead(limitSwitchDownPin); 235 236 } 237 238 previousSwitchState = currentSwitchState; 239 analogWrite(11, 0); //Stop 240 motor 241 } 242 243 // OPENING - if switch button pressed 244 if (currentSwitchState 245 != previousSwitchState && limitSwitchDownState == HIGH) { 246 while (limitSwitchUpState 247 != HIGH) { 248 249 // Open door 250 openDoor(); 251 limitSwitchUpState 252 = digitalRead(limitSwitchUpPin); 253 limitSwitchDownState = digitalRead(limitSwitchDownPin); 254 255 256 } 257 previousSwitchState = currentSwitchState; 258 259 analogWrite(11, 0); //Stop motor 260 } 261} 262 263 264///////////////////////////////////////////////////////// 265//----------------------FUNCTIONS----------------------// 266///////////////////////////////////////////////////////// 267 268// 269 Function to open or close the door 270// To reverse engine rotation, juste change 271 the line in the functions: 272// digitalWrite(13, HIGH); // to digitalWrite(13, 273 LOW); or reverse 274 275void closeDoor () { 276 digitalWrite(13, HIGH); //Establishes 277 backward direction of Channel B 278 digitalWrite(8, LOW); //Disengage the 279 Brake for Channel B 280 analogWrite(11, 255); //Spins the motor on Channel 281 B at full speed 282} 283 284void openDoor () { 285 digitalWrite(13, LOW); 286 //Establishes forward direction of Channel B 287 digitalWrite(8, LOW); //Disengage 288 the Brake for Channel B 289 analogWrite(11, 255); //Spins the motor on 290 Channel B at full speed 291}
Downloadable files
Schematics
Schematics

Schematics
Schematics

Schematics_Fritzinig
https://fritzing.org/
Schematics_Fritzinig
Comments
Only logged in users can leave comments