ESP32 touch-pad piano
An ESP32 plays notes on a passive buzzer when you touch foil pads or bare wires, using the touch inputs built into the chip.
Build it step by step
0 of 6 doneWire the pads
GPIO4, 13, 14 and 27 for notes, GPIO33 for the octave.
Wire the buzzer
GPIO25 through 330 ohms, minus to GND.
Upload the sketch
Keep your hands away for the first second.
Open the serial monitor
115200 baud shows the idle values.
Touch a pad
Its note plays.
Touch the octave pad as well
The note is an octave higher.
How do you build an ESP32 touch-pad piano?
An ESP32 plays notes on a passive buzzer when you touch foil pads or bare wires, using the touch inputs built into the chip. You need ESP32 DevKit V1, Passive buzzer, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 6 steps and about 130 minutes.
- Level
- Intermediate
- Time
- About 130 min
- Parts
- 4
Parts list
- Development BoardsBuyESP32 DevKit V1 × 1
- Motors & ActuatorsBuyPassive buzzer × 1
- PassivesBuyResistor 330 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Pad C wire | ESP32 GPIO4 (touch 0) | Note C |
| Pad D wire | ESP32 GPIO13 (touch 4) | Note D |
| Pad E wire | ESP32 GPIO14 (touch 6) | Note E |
| Pad G wire | ESP32 GPIO27 (touch 7) | Note G |
| Octave pad wire | ESP32 GPIO33 (touch 8) | Raises the pitch by an octave while touched |
| Passive buzzer plus | 330 ohm resistor to ESP32 GPIO25 | Tone output |
| Passive buzzer minus | ESP32 GND | Return |
Code
The original ESP32 has ten pins that can sense touch. Each one measures how long a tiny charge takes to move on a metal pad, and a finger adds capacitance that changes the number, so no button or switch is needed, only a bare wire or a piece of foil. On this chip the number gets smaller when the pad is touched. The program first learns the idle value of each pad while you keep your hands away, then treats a drop below 60 percent as a touch. Each pad maps to a note, and the octave pad doubles the frequency, which raises the pitch by one octave. A passive buzzer has no tone circuit of its own, so the chip makes the tone with its LEDC PWM output at the note frequency. The code has two small branches because the board package changed its PWM calls between versions 2 and 3. Keep the volume modest and the buzzer away from your ear.
Runs on a virtual ESP32 Dev Module 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).
// For the original ESP32 (touch values fall when touched). Works with ESP32 Arduino core 2.x and 3.x.
const int PADS[5] = {4, 13, 14, 27, 33}; // C, D, E, G and the octave pad
const int NOTES[4] = {262, 294, 330, 392}; // C4, D4, E4, G4 in hertz
const int BUZZER = 25;
int idle[5];
void toneOn(int freq) {
#if ESP_ARDUINO_VERSION_MAJOR >= 3
ledcAttach(BUZZER, 2000, 8);
ledcWriteTone(BUZZER, freq);
#else
ledcSetup(0, 2000, 8);
ledcAttachPin(BUZZER, 0);
ledcWriteTone(0, freq);
#endif
}
void toneOff() {
#if ESP_ARDUINO_VERSION_MAJOR >= 3
ledcWriteTone(BUZZER, 0);
#else
ledcWriteTone(0, 0);
#endif
}
void setup() {
Serial.begin(115200);
delay(500); // do not touch the pads while it learns the idle values
for (int i = 0; i < 5; i++) {
long sum = 0;
for (int k = 0; k < 20; k++) { sum += touchRead(PADS[i]); delay(5); }
idle[i] = sum / 20;
Serial.print("Pad "); Serial.print(i); Serial.print(" idle "); Serial.println(idle[i]);
}
}
void loop() {
int note = -1;
for (int i = 0; i < 4; i++) {
if (touchRead(PADS[i]) < idle[i] * 6 / 10) { note = i; break; } // touched: value drops
}
bool octave = touchRead(PADS[4]) < idle[4] * 6 / 10;
if (note >= 0) {
toneOn(octave ? NOTES[note] * 2 : NOTES[note]);
} else {
toneOff();
}
delay(10);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Pad 0 idle 70 Pad 1 idle 70 Pad 2 idle 71 Pad 3 idle 71 Pad 4 idle 70
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| No touch | Silence |
| Touch pad C | Plays C |
| Touch pad G | Plays G |
| Touch octave and a note | Same note, higher |
| Idle values | Printed at start-up |
If it does not work
Sounds on its own
Keep the wires short and away from mains cables and hands at start-up.
Never plays
Check the pad GPIO numbers and that the board is the original ESP32.
Wrong notes
Check which pad is on which pin.
No sound
Check the buzzer is passive and wired to GPIO25.
Compile error on ledcAttach
Update or match the ESP32 board package.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the ESP32 touch-pad piano?
ESP32 DevKit V1, Passive buzzer, Resistor 330 Ω and Breadboard 830 Tie Points.
How long does the ESP32 touch-pad piano take to build?
About 130 minutes for a intermediate maker, in 6 steps.
How do I know the ESP32 touch-pad piano works?
No touch: Silence. Touch pad C: Plays C. Touch pad G: Plays G.
Sounds on its own: what should I check?
Keep the wires short and away from mains cables and hands at start-up.
Never plays: what should I check?
Check the pad GPIO numbers and that the board is the original ESP32.
Wrong notes: what should I check?
Check which pad is on which pin.
No sound: what should I check?
Check the buzzer is passive and wired to GPIO25.
