piggy-bank-slot-machine-esp32

ESP32 Arcade Project

Piggy Bank Slot Machine

Saving money is usually boring, so I wanted to turn it into a small arcade game. This project combines a piggy bank with a slot machine: every inserted coin gives one spin, small wins give extra spins, and the jackpot unlocks the money chamber.

The machine is built around an ESP32, a coin sensor, a spin button, a TFT display, and a servo-controlled gate. The main idea is simple: coins go in, the game starts, and the chamber only opens when the machine hits 777.

DIY piggy bank slot machine with ESP32, display, coin slot and servo gate

1. Project Idea

The goal of this project was to make saving coins more fun. Instead of just dropping money into a normal piggy bank, every coin gives the player one spin on a small slot machine. The player can win extra spins, but the money chamber stays locked until the jackpot result appears.

Coin insertedOne coin is detected and one available spin is added.
Spin buttonThe button starts the slot machine when at least one spin credit is available.
Small winsWinning combinations give extra spins, but do not open the money chamber.
JackpotWhen the result is 777, the ESP32 opens the gate to the money chamber.

2. Required Parts

For the first working prototype, I used the following parts:

ControllerESP32 development board
DisplayTFT display controlled with the TFT_eSPI library
Coin sensorTCST2103 optical light gate or similar IR slot sensor
ButtonMomentary push button for starting a spin
Gate motorSmall servo motor for locking and unlocking the money chamber
Resistors220 Ω for the IR LED and 10 kΩ for the sensor signal circuit
Power5 V supply for the ESP32/display setup and a suitable supply for the servo
Housing3D printed two-chamber enclosure with coin slot and front panel
Important: A servo should not be powered directly from a weak ESP32 3.3 V pin. Use a suitable 5 V supply for the servo and connect the ESP32 ground and servo supply ground together.

3. System Overview

The machine has three main jobs: detect an inserted coin, detect the spin button, and control the gate. The ESP32 connects these parts together and decides when the player is allowed to spin or open the money chamber.

Coin passes through the optical sensor
ESP32 adds one spin credit
Button press starts the slot machine animation
ESP32 checks the result and calculates the payout
Only 777 activates the servo gate and opens the money chamber

4. Coin Detection with an Optical Sensor

The most important part of the project is the coin detection. I used a TCST2103 optical switch. This component works like a small light gate. One side contains an infrared LED, and the other side contains a phototransistor.

If there is no coin inside the slot, the infrared light reaches the phototransistor. When a coin falls through the slot, it blocks the light. The ESP32 reads this change as the coin signal.

In the prototype code below, the coin input is configured as an active-HIGH signal: the sensor reads LOW when the slot is free and HIGH while the coin blocks the light gate. This matches the uploaded prototype firmware.

ConditionSensor state in this prototypeESP32 interpretation
No coin in the light gateLOW / freeNo new coin counted
Coin blocks the light gateHIGH / blockedOne coin is accepted and one spin credit is added
Wiring note: The video explanation and the prototype code must use the same signal logic. If your sensor circuit uses a pull-up version where the signal is inverted, change COIN_SENSOR_ACTIVE_STATE in the code or invert the condition in readCoinSensorActive().

5. Spin Button

The spin button is wired from an ESP32 input pin to ground. In the firmware, the ESP32 internal pull-up resistor is enabled. That means the pin is normally HIGH. When the button is pressed, the pin is pulled to ground and becomes LOW.

The firmware detects this falling edge and starts the slot machine only if a spin credit is available. A short debounce time prevents one physical button press from being counted multiple times.

6. Servo Gate for the Money Chamber

The housing uses a two-chamber design. One side contains the electronics and the servo. The other side is the money chamber. The servo acts as a small gate that blocks access to the coins.

At startup, the servo is moved into the closed position. When the jackpot condition is reached, the ESP32 moves the servo to the open position for a short time and then closes it again.

7. Slot Machine Logic

The game logic is intentionally simple. Every spin creates three symbols on the display. The middle payline is the main result. If the symbols form a winning combination, the player gets extra spins. Only the jackpot opens the gate.

ResultRewardGate opens?
777JackpotYes
Other strong winning combinationsExtra spin creditsNo
Small winning combinationsSmall number of extra spinsNo
No winNo extra creditsNo
Design idea: Small wins keep the game fun, but they do not unlock the stored money. The real reward is only triggered by the jackpot result.

8. Prototype Electronics Test

