Instructables
Picture of Capacitive-Touch Arduino Keyboard Piano
Using only an Arduino, a few resistors, a buzzer, and some bits of aluminum foil, you can create your own touch-sensor piano keyboard in just a few minutes! Using an Arduino Uno, you can create a piano with up to 18 keys; or with an Arduino Mega, you can get over 60 keys! Other Arduino boards should work with this project with little to no modification to the code.

 
 
Remove these adsRemove these ads by Signing Up

Step 1: Parts and Material

You'll first want to collect all of the parts you will need for the project:

• An Arduino (or Arduino-compatible) microcontroller board.
    • Any Arduino board should work: Uno, Leonardo, Mega, Pro Mini, etc.
• Eight 2.2 Megaohm (2.2 MΩ) resistors
    • Anywhere between 1 MΩ and 4.7 MΩ should work
    • You need one resistor per piano key
    • A 2.2 MΩ resistor has a color code of Red-Red-Green or Red-Red-Black-Yellow
• A piezo buzzer
• Some spare wires or jumper cables
• Aluminum foil
    • A foot or two should do
• Tape
• A surface to tape your keys to
    • We used a scrap piece of cardboard, but it can be anything you like, even the tabletop itself!

Equipment that you may need:

• Soldering iron and solder
    • You can probably get by without an iron by wrapping wires together instead of soldering, but the connection will not be as reliable
• Scissors to cut aluminum foil and tape
SimonR18 days ago

Hello! Thanks very much for taking the time to put this together.

Is there anything stopping me from programming this onto an ATmega328?? As far as I can see the Uno isn't doing anything that the mega couldn't, but would there be memory issues?

Thanks,

Simon

tylercrumpton (author)  SimonR16 days ago
Hey Simon!

There shouldn't be any problem using an Uno; it's what we used! You'll only need something with more inputs like the Mega if you need more pads.
Ahh sorry youve misunderstood, i was asking if i could use the microprocessor that forms the heart of the uno (the atmega328) in isolation, because I'd like to make several of these cheaply with some conductive ink, that stand alone with a couple of 3V coin cells. I think what Ive realised though is that I can make a battery powered uno board for pennies myself and use a breakout board to program it.
Thanks though!
karti3 months ago

hi, i tried your project, but there seems to be a delay from when I press the foil to when the sound comes. can someone please help me out.

happy,happy3 months ago

hey i am using arduino Uno

after writing code

i have uploaded the libarary

error is coming

CapSensePiano:36: error: 'CapacitiveSensor' does not name a type
CapSensePiano.ino: In function 'void setup()':
CapSensePiano:43: error: 'keys' was not declared in this scope
CapSensePiano.ino: In function 'void loop()':
CapSensePiano:54: error: 'keys' was not declared in this scope

happy,happy3 months ago

hey i am using arduino Uno

after writing code

i have uploaded the libarary

error is coming

CapSensePiano:36: error: 'CapacitiveSensor' does not name a type
CapSensePiano.ino: In function 'void setup()':
CapSensePiano:43: error: 'keys' was not declared in this scope
CapSensePiano.ino: In function 'void loop()':
CapSensePiano:54: error: 'keys' was not declared in this scope

happy,happy3 months ago

hey i am using arduino Uno

after writing code

i have uploaded the libarary

error is coming

CapSensePiano:36: error: 'CapacitiveSensor' does not name a type
CapSensePiano.ino: In function 'void setup()':
CapSensePiano:43: error: 'keys' was not declared in this scope
CapSensePiano.ino: In function 'void loop()':
CapSensePiano:54: error: 'keys' was not declared in this scope

Where is the .zip file with the code?

Thanks

RobertCraven4 months ago

im using a arduino mega and it says

CapacitiveSensorSketch:12: error: 'CapacitiveSensor' does not name a type

CapacitiveSensorSketch:13: error: 'CapacitiveSensor' does not name a type

CapacitiveSensorSketch:14: error: 'CapacitiveSensor' does not name a type

