Count objects with a light gate

Pass objects through the photo-interrupter's slot to count them, with a click from the buzzer for each one.
  • Level:Beginner
  • Duration: 35 min
  • Ages 10+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Count objects with a light gate – EEMENTOR STEM™ lesson

Factories count products on a conveyor belt with light gates: a beam of light across a gap, and a sensor that notices when something blocks it. The photo-interrupter from the Sensor Kit is a tiny light gate. Drop a strip of paper through its slot and the UNO counts it.

What you'll learn

  • How a light gate (photo-interrupter) works
  • How to count events without double-counting
  • How to give feedback with a buzzer

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • Photo-interrupter module (45-in-1 Sensor Kit)
  • Active buzzer (UNO R3 Starter Kit)
  • Strips of card to pass through the slot
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Wiring

Breadboard wiring diagram: Count objects with a light gate
Breadboard view. Check each connection against the wiring table below.
From To
Photo-interrupter S UNO pin 2
Photo-interrupter + 5V
Photo-interrupter − GND
Buzzer + (longer leg) UNO pin 8
Buzzer − GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

The sketch

Some versions of this module switch their output to HIGH when they trigger, others to LOW. The sketch reads the output when it starts (keep things quiet and still for a moment) and treats any change from that resting state as a trigger, so it works with both.

const int GATE_PIN = 2;
const int BUZZER_PIN = 8;

int clearState;        // output when the slot is empty
bool wasBlocked = false;
long count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(GATE_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  delay(300);
  clearState = digitalRead(GATE_PIN);
  Serial.println("Pass something through the slot");
}

void loop() {
  bool blocked = digitalRead(GATE_PIN) != clearState;

  if (blocked && !wasBlocked) {     // something just entered the slot
    count++;
    Serial.print("Count: ");
    Serial.println(count);
    digitalWrite(BUZZER_PIN, HIGH); // short click
    delay(30);
    digitalWrite(BUZZER_PIN, LOW);
  }
  wasBlocked = blocked;
  delay(2);
}

How it works

One side of the slot has an infrared LED, the other side a light sensor. When something blocks the beam, the output changes. The sketch counts only the moment the beam becomes blocked (the "edge"), so an object that stays in the slot is counted once, not hundreds of times.

Try this next

  • Measure how long the beam is blocked to estimate how fast an object passes.
  • Show the count on the 1602 LCD from the UNO kit.
  • Glue a strip of card to a wheel and count its turns.

Troubleshooting

  • Counts jump by more than one: pass the card through in one smooth movement, or add a short delay after each count.
  • Nothing is counted: make sure the object fully blocks the slot; thin paper may let light through.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit