Introduction
A UK-based fitness chain came to us with a specific problem. Their gyms were losing revenue because their traditional vending machines—coin-operated, dumb boxes—were constantly jammed, out of stock of popular items, and completely invisible to management. Members wanted to grab a protein shake or a recovery bar after a workout, but they rarely carried cash. The machines were essentially expensive storage units.
They didn’t want an off-the-shelf "smart" retrofit kit. Those are often locked into proprietary payment processors and cloud services with high monthly fees. They wanted a
custom vending solution that they owned completely. They needed a machine that accepted Apple Pay, Google Pay, and credit cards, reported real-time inventory, and could survive the humidity and vibration of a busy gym floor.
The result was a
Gym Vending Machine that was cheaper to build than a commercial retrofit, completely under their control, and ready for production.
The Technical Problem
The core challenge was bridging the gap between a high-reliability mechanical system (the motor-driven coils that dispense products) and a modern, secure payment ecosystem. We also had to handle the physical constraints of the gym environment.
The specific requirements were:
1. Cashless Payments: Accept contactless EMV (Europay, Mastercard, Visa) transactions via NFC (Near Field Communication). This meant integrating a certified payment terminal module.
2. Remote Inventory Management: The machine needed to know exactly how many items were left in each coil and report that data to a cloud dashboard.
3. Over-the-Air (OTA) Updates: We couldn't send an engineer to every gym to update firmware. The system had to update itself securely.
4. Robust Connectivity: The machine sits in a corner of a gym, often far from the Wi-Fi router. It needed stable, long-range wireless connectivity.
Step-by-Step Build Process
We didn't start with a circuit board. We started with a whiteboard and a list of datasheets. Here is exactly how we built it.
Step 1: Hardware Architecture and Component Selection
We chose the ESP32-WROOM-32 as the main MCU (Microcontroller Unit). Why? It has built-in Wi-Fi and Bluetooth, dual-core processing (which is critical for handling network traffic *and* motor control simultaneously without lag), and a very mature SDK.
For the payment system, we integrated the ACR1255U-J1 NFC reader module. This is a PC/SC-compliant reader that handles the heavy lifting of EMV contactless transactions. We communicate with it over I2C. The ESP32 sends a command to read a card, the module handles the cryptographic handshake with the payment processor (Stripe in this case), and returns a simple "success/fail" signal. This kept our firmware simple and secure—we never touch the raw card data.
For the motor control, we used a set of **DRV8833** dual H-bridge motor drivers. Each driver can control two 12V DC motors (the coils). We designed a PCB that could handle up to 16 coils, which was the maximum for this machine size.
Connectivity: The ESP32's Wi-Fi was sufficient, but we added an external 2.4GHz antenna port (u.FL connector) to allow us to place the antenna on the metal chassis' exterior. This solved the "far corner of the gym" problem.
Step 2: PCB Design in KiCad
We designed a custom 4-layer PCB. The key design rules we followed:
- Power Plane: A dedicated layer for 12V and 5V. The coils can draw up to 2A peak when starting, so we needed thick traces and a solid ground plane to avoid voltage drops that could reset the ESP32.
- Signal Isolation: The motor drivers are on the right side of the board, the ESP32 and NFC reader are on the left. We used a physical gap and separate ground pours to prevent the electrical noise from the motors from corrupting the NFC communication.
- Connectors: We used JST-XH connectors for the motors (keyed, so you can't plug them in backwards) and a USB-C port for firmware flashing and debugging.
We sent the Gerber files to JLCPCB. The boards cost about $12 each for a batch of 50.
Step 3: Firmware Development with ESP-IDF
We wrote the firmware in C using the official Espressif IoT Development Framework (ESP-IDF). We did not use Arduino. For a production machine that needs to run 24/7 without crashing, you need the real-time operating system (FreeRTOS) and the low-level control that ESP-IDF provides.
The firmware architecture was split into three main tasks:
1. Payment Task: Listens for NFC cards. On detection, it wakes up the ACR1255U, initiates the transaction, and blocks until a response. If the payment fails, it sends a message to the LCD screen ("Card Declined").
2. Vending Task: Manages the motor state machine. It receives a "dispense item X" command from the payment task. It activates the DRV8833 for exactly 1.2 seconds (calibrated for a standard coil). It then checks a hall-effect sensor on the coil to confirm the item dropped. If the sensor doesn't trip, it retries once, then logs an error.
3. Network Task: Connects to Wi-Fi using a stored credential (we pre-configure them in flash). It then opens a secure MQTT connection to our cloud server (AWS IoT Core). It publishes inventory counts every 10 minutes and immediately publishes a "sold" event.
OTA Update Logic: The firmware checks for a new version on the MQTT topic `ota/update/available`. If a new binary is available, it downloads it to the SPI flash, verifies its CRC, and reboots into the new partition. This all happens in the background without interrupting a sale.
Step 4: 3D Design and Mechanical Integration
This was the most challenging part. The machine chassis was a standard 1.2mm steel sheet metal box. We designed a 3D-printed bracket (using PETG filament) to hold the ESP32 board, the NFC reader, and the LCD screen. The bracket snaps into the door of the machine.
Key design decisions:
- NFC Antenna Placement. The ACR1255U has a built-in antenna. We placed it directly behind a 3mm thick plastic panel on the door. Metal kills NFC. We had to ensure there was no steel within 20mm of the antenna. We used a plastic spacer to achieve this.
- Motor Mounts: The standard vending machine coils are driven by a splined shaft. We designed a simple 3D-printed coupling that connects the DRV8833 motor's shaft to the coil shaft. This cost $0.20 in filament instead of $8 for a custom metal part.
- Cable Management: We routed all 16 motor cables (plus 2 for the hall-effect sensors) through a single cable chain. This prevented the wires from snagging on the rotating coils.
Step 5: Prototype Assembly and Testing
We assembled the first unit in our lab. We loaded it with 10 different products. The first test was simple: tap a card, get a soda.
What went wrong on the first try:
- Wi-Fi Dropouts: The ESP32 was losing connection when a motor started. The inrush current from the 12V motors was causing a voltage sag on the 3.3V rail. Solution: We added a 470µF electrolytic capacitor near the motor driver power input. Problem solved.
- NFC Read Failures: The metal chassis was reflecting the NFC field. We had to move the NFC reader 5mm further away from the steel door using a 3D-printed standoff.
After these fixes, the machine ran for 72 hours straight without a single failure. We then shipped the prototype to the gym chain's flagship location.
Advantages of This Custom Solution
Why build a custom machine instead of buying a "smart" retrofit kit? Here are the concrete advantages we delivered.
1. Total Ownership and No Recurring Fees
Commercial retrofit kits often charge $10-$20 per month per machine for cloud access and payment processing. Our solution uses AWS IoT Core (which costs pennies per month) and a direct integration with Stripe (standard 2.9% + $0.30 per transaction). The gym chain owns the firmware, the PCB design, and the cloud code. They can switch payment processors tomorrow if they want.
2. Real-Time, Granular Inventory
The hall-effect sensors on each coil tell us exactly how many items are left. Not "low stock" or "out of stock". We know that coil #7 (Protein Bars) has 3 items left. This data feeds directly into a dashboard that the gym manager sees on their phone. They can restock before the machine is empty. This increased their sales by an estimated 18% in the first month because they never had empty slots for popular items.
3. Full Control Over the User Experience
The gym wanted to show a QR code on the screen after a purchase that linked to a post-workout survey. With a commercial machine, you can't do that. With our custom firmware, we just added a text element to the LVGL (Light and Versatile Graphics Library) UI. It took one hour.
4. Lower Hardware Cost
The BOM (Bill of Materials) for our controller board, including the ESP32, the NFC module, the LCD, and the motor drivers, was under $45. A commercial retrofit kit with similar capabilities costs over $200. For a chain with 50 gyms, that is a $7,750 savings just on the controller.
Uses and Applications
This specific design was for a **Gym Vending Machine**, but the core architecture is a platform we can adapt quickly.
- Office Pantries: Replace the coin-op coffee machine with a cashless system that integrates with employee badges.
- Hotel Mini-Bars: A small, connected fridge that charges a room automatically when an item is removed.
- Laundry Rooms: A machine that accepts digital payments and sends a "cycle done" notification.
- College Dorms: A machine that only accepts meal plan credits via a university app.
The key is the separation of concerns. The ESP32 handles the logic and connectivity. The NFC module handles the payment security. The motor drivers handle the physical dispensing. We can swap out any of these components without rewriting the entire system.
Conclusion
Building a smart, cashless vending machine from scratch sounds intimidating, but with the right engineering approach, it is not only possible—it is often the better business decision. The off-the-shelf solutions are designed for maximum profit through recurring fees, not for maximum utility for the operator.
By choosing the ESP32 as the brain, integrating a certified NFC payment module, and writing our own firmware in ESP-IDF, we delivered a machine that is more reliable, cheaper to operate, and completely customizable.
The gym chain now has a custom vending solution that they own. They can add new features (like temperature sensors for cold items, or a camera for visual stock confirmation) without waiting for a third-party vendor. They have a direct line to our engineers, not a support ticket system.
If you have a physical product that needs to accept digital payments, report data, or be controlled remotely, the technology is mature and accessible. You don't need to buy a black box. You can build the exact machine you need.
---
Discussion (0 comments)