CapacitiveSensorSketch.pde: In function 'void setup()':

CapacitiveSensorSketch:18: error: 'cs_4_2' was not declared in this scope

CapacitiveSensorSketch.pde: In function 'void loop()':

CapacitiveSensorSketch:25: error: 'cs_4_2' was not declared in this scope

CapacitiveSensorSketch:26: error: 'cs_4_6' was not declared in this scope

CapacitiveSensorSketch:27: error: 'cs_4_8' was not declared in this scope

RobertCraven4 months ago

hello,

i have a very simple and dumb problem.

it says:

"Attached below, uou will find a zip file that contains the Arduino code for the Capacitive-Touch Arduino Keyboard Piano."

...i cant find the zipfile :/

jordanguy1237 months ago
Can you please tell me how to hook this up to an external powers (meaning no laptop involved for portability purposes)
tylercrumpton (author)  jordanguy1237 months ago

You've got a few options available that should work!

1) Search eBay/Amazon for "9v arduino" and find a battery clip that lets you hook up a 9V battery to power the Arduino through the barrel jack connect on the board, or

2) Use a rechargeable USB battery pack (can also be found on eBay/Amazon) to power the Arduino over the USB port.

Note: I've never tried to power this particular project off of battery power, so it's possible that the grounding may be a little strange for the sensor pads. If you try it out, please let us know how it goes!

Thanks for the great instructions - my son and I built this and it worked great right away using the code from mrizkall below - until we tried anything other than USB power. It didn't work at all on batteries or a 9V wall wart, or even a USB cable connected to a USB charger.

The fix I found was to rig a 9V connector so the negative lead splits off and connects to a good earth ground - like a copper pipe - while still going to the Arduino as usual.

Not the most portable solution, but it works for the installation we have in mind.

Side note... we experimented with 1M, 2M, and 10M resistors. We wanted it to work on touch contact, and the 1M worked fine for that. We also used copper tape for pads, and were able to solder wires directly to them for a solid connection.

Update! My 7 year old son just discovered that the ground wire doesn't need to be attached to ground. If you hold the exposed end in one hand, you can play the piano with the other and it works.

tylercrumpton (author)  jordanguy1237 months ago

You've got a few options available that should work!

1) Search eBay/Amazon for "9v arduino" and find a battery clip that lets you hook up a 9V battery to power the Arduino through the barrel jack connect on the board, or

2) Use a rechargeable USB battery pack (can also be found on eBay/Amazon) to power the Arduino over the USB port.

Note: I've never tried to power this particular project off of battery power, so it's possible that the grounding may be a little strange for the sensor pads. If you try it out, please let us know how it goes!

mrizkalla8 months ago

Thanks. Very cool. Here is how I changed the loop() function to make it so the sound stops playing when the finger has been lifted off the keys.

void loop() {

unsigned int keysPlaying = 0;

// Loop through each key:

for (int i = 0; i < NUM_OF_KEYS; ++i) {

// If the capacitance reading is greater than the threshold, play a note:

int c = keys[i].capacitiveSensor(NUM_OF_SAMPLES);

if(c > CAP_THRESHOLD) {

tone(BUZZER_PIN, notes[i]); // Plays the note corresponding to the key pressed

keysPlaying++;

}

}

if (keysPlaying == 0) {

noTone(BUZZER_PIN);

}

}

ccronkhite11 months ago
I'm also getting the error codes with the Uno. Mine are slightly different though.


sketch_nov14a:32: error: 'CapacitiveSensor' does not name a type
sketch_nov14a.ino: In function 'void setup()':
sketch_nov14a:39: error: 'keys' was not declared in this scope
sketch_nov14a.ino: In function 'void loop()':
sketch_nov14a:50: error: 'keys' was not declared in this scope
tylercrumpton (author)  ccronkhite9 months ago

Hello! Sorry for taking so long to get back to you; do you have the Capacitive Sense Arduino library properly installed? Make sure that this library is installed before trying to run the sketch.