Before putting everything into the housing, I tested the electronics on the bench. For the first prototype setup, the ESP32 was mainly used to provide power, read the coin signal, read the button input, drive the display, and control the servo gate.

I also reused my small hardware monitor to directly check whether the coin recognition and button input were working correctly. This made debugging much easier before the full mechanical assembly.

9. Housing and Mechanical Assembly

After the electronics test, I started assembling the full prototype. The housing uses two chambers: the electronics chamber and the money chamber. The coin sensor sits directly below the coin slot, so every inserted coin must pass through the light gate before it drops into the storage area.

For this first version, I soldered the resistors and wires directly to the sensor leads instead of using a separate PCB. This is not the cleanest final solution, but it gives more freedom for cable routing inside a tight 3D printed housing.

Some parts of the 3D print were joined by carefully melting the PLA edges with an old soldering iron. This works for rough prototypes, but it should be done carefully because the visible surfaces can deform easily.

Prototype warning: Melting PLA with a soldering iron is useful for quick tests, but it is not a clean final assembly method. Work with good ventilation and avoid touching hot plastic or fumes.

10. Display Rotation Issue

During assembly, I noticed that the display was mounted upside down. Luckily, this was only a software problem. The display output can be rotated by 180 degrees in the firmware, so the hardware did not need to be rebuilt.

11. Final Prototype Result

This is the first working prototype of the piggy bank slot machine. A coin gives one spin, the button starts the slot machine, small wins give extra spins, and the jackpot result 777 opens the money chamber.

The prototype still needs improvements, especially in the housing design, cable management, and gate mechanism. But the basic idea works: the ESP32 can detect coins, manage spin credits, run the slot machine logic, and unlock the chamber only after a jackpot.

In the next version, I want to make the design cleaner, improve the gate mechanism, and build a more arcade-like front panel.

Final working prototype of the ESP32 piggy bank slot machine

12. Pin Overview

These are the most important pins used in the uploaded prototype code. Change them if your wiring is different.

FunctionESP32 PinNotes
Coin sensor inputGPIO34Input-only pin. The code expects an external signal circuit and active-HIGH coin detection.
Constant HIGH output for sensor circuitGPIO32Used as a permanent HIGH signal in the prototype wiring.
Spin buttonGPIO33Uses internal pull-up. Button connects the pin to GND when pressed.
Servo signalGPIO27Servo signal only. Use a suitable power supply for the servo.
TFT enable / backlightGPIO17Turns the display enable/backlight signal on.
Touch CS / IRQGPIO26 / GPIO25Included for future use, but not used as touch buttons in this prototype.
Full Prototype Code: piggybank.ino

This is the full Arduino prototype code used for the ESP32 piggy bank slot machine. Create a new Arduino IDE project, paste the code into the main .ino file, install the required libraries, and adjust the pin configuration if your wiring is different.

piggybank.ino
Code Explanation

Libraries

The code uses TFT_eSPI for the display, XPT2046_Touchscreen for future touch support, and ESP32Servo for controlling the gate servo.

Coin Input

The coin sensor is connected to GPIO34. The code expects the signal to become HIGH when the coin blocks the light gate. An interrupt captures the active edge, while the main loop handles lockout and re-arming so that one coin is not counted twice.

Spin Button

The button is connected to GPIO33 and uses the ESP32 internal pull-up. The firmware detects the falling edge from HIGH to LOW and starts a spin if enough credits are available.

Game State Machine

The firmware uses three main states: idle, spinning, and result. During a spin, the reels update with a short animation. After the reels stop, the code checks the winning condition and updates the credits or opens the servo gate.

Jackpot Gate

The servo starts in the closed position. When the slot machine hits 777, the code moves the servo to the open angle, waits for the configured open time, and then closes the gate again.

13. Next Improvements

The first prototype proves that the concept works. For the next version, the biggest improvements are mechanical and visual rather than electrical.

Cleaner housingImprove the 3D model, screw points, display mount, and service access.
Better cable managementUse connectors, shorter cables, and possibly a small custom PCB.
Improved gateMake the locking mechanism stronger, smoother, and harder to bypass.
Arcade front panelAdd a cleaner button layout, graphics, labels, and a more polished look.

Final Thoughts

This project is a fun example of combining simple electronics with a mechanical idea. The electronics are not very complicated on their own: one sensor, one button, one display, and one servo. The interesting part is how these pieces work together to create a small interactive machine.

Feel free to recreate this project, change the winning chances, modify the display design, or build a completely different housing. If you build your own version, make sure the money chamber is mechanically safe and that the electronics are powered correctly.