This project turns an ESP32 into an interactive "piano key". It plays a sound when someone steps in front of a distance sensor (VL53L0X).
There are two versions included in this project:
- Simple Version: Hardcoded single sound, no WiFi.
- Advanced Version: Web interface for configuration, multiple sounds (C, D, E...), WiFi AP with Captive Portal.
This project uses two separate environments. Choose the one you want to upload in PlatformIO:
- File:
src/main_simple.cpp - Features:
- Plays
/piano.wavfrom LittleFS. - Config (Trigger distance, Volume) is hardcoded in
main_simple.cpp. - No WiFi, instant start.
- Plays
- File:
src/main.cpp - Features:
- WiFi Access Point: Creates "Piano-Config" (Pass:
Piano1234) for 5 minutes after boot. - Captive Portal: Automatically opens settings page on phone connection.
- Web Interface: Set Trigger Distance, Volume, and upload new sounds via browser.
- Multi-Sound: Upload sounds for notes C, D, E, F, G, A, H and select the active one.
- Persistence: Settings are saved permanently (even after reboot).
- WiFi Access Point: Creates "Piano-Config" (Pass:
To upload:
- In VS Code (PlatformIO): Project Tasks -> env:main (or main_easy) -> Upload.
- Don't forget: Upload Filesystem Image first!
- ESP32 DevKit V1
- GY-53 (VL53L0X) Time-of-Flight Sensor
- Speaker (8 Ohm / ?? W)
- Amplifier: PAM8403 Module (Recommended) OR Transistor (BC546B)
| Sensor | ESP32 |
|---|---|
| VCC | 3V3 |
| GND | GND |
| SDA | GPIO 21 |
| SCL | GPIO 22 |
Using a PAM8403 amplifier eliminates "popping/clicking" noise and is much louder. ESP32 VIN (5V) -----> PAM8403 VCC ESP32 GND -----> PAM8403 GND ESP32 GPIO25 -----> PAM8403 L_IN PAM8403 L_OUT -----> Speaker
Note: This circuit is quiet and may cause clicking sounds on start/stop. ESP32 VIN (5V) -----> Speaker (+) Speaker (-) -----> BC546B Collector ESP32 GND -----> BC546B Emitter ESP32 GPIO25 --[1kΞ©]-- BC546B Base
- Format: WAV, Mono, 16-bit PCM, 22050 Hz (or 16000 Hz). (can be converted using https://audio.online-convert.com/convert-to-wav)
- Location: Put default files in
data/folder (e.g.,C.wav,piano.wav). - Optimization: Add a short Fade-In (10ms) and Fade-Out (10ms) to your WAV files to prevent clicking noises.
βββ platformio.ini # Configuration for env:main and env:main_easy βββ data/ # WAV files (Upload this via "Upload Filesystem Image") β βββ piano.wav # Default for simple version β βββ C.wav # Default note for advanced version βββ src/ βββ main.cpp # Code for Advanced Version βββ main_simple.cpp # Code for Simple Version