Arduino Tinfoil Piano

by tanyapurba8 in Circuits > Arduino

19 Views, 0 Favorites, 0 Comments

Arduino Tinfoil Piano

Screenshot 2025-06-17 224803.png

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

Making the Keys

IMG_8111.jpeg

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

IMG_8196.jpeg

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

IMG_8180.jpeg
Screenshot 2025-06-17 225512.png
Screenshot 2025-06-17 230113.png

Assemble the circuit as shown in the image above.

You can change the sensitivity of the sensors by changing the resistor value:

  1. You can use a 1 megohm resistor for absolute touch to activate.
  2. With a 10 megohm resistor the sensor will start to respond 4-6 inches away.
  3. 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

Screenshot 2025-06-16 185638.png

We first include the CapacitiveSensor library. It lets us measure capacitance on a pin.

Next we define musical notes as frequencies in hertz (Hz).

  1. Each NOTE_ corresponds to a particular pitch — for example, NOTE_C4 is Middle C (about 262 Hz).

Code: Initializing Sensors

Screenshot 2025-06-16 190035.png

We create CapacitiveSensor objects for each touch sensor.

  1. The first number (2) is a “common” or “send” pin;
  2. The second (like 3, 4, 5...) is a “receive” pin.
  3. So we have 7 sensors, one for each note.

Code: Threshold and Notes

Screenshot 2025-06-16 190644.png

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.

  1. We put all sensor pointers into another array sensors.
  2. wasTouched lets us track whether each sensor was previously touching or not. This prevents repeated triggering while touching.

Code: Setup

Screenshot 2025-06-16 191642.png

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

Screenshot 2025-06-16 192346.png

The loop() function runs over and over, forever.

  1. Inside it, we use a for loop to check all 7 sensors.
  2. 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.
  3. 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:

  1. Start playing a note on pin 11 with tone(11, notes[i]).
  2. Set wasTouched[i] to true.

If sensor <= threshold and we previously were touching it (wasTouched[i] == true), we:

  1. Stop the note with noTone(11).
  2. Set wasTouched[i] back to false.

Serial.println() moves Serial Monitor output to a new line after we finished all 7 sensors.

  1. delay(50) waits 50 milliseconds before starting again.This lets the sensor stabilize and avoids crazy-fast looping.

Upload the Code + Demo