Community project

Wireless Coin Controller

Photo of Wireless Coin Controller

Chen Henry

Published November 17, 2026

The Wireless Coin Controller is an ESP32-based remote control that sends commands over ESP-NOW wireless protocol to a compatible receiver. Built around the M5Stack StickS3 platform and powered via USB, it features a debounced button input that queues and transmits radio packets with sequence tracking and delivery confirmation.

This guide provides a complete wiring diagram, parts list, and step-by-step assembly instructions for building the controller. You'll also receive the firmware code that handles button debouncing, packet queuing, and reliable wireless transmission to a target device on a dedicated radio channel.

Wiring diagram

Wiring diagram for Wireless Coin Controller
Loading diagram…

Assemble it in 2 steps

1. 准备 StickS3

本项目只用 StickS3 板载 KEY1(GPIO11)按钮。无需外接元件,也不需要连接 K10 的任何电线。不要把电源按钮当成金币按钮。

2. 使用 USB 供电

使用能传输数据的 USB-C 线连接 StickS3 与电脑,K10 单独供电。测试时将两台设备放在附近。

  • 本项目使用 USB 供电,不改动板载电池或电源线路。
  • 已设为单播,目标 K10 STA MAC 为 7C:DF:A1:FE:D7:B8,双方信道必须为 1。

Deploy the firmware

#include <Arduino.h>
#include <M5Unified.h>
#include <WiFi.h>
#include <esp_now.h>
#include <esp_wifi.h>
#include <esp_system.h>
#include <esp_idf_version.h>

constexpr uint8_t COIN_RADIO_CHANNEL = 1;
// Unencrypted unicast to the user-provided K10 STA MAC on channel 1.
constexpr bool BROADCAST_TEST = false;
uint8_t targetMac[6] = {0x7C,0xDF,0xA1,0xFE,0xD7,0xB8};
struct __attribute__((packed)) CoinRadioPacket {
  char magic[4]; uint8_t version; uint8_t command;
  uint16_t reserved; uint32_t sequence;
};
static_assert(sizeof(CoinRadioPacket)==12, "Unexpected radio packet size");
constexpr int KEY1_PIN = 11; // built-in KEY1, active LOW; initialized by M5.begin
constexpr uint32_t DEBOUNCE_MS=15, SEND_GAP_MS=100;
CoinRadioPacket queue[32];
uint8_t head=0, count=0, attempts=0;
uint32_t nextSequence, lastSend=0, rawSince=0;
bool radioReady=false, pending=false, rawPressed=false, stablePressed=false, armed=false;
portMUX_TYPE callbackMux=portMUX_INITIALIZER_UNLOCKED;
volatile bool callbackReady=false;
volatile esp_now_send_status_t callbackStatus;
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,5,0)
void onSent(const wifi_tx_info_t*, esp_now_send_status_t status) {
#else
void onSent(const uint8_t*, esp_now_send_status_t status) {
#endif
  portENTER_CRITICAL(&callbackMux);
  callbackStatus=status; callbackReady=true;
  portEXIT_CRITICAL(&callbackMux);
}
void popEvent() { head=(head+1)%32; --count; attempts=0; }
void trigger() {
  uint32_t seq=nextSequence++;
  if (!radioReady) { Serial.printf("event=%lu rejected: radio unavailable\n",(unsigned long)seq); return; }
  if(count==32) { Serial.printf("event=%lu dropped: sender queue full\n",(unsigned long)seq); return; }
  queue[(head+count)%32]={{'C','O','I','N'},1,1,0,seq}; ++count;
  Serial.printf("event=%lu queued depth=%u\n",(unsigned long)seq,count);
}
void setup() {
  auto cfg=M5.config();
  cfg.internal_imu=false; cfg.internal_spk=false; cfg.internal_mic=false;
  M5.begin(cfg); // vendor initializes onboard power and KEY1/KEY2 before use
  Serial.begin(115200);
  rawPressed=stablePressed=(digitalRead(KEY1_PIN)==LOW);
  rawSince=millis(); armed=!stablePressed; // held during reboot is not a new press
  WiFi.mode(WIFI_STA); WiFi.setAutoReconnect(false); WiFi.disconnect();
  esp_err_t channelResult=esp_wifi_set_channel(COIN_RADIO_CHANNEL,WIFI_SECOND_CHAN_NONE);
  WiFi.setSleep(false);
  nextSequence=esp_random();
  Serial.printf("Coin sender STA MAC=%s target=%02X:%02X:%02X:%02X:%02X:%02X channel=%u mode=%s seed=%lu\n",
    WiFi.macAddress().c_str(),targetMac[0],targetMac[1],targetMac[2],targetMac[3],targetMac[4],targetMac[5],COIN_RADIO_CHANNEL,
    BROADCAST_TEST?"BROADCAST TEST":"UNICAST",(unsigned long)nextSequence);
  Serial.printf("channel init=%s\n",esp_err_to_name(channelResult));
  if(channelResult!=ESP_OK) return;
  esp_err_t result=esp_now_init(); Serial.printf("ESP-NOW init=%s\n",esp_err_to_name(result));
  if(result!=ESP_OK) return;
  result=esp_now_register_send_cb(onSent);
  if(result!=ESP_OK) { Serial.printf("callback init=%s\n",esp_err_to_name(result)); return; }
  esp_now_peer_info_t peer={}; memcpy(peer.peer_addr,targetMac,6);
  peer.channel=COIN_RADIO_CHANNEL; peer.ifidx=WIFI_IF_STA; peer.encrypt=false;
  result=esp_now_add_peer(&peer); Serial.printf("peer init=%s\n",esp_err_to_name(result));
  radioReady=(result==ESP_OK);
  Serial.println("KEY1: one event per press; link callback is NOT K10 playback/count confirmation.");
}
void loop() {
  uint32_t now=millis();
  bool pressed=digitalRead(KEY1_PIN)==LOW;
  if(pressed!=rawPressed) { rawPressed=pressed; rawSince=now; }
  if(now-rawSince>=DEBOUNCE_MS && stablePressed!=rawPressed) {
    stablePressed=rawPressed;
    if(!stablePressed) armed=true;
    else if(armed) { armed=false; trigger(); }
  }
  bool done; esp_now_send_status_t status;
  portENTER_CRITICAL(&callbackMux);
  done=callbackReady; status=callbackStatus; callbackReady=false;
  portEXIT_CRITICAL(&callbackMux);
  if(pending && done) {
    pending=false;
    Serial.printf("event=%lu attempt=%u link=%s (not application ACK)\n",(unsigned long)queue[head].sequence,attempts,
      status==ESP_NOW_SEND_SUCCESS?"SUCCESS":"FAIL");
    if(status==ESP_NOW_SEND_SUCCESS || attempts>=3) popEvent();
  }
  if(pending && now-lastSend>1000) {
    // Stop rather than overlap sends and misattribute a late callback.
    pending=false; radioReady=false;
    Serial.println("Send callback timeout: radio stopped; restart sender to recover.");
  }
  if(radioReady && count && !pending && now-lastSend>=SEND_GAP_MS) {
    ++attempts; lastSend=now;
    esp_err_t result=esp_now_send(targetMac,(uint8_t*)&queue[head],sizeof(CoinRadioPacket));
    if(result==ESP_OK) pending=true;
    else {
      Serial.printf("event=%lu attempt=%u send API=%s\n",(unsigned long)queue[head].sequence,attempts,esp_err_to_name(result));
      if(attempts>=3) popEvent();
    }
  }
  delay(1);
}

Remix this project

Make it yours in one click

Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.

Open in Schematik