Build it step by step
0 of 7 doneWire the sensor
OUT to D2, 5V and GND.
Wire the buttons and buzzer
Value on D3, reset on D4, buzzer on D8.
Wire the OLED
SDA to A4, SCL to A5, 5V and GND.
Upload the sketch
The screen shows the next coin value and a total of 0.
Mount the sensor at the slot
So a falling coin breaks the beam.
Press the value button
The next coin value changes.
Drop a coin
A tick, and the total goes up.
How do you build a piggy bank coin counter?
A sensor at the slot of a piggy bank spots each coin dropping in, and two buttons choose the coin's value before each drop, so the screen shows the total saved. A simple way to learn about counting and saving. The count is an estimate, so always count the coins by hand as well. You need Arduino Nano, IR obstacle avoidance sensor module, SSD1306 OLED Display, Tactile push button 6 mm × 2, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 80 minutes.
- Level
- Beginner
- Time
- About 80 min
- Parts
- 7
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- DisplaysBuy
- SwitchesBuy
- Motors & ActuatorsBuyPassive buzzer × 1
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| IR sensor OUT | Nano D2 | Goes low when a coin passes |
| IR sensor VCC and GND | Nano 5V and GND | Sensor power |
| Value button | Nano D3 and GND | Steps the coin value 1, 2, 5, 10 |
| Reset button | Nano D4 and GND | Hold for two seconds to empty the total |
| Passive buzzer + and - | Nano D8 and GND | A tick for each coin |
| OLED SDA and SCL | Nano A4 and A5 | I2C data and clock |
| OLED VCC and GND | Nano 5V and GND | Power for a 0.96 inch module |
Code
The sensor looks across the coin slot, so a coin dropping through breaks its beam for an instant. The sketch counts one coin on each break, and ignores further breaks for 300 milliseconds so one coin cannot be counted twice. The sensor cannot tell one coin from another, so you choose the value of the next coin with the value button before each drop, stepping through 1, 2, 5 and 10. The total and the number of coins are saved in the Nano's EEPROM after every coin, so they survive a power cut. Holding the reset button for two seconds empties the total, which you do when you empty the piggy bank. The count is only an estimate: a coin that bounces, two coins together, or a missed drop will make it wrong, so always count the real coins as well. Turn the sensor's small knob until it sees a falling coin but not the slot's sides, and shade it from bright light. Install "Adafruit SSD1306" and "Adafruit GFX" from the Library Manager. Keep the electronics out of the coin path, and keep small coins away from small children. Try a second sensor lower down to measure a coin's size and guess its value automatically.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Libraries: Adafruit SSD1306 and Adafruit GFX.
#include <Wire.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const uint8_t IR_PIN = 2;
const uint8_t BTN_VALUE = 3;
const uint8_t BTN_RESET = 4;
const uint8_t BUZZ = 8;
const unsigned int VALUES[4] = {1, 2, 5, 10};
const unsigned long LOCKOUT_MS = 300;
uint8_t valueIdx = 2; // start at 5
unsigned long total = 0;
unsigned int coins = 0;
bool prevIr = false, prevValue = false;
unsigned long lastCoin = 0;
unsigned long resetHold = 0;
void draw() {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print(F("Next coin: Rs "));
oled.print(VALUES[valueIdx]);
oled.setTextSize(3);
oled.setCursor(0, 18);
oled.print(total);
oled.setTextSize(1);
oled.setCursor(0, 54);
oled.print(coins);
oled.print(F(" coins (estimate)"));
oled.display();
}
void save() {
EEPROM.put(0, total);
EEPROM.put(4, coins);
}
void setup() {
pinMode(IR_PIN, INPUT);
pinMode(BTN_VALUE, INPUT_PULLUP);
pinMode(BTN_RESET, INPUT_PULLUP);
pinMode(BUZZ, OUTPUT);
Serial.begin(9600);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
EEPROM.get(0, total);
EEPROM.get(4, coins);
if (total == 0xFFFFFFFFUL) { total = 0; coins = 0; } // nothing saved yet
draw();
}
void loop() {
unsigned long now = millis();
bool ir = (digitalRead(IR_PIN) == LOW);
if (ir && !prevIr && now - lastCoin > LOCKOUT_MS) { // a coin passed the beam
lastCoin = now;
total += VALUES[valueIdx];
coins++;
save();
tone(BUZZ, 1800, 40);
Serial.print(F("Coin of Rs "));
Serial.print(VALUES[valueIdx]);
Serial.print(F(", total Rs "));
Serial.println(total);
draw();
}
prevIr = ir;
bool v = (digitalRead(BTN_VALUE) == LOW);
if (v && !prevValue) { valueIdx = (valueIdx + 1) % 4; draw(); delay(20); }
prevValue = v;
if (digitalRead(BTN_RESET) == LOW) { // hold two seconds to empty the total
if (resetHold == 0) resetHold = now;
if (now - resetHold > 2000) {
total = 0; coins = 0; save();
tone(BUZZ, 600, 200);
draw();
resetHold = now + 100000UL;
}
} else {
resetHold = 0;
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Coin through the slot | One tick and the total rises |
| Value button | Steps through 1, 2, 5 and 10 |
| Power cut | The total is remembered |
| Two-second reset hold | The total goes to zero |
| Serial monitor | Prints each coin and the total |
If it does not work
Counts without a coin
Turn the knob to reduce the range or shade the sensor.
Misses coins
Turn the knob up and aim the sensor across the slot.
Counts a coin twice
Increase LOCKOUT_MS.
Blank screen
Check the OLED wires and try address 0x3D.
Total looks wrong
It is an estimate, so reset and count by hand.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the piggy bank coin counter?
Arduino Nano, IR obstacle avoidance sensor module, SSD1306 OLED Display, Tactile push button 6 mm × 2, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the piggy bank coin counter take to build?
About 80 minutes for a beginner maker, in 7 steps.
How do I know the piggy bank coin counter works?
Coin through the slot: One tick and the total rises. Value button: Steps through 1, 2, 5 and 10. Power cut: The total is remembered.
Counts without a coin: what should I check?
Turn the knob to reduce the range or shade the sensor.
Misses coins: what should I check?
Turn the knob up and aim the sensor across the slot.
Counts a coin twice: what should I check?
Increase LOCKOUT_MS.
Blank screen: what should I check?
Check the OLED wires and try address 0x3D.
