Arduino Tinfoil Piano

Have you ever wanted to play a piano, but without any actual keys? In this project, I built a simple piano using capacitive touch sensors and an Arduino. Instead of pressing buttons, you just touch a wire or foil pad, and it plays a musical note!
This works using something called capacitive sensing, which detects changes in electrical charge when your finger gets close. It’s similar to how touchscreens work!
Supplies
For this project here are the materials you will need:
Making the Keys

To make the main keyboard, carefully cut out 7 pieces of tinfoil and cut out a piece of cardboard that could fit these strips of foil. (Making the keys too big may affect its sensitivity and not activate upon press)
IMPORTANT: Do not let the keys touch each other, short-curciting the keyboard
Assemble the Piano

To connect the keys - Use a male to female wire and rip off one end of the connection (the female), exposing about 1cm of the copper wires, spread out these thin strands of wires (shown above) to help ensure a better connection when taped to the foil.
Use some double-sided tape and secure the foil strips onto the cardboard, don't forget to sandwich the copper wires in between (shown above)
OPTIONAL* I made a box to hide the arduino and breadboard underneath but this is not required.
Assemble the Circuit



Assemble the circuit as shown in the image above.
You can change the sensitivity of the sensors by changing the resistor value:
- You can use a 1 megohm resistor for absolute touch to activate.
- With a 10 megohm resistor the sensor will start to respond 4-6 inches away.
- With a 40 megohm resistor the sensor will start to respond 12-24 inches away (dependent on the foil size).
More info about Capacitive Sensors here.
Note: The schematic doesnt include the connections from the resistors to the piano keys
Downloads
Code: Define Notes

We first include the CapacitiveSensor library. It lets us measure capacitance on a pin.
Next we define musical notes as frequencies in hertz (Hz).
- Each NOTE_ corresponds to a particular pitch — for example, NOTE_C4 is Middle C (about 262 Hz).
Code: Initializing Sensors

We create CapacitiveSensor objects for each touch sensor.
- The first number (2) is a “common” or “send” pin;
- The second (like 3, 4, 5...) is a “receive” pin.
- So we have 7 sensors, one for each note.
Code: Threshold and Notes

Threshold- If the capacitive reading is above 250, we consider it a “touch.” You might have to play around with this after while looking at the serial moniter and around what number does it reach and make sound.
We put all notes into an array notes.
- We put all sensor pointers into another array sensors.
- wasTouched lets us track whether each sensor was previously touching or not. This prevents repeated triggering while touching.
Code: Setup

Each capacitive sensor normally calibrates itself over time.That means it adjusts its baseline to match surroundings.
But we disabled this by setting set_CS_AutocaL_Millis(0xFFFFFFFF), which effectively tells it “never recalibrate.”
Why? If we kept automatic calibration on, touching the sensor for a long time might confuse it. It would think your touch is “normal",which we don’t want.
Code: Loop

The loop() function runs over and over, forever.
- Inside it, we use a for loop to check all 7 sensors.
- capacitiveSensor(30) measures capacitance 30 times and then adds them together. The result (sensor) is a number — a small number if you’re not touching it and a large number if you’re touching it.
- We then Serial.print(sensor) and a space.That lets us see the sensor’s values in Serial Monitor
If sensor > threshold and we hadn’t previously touched it (!wasTouched[i]), we:
- Start playing a note on pin 11 with tone(11, notes[i]).
- Set wasTouched[i] to true.
If sensor <= threshold and we previously were touching it (wasTouched[i] == true), we:
- Stop the note with noTone(11).
- Set wasTouched[i] back to false.
Serial.println() moves Serial Monitor output to a new line after we finished all 7 sensors.
- delay(50) waits 50 milliseconds before starting again.This lets the sensor stabilize and avoids crazy-fast looping.