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.
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)
| Segment | ATmega16 Pin |
|---|---|
| a | PB0 |
| b | PB1 |
| c | PB2 |
| d | PB3 |
| e | PB4 |
| f | PB5 |
| g | PB6 |
Hex Code for Digits 0–9
| Digit | Segments | Hex Code |
|---|---|---|
| 0 | a b c d e f | 0x3F |
| 1 | b c | 0x06 |
| 2 | a b g e d | 0x5B |
| 3 | a b c d g | 0x4F |
| 4 | f g b c | 0x66 |
| 5 | a f g c d | 0x6D |
| 6 | a f g e c d | 0x7D |
| 7 | a b c | 0x07 |
| 8 | a b c d e f g | 0x7F |
| 9 | a b c d f g | 0x6F |
Connection Summary
- Opto-interrupter output → INT0 (PD2)
- Seven segment → PORTB (PB0–PB6)
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]