4.12) Case Study: Single Digit Event Counter Using Opto-Interrupter and Seven Segment Display

posted by Hamid Sayyed • November 14, 2025 0 Comments

Single-digit event counters are widely used in industrial automation, laboratory instruments, and consumer devices where each passing object or interruption must be counted electronically. An opto-interrupter (also called a slotted optical switch) is one of the most reliable sensors for such counting applications. It detects the movement of an object by sensing the interruption of light between an IR LED and phototransistor placed inside a narrow slot. When an object passes through this slot, the light beam breaks and generates a clean digital pulse that can be detected by a microcontroller like ATmega16. By using a seven-segment display, the detected pulses can be visualised in real time as digits ranging from 0 to 9. This experiment helps beginners understand sensing, debouncing, real-time counting, digital display driving, and interfacing external hardware with AVR ports. In this tutorial, we will build a complete event counter system, write the C program, and visualize the working with an animated diagram using JavaScript.

Understanding the Opto-Interrupter

An opto-interrupter consists of:

  • IR LED on one side (emitter)
  • Phototransistor on the opposite side (receiver)
  • A slot in between where objects pass

When nothing is inside the slot, IR light reaches the phototransistor, keeping its output LOW or HIGH depending on configuration. When an object interrupts the beam, the transistor switches state, producing a clean digital pulse.

Important: Most opto-interrupters require a pull-up resistor on the microcontroller input pin to ensure sharp transitions between HIGH and LOW levels.

Seven Segment Display Overview

A single digit 7-segment display contains LEDs arranged to show numbers 0–9. For a common cathode display, all LED cathodes are tied together to ground, and segments glow when their anode pins receive HIGH signals.

Segment-to-PIN Mapping (Common Cathode)

SegmentATmega16 Pin
aPB0
bPB1
cPB2
dPB3
ePB4
fPB5
gPB6

Hex Code for Digits 0–9

DigitSegmentsHex Code
0a b c d e f0x3F
1b c0x06
2a b g e d0x5B
3a b c d g0x4F
4f g b c0x66
5a f g c d0x6D
6a f g e c d0x7D
7a b c0x07
8a b c d e f g0x7F
9a b c d f g0x6F

Connection Summary

  • Opto-interrupter output → INT0 (PD2)
  • Seven segment → PORTB (PB0–PB6)
Why INT0? Using the external interrupt pin improves accuracy and detects even very fast moving objects.

C Program — Single Digit Counter Using Opto-Interrupter

#include <avr/io.h>
#include <avr/interrupt.h>

uint8_t count = 0;

uint8_t segCode[10] = {
  0x3F,0x06,0x5B,0x4F,0x66,
  0x6D,0x7D,0x07,0x7F,0x6F
};

ISR(INT0_vect)
{
    count++;
    if(count > 9) count = 0;
}

int main(void)
{
    DDRB = 0x7F;      // PB0–PB6 as output
    PORTB = segCode[0];

    DDRD &= ~(1<<PD2);   // INT0 input
    PORTD |= (1<<PD2);   // Pull-up enabled

    MCUCR |= (1<<ISC01);  // Falling edge trigger
    GICR |= (1<<INT0);    // Enable INT0
    sei();                 // Global interrupt enable

    while(1)
    {
        PORTB = segCode[count];
    }
}
  

Animated Working Diagram

The following animation shows an object passing through the opto-interrupter, generating pulses which increment the seven segment display. Each time the virtual object crosses the sensor, the number increases from 0 to 9 and repeats.

Applications

  • Object counters in factories
  • Laboratory experiment counters
  • RPM / speed measurement
  • Visitor counters in halls
  • Conveyor belt automation

Conclusion

Opto-interrupter based counters are simple yet powerful and highly reliable for real-time counting tasks. With ATmega16 and a seven-segment display, this project becomes an excellent experiment to understand sensors, interrupts, and display driving. Once you understand this basic setup, it can be extended to multi-digit counters, LCD display counters, RPM meters, and even high-speed industrial automation systems.

Comments

Post a Comment

Subscribe to Post Comments [Atom]