The very classical way to determine "opened" and "closed" postions of the door is to add "home switches" to the door.
One would be at the bottom and the second at the top of the door.
Well I started with this solution and soon discovered that the chicken are not "clean" birds so that the floor of the coop is often full of "straw" (not to say shit !)
Placing home switches into the coop was not a very good idea.
Instead adding a "rotary encoder" to the shaft has a lot of advantages :
- no flying wires
- no electronic part with contacts which could rust of oxydize
- good precision
- possibility of detecting blockages of the door
I thus made a simple wheel with 8 slots. The wheel rotates in front of an IR sensor and sends "pulses" to the ESP32 inside an interrupt routine
This routine is quite tricky as it has to take into account signal "bouncing" when the slot goes from dark to light...
But the software implementation is finally very very simple !
void IRAM_ATTR optical_ISR() //IR sensor interrupt routine
{
if ((millis() - IRtimeout) > 250)
{
IRtimeout = millis();
if (chickenStatus == opening) tops++;
else tops--;
//Serial.println( tops);
}
}
A simple timeout is added into the interrupt... if the signal bounces faster than 250ms, it will be considered as noise, and only the first pulse will be counted.
Remember that we expect a motor turning at 10 rotations per minute. That is 8*10 = 80 calls of the interrupt every minute... Plenty of time to debounce the switch.
Note as well that the Serial.println(tops); is commented.... If not it may occur that the interupt wll be called a second time whie printing is not finished... and this will crash the ESP32 (guru panic!). DO NOT uncomment this line if you want a safe behavior.
Finally here is the schematics of this sensor
You will note that the IR Led and the phototransistor are not powered directly by the battery, but instead they are powered via a PWR pin of the ESP32. Doing this allows to switch the IR sensor off when the ESP32 is in deepsleep mode. Simply configure PWR pin as "input" and that's it, you save 20mA of the led!
Ok we have now a way to "count the pulses" when the door opens or closes, but we do not know the absolute posiiton of this door... We have to add a "calibration procedure".
Calibration of the door
This calibration will require the help of the user... It must be done once when the door is installed.
- Start with the door in closed position
- touch or press the "Calib" button (do not release it)
- reset the ESP32 (reset button on the lolin lite module)
- the door will start to open
- when in the desired "open position" release the calibration button
That's it the door is calibrated!
You can of course re calibrate the door any time you want, although it shouldn't be necessary as the calibration is stored into the eeprom of the ESP32 and wouldn't erase even after a power disconnection.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.