Coin machines, vending machines and money boxes in shops count coins with light. Each coin rolls past a beam of light and blocks it for a moment. In this workshop project, the photo-interrupter from the Sensor Kit is that light barrier: drop a coin edge-first through its slot, and the UNO counts it, beeps and shows the total on the LCD.
What you'll learn
- How a photo-interrupter (light gate) works
- How to count events without counting the same one twice
- How fast an object passes and why the sketch must never be slower
- How to show a running count on the LCD
What you need
- UNO R3 board and USB cable (UNO R3 Starter Kit)
- Photo-interrupter module (45-in-1 Sensor Kit)
- Active buzzer module (45-in-1 Sensor Kit)
- 1602 LCD display, 10 kΩ potentiometer and 1 resistor, 220 Ω (UNO R3 Starter Kit)
- A few small coins
- Breadboard and jumper wires (UNO R3 Starter Kit)
Before you start
The LCD has 16 connections in a row. If yours has no pins, solder 16 pins from the 40-pin male header in the kit first, so it plugs into the breadboard. Ask an adult to help if you're new to soldering.
Make it real: the slot is only about 5 mm wide, so the coin has to fall through it on its edge. Fold a strip of card into a narrow channel that ends right above the slot, and put a cup underneath to catch the coins.
Wiring

| From | To |
|---|---|
| LCD VSS, RW and K | GND |
| LCD VDD | 5V |
| LCD V0 | Middle pin (W) of the potentiometer |
| LCD RS | UNO pin 12 |
| LCD E | UNO pin 11 |
| LCD D4, D5, D6, D7 | UNO pins 5, 4, 3, 2 |
| LCD A (backlight +) | 220 Ω resistor, then 5V |
| Potentiometer outer pins | 5V and GND |
| Photo-interrupter S | UNO pin 7 |
| Photo-interrupter + and − | 5V and GND |
| Active buzzer module S | UNO pin 8 |
| Active buzzer module + and − | 5V and GND |
Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.
The sketch
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS, E, D4, D5, D6, D7
const int GATE_PIN = 7;
const int BUZZER_PIN = 8;
int gateRest; // the reading with nothing in the slot
bool blocked = false;
unsigned long lastCoin = 0;
long coins = 0;
void showCount() {
lcd.setCursor(0, 1);
lcd.print("Coins: ");
lcd.print(coins);
lcd.print(" ");
}
void setup() {
pinMode(GATE_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
lcd.begin(16, 2);
lcd.print("Coin counter");
delay(500);
gateRest = digitalRead(GATE_PIN); // the slot must be empty now
showCount();
}
void loop() {
bool nowBlocked = digitalRead(GATE_PIN) != gateRest;
// count at the moment the beam is broken, at most once every 80 ms
if (nowBlocked && !blocked && millis() - lastCoin > 80) {
coins++;
lastCoin = millis();
showCount();
digitalWrite(BUZZER_PIN, HIGH);
delay(30);
digitalWrite(BUZZER_PIN, LOW);
}
blocked = nowBlocked;
}
How it works
The photo-interrupter has a small infrared LED on one side of the slot and a light sensor on the other. While the slot is empty the sensor sees the light; a coin passing through blocks it for a moment, and the module's output changes. The sketch learns the "empty" reading at start-up, so it works with modules that switch to HIGH and those that switch to LOW.
A falling coin passes the slot in a few thousandths of a second, so the sketch checks the gate all the time and never waits, except for the 30 ms beep after a coin. It counts only at the moment the beam goes from free to blocked (an edge), and ignores anything within 80 ms of the last coin. A coin that wobbles in the slot can't be counted twice that way.
Try this next
- Add a reset button that sets the count back to zero.
- Save the count in EEPROM so the money box remembers it without power.
- Measure how long each coin blocks the beam: bigger coins block it longer, which is a first step towards telling coins apart.
Troubleshooting
- The screen lights up but shows only blocks or nothing: turn the potentiometer slowly from one end to the other.
- Coins are not counted: make sure each coin really passes through the slot, edge first, and that the slot was empty at start-up.
- One coin counts several times: raise the 80 ms to 150.
- It counts without coins: strong sunlight can reach the sensor. Shade the slot.