I want to automate the chicken coop of my grandmother. To do so, I want to use Arduinos and NodeMCUs, respectively. I want to check if all chickens are in the coop at night and then close a little door. I was trying to use RFID technology and tag the chickens with a little RFID tag but the read range I got was too short to get it working properly. Now, I am trying to implement a light gate with two small lasers and two receivers. I want to detect the direction (in or out) of the chickens. I got the lasers working and I get a signal (1 and 0 = light gate interrupted). I have to know which of the two laser detectors first detected a 0 (zero). I have no idea how to implement this in code. I was trying to use timers to no avail. I also was trying to use interrupts (see small code fragment).

I hope I made clear what I am trying to achieve. Please tell me if and how I can improve this question. Any google fu was to no avail. Every hint/help is greatly appreciated.

#include <Arduino.h>

// Functions
void pin_ISR();

const int detectorPin = 3;     // the number of the detector pin
const int ledPin =  13;      // the number of the LED pin
const int Laser = 6;
// variables will change:
volatile int buttonState = 1;         // variable for reading the status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(detectorPin, INPUT);
pinMode(Laser, OUTPUT);
digitalWrite(Laser, HIGH);
// Attach an interrupt to the ISR vector
attachInterrupt(digitalPinToInterrupt(3), pin_ISR, CHANGE);

void loop() {

void pin_ISR() {
Serial.println("There was an interrupt!");
buttonState = digitalRead(detectorPin);
digitalWrite(ledPin, buttonState);
  • You have only one light gate in your code. Add the second. – Juraj Nov 1 '18 at 11:40
  • Do the light gates both get active together, or one active then inactive, followed by the other? I.e., do you have a sequence of "HH -> LH -> LL -> HL -> HH" or do you have "HH -> LH -> HH -> HL -> HH" for the gates? – Majenko Nov 1 '18 at 12:19

Consider using quadrature encoding to detect travel direction using two light beams. A method used in older mechanical mouse pointer devices and track balls. Using an Arduino to decode quadrature encoding is talked about in many places on the web.

Place beam A and beam B such that a chicken waling out of the coop will activate beam B first and beam A second.

enter image description here

Because the activation of beam B occurs first we know the chicken is walking out of the coop. If beam A occurred first we know the chicken is walking into the coop.

The shape of a chicken is irregular and a chicken may change its mind half way out of the coop. These and other unexpected anomalies may lead to unexpected results. Consider supplementing the beams with a PIR sensor outside the coop to confirm there are no chickens left outside.

| improve this answer | |
  • Thanks for all your help and efforts. @juraj, I was aware that I just implemented one light gate. It was just in order to deliver sample code for showing how I was using interrupts. st2000, Thanks for your big help, but I am not the lightest light in the room, so the quadrature encoding was a little to complex for me. I finally figured out that I have to search for "people counter arduino" or "bidirectional counter". Here, I found a really similar project and I can adapt much of the code from there. – Strohmi Nov 3 '18 at 20:30
  • Don't sell your self short. The program you linked to is using quadrature encoding. They just don't say it. Perhaps it would help to think of the clock wise rotation of the wheel to be a chicken going into the coop and the counter clock wise rotation of the wheel to be a chicken going out of the coop. – st2000 Nov 4 '18 at 12:35
  • @Strohmi, I asked you to add the second light gate, because then it would be easy to add evaluation of which one triggered first – Juraj Nov 5 '18 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.