edevane1 year ago
hi, thanks for this instructable! I'm trying to make something based on these instructions using 2.5m ohm (1+1.5m ohm resistors), copper pads about 2x2 inches and a capacitance threshold of anywhere up to 4000. The notes do play but it's outputting a constant tone or arpeggio of the scale when I don't touch anything. What is the range of the cap_threshold? Can you recommend any other alterations? I'm currently making the pads bigger by adding more copper tape to see if that works.

If not I'll try the WWC's suggestion about having a ground plane below the pads, although I've read contradictory information which specifically says to keep ground or other connections away from the cap sensors: http://ww1.microchip.com/downloads/en/AppNotes/01101a.pdf
gower edevane9 months ago

We are having the same problem. It continuously plays the last note touched.

tylercrumpton (author)  gower9 months ago
Hi! That sustained last note might have been caused by a line that was accidentally removed when I was fixing up the code for uploading. Can you try adding the following line to the beginning of the loop()?

noTone(BUZZER_PIN);

Let me know if that helps!

tylercrumpton (author)  edevane1 year ago
Hm, with those pad-sizes and resistor values, you shouldn't be having any issues with constant tones, especially when you crank the threshold that high. Is it just one note a just a few notes that seem to ring, or does it seems like every note is ringing? Here is some code to try that will display the values from the capacitance sensors in the Serial Monitor. What do you see when you run this code found in the pastebin link below?

http://pastebin.com/dP8AUPe7
omadison1 year ago
The code doesn't work. I have installed the library correctly (I think, because it shows on my libraries when the arduino program is opened) and I cannot get the code to verify without this error :
CapSensePiano:32: error: 'CapacitiveSensor' does not name a type
CapSensePiano.ino: In function 'void setup()':
CapSensePiano:37: error: 'keys' was not declared in this scope
CapSensePiano.ino: In function 'void loop()':
CapSensePiano:47: error: 'keys' was not declared in this scope

I really suck with code. That's why I use this site to make cool things without a lot of coding. Can you help me out?
tylercrumpton (author)  omadison1 year ago
Hello! Which Arduino are you using? It looks like there is a separate version of the library for the Arduino Due (which is included in the library .zip file).

Does the sample "CapacitiveSensorSketch" Arduino code that came with the library verify correctly for you? If not, then there may be something wrong with the way the library is set up on your computer.
i am using arduino uno but am experiencing the same problem as omadison
12
tylercrumpton (author)  amore29 months ago

Hi! Sorry for the delayed response; do you have the Capacitive Sense Arduino library properly installed? Make sure that this library is installed before trying to run the sketch.

Hi! Great project! This is my first time using capacitive touch keys and I didn't really understand how they are supposed to be made. The aluminum foil doesn't have to be folded, right?
I tried to tape them just like shown in the pics but they don't seem to work! Any idea why?? Thanks!
Make sure the code on your Arduino Uno/Duemilanove is properly uploaded, I personally found MANY errors within the programming!
akinich10 months ago
Hey this project is awesome
I seem to get confused a bit when you are describing the circuit , is it possible for you to put up a simple circuit diagram ?
Thanks
igneonumous11 months ago
what should i change from the sketch for use it on fl studio via serial midi converter?
Please, any idea? Thanks
omadison1 year ago
And of course right after I ask for help it verifies. lol. Thank you very much for replying so soon and sorry for the stupid question. I am a beginner at this stuff :D I'm interested a lot in making and building electrical devices but i can't stand programming which kinda sucks. But I will get it soon after practicing and mastering circuitry.
tylercrumpton (author)  omadison1 year ago
Awesome, I'm glad it works for you now! No question is a stupid question, especially if you're trying to learn. Feel free to ask for help if you get stuck on anything else!
WWC1 year ago
I have experimented with capacitive touch using the similar method. I had found that at least in my circuit, i needed to make another foil the same size as the key foil and place it under as a grounding plate. Separated by thin card stock.
Nice work.