Introduction: 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.

 

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

Step 2: The Idea and Design

The basic principle behind our method of touch sensing is that each piano "key" is sensor that can measure the electrical capacitance of a person's body. The measurement will change as the user gets closer to the sensor and will spike dramatically when the user touches the sensor. The Arduino will be looking for these spikes in order to detect when the user has touched the key.

We will need to use one pin an the Arduino for each key as an "input pin", and we'll need one pin total to be used as the "common send" pin. We will also need one pin for the buzzer that will be used to generate a tone. This means that since an Arduino one has 20 input pins (14 digital and 6 analog), we can have up to 18 keys on our keyboard! If you have an Arduino Mega or any other board with more pins, that number can be even higher!

Step 3: Create Your "Keys"

Out of your aluminum foil, cut a few shapes to be your piano "keys". Any shape will do, but we took the simple route and made squares. Each key should be somewhere between 2"x2" and 4"x4", but most other sizes will work as well.

Step 4: Assemble the Key Circuitry

Each touch-sensitive "key" on the piano is going to require one resistor and two wires/cables. Solder one wire to each leg of the resistor, but make sure to leave part of the leg sticking out. The leg will be used to connect to the foil in the next step. Make as many of these as you plan on having piano keys.

Step 5: Connect the Foil "Key"

Remember the extra bit of leg we saved? Take it and poke it through a corner or side of one of your aluminum foil squares. Fold the leg over the edge back towards itself to make the connect hold more strongly. Go ahead and assemble all of your keys.

Step 6: Attach the Keys

Using your tape, attach each key to your surface, making sure to leave some of the foil exposed. It's okay to tape over the resistor, but be sure to remember which wire is attached to the side of the resistor that is touching the foil and which wire is not.

Step 7: Finish the Wiring

After attaching all of your keys to the surface, take the wire that is connected to the resistor (but not on the side connected to the foil) and connect to the the same side of the resistor on the adjacent key. Basically, we want to connect all of the non-foil-sides of the resistors together. These can be soldered are just connected with tape.

You will have one wire left over that is not connected to anything; this will be our "common send" wire.

Step 8: Hook Up the Arduino

Connect the "common send" wire left over from the previous step to Pin 2 on the Arduino. Then, connect the "key" wires to Pins 3-10, starting with the left-most key on Pin 2 and ending with the right-most key on Pin 10.

Take your buzzer and connect the pin marked negative (-) to a ground (GND) pin on the Arduino. Connect the pin marked positive (+) to Pin A4 on the Arduino. (NOTE: Depending on your buzzer, you may need connect a resistor between the A4 Pin and the positive side of the buzzer. This is to prevent the buzzer from drawing too much current from the Arduino pin and possibly damaging it. A 220 Ohm resistor or greater should work fine for most buzzers.)

Step 9: The Code

Before you can start playing your newly-built piano, you will need to obtain and install the CapSense Arduino library if it is not already installed. This can be downloaded from Github here. If you do not know how to install third-party Arduino libraries in you version of the Arduino IDE, reference this guide on Arduino.cc.

Attached below, uou will find a zip file that contains the Arduino code for the Capacitive-Touch Arduino Keyboard Piano. Download it and unzip it somewhere on your computer. Open CapSensePiano.ino in the Arduino IDE and upload the code to your Arduino.

Step 10: Play!

And that's it! You should now be able to tap on the foil keys and hear the corresponding notes played through the buzzer. If the keys are not very responsive, or are trigger without being touch, you can adjust the CAP_THRESHOLD value in the Arduino sketch to set what value that the keys are triggered at. Lower values will cause the keys to be easier to trigger, and higher values will reduce the number of keys that are mistakenly triggered.

You can also change the scale that is played by uncommenting one of the few scales included, or make your own scale! If you make your own piano, please comment and show us some pictures and videos. We'd love to see some creative instruments!

Thanks for reading!

Credit for this project goes to Nicholas Jones and Tyler Crumpton.

Comments

author
megantissington (author)2015-03-06

Hi there,

I made this the other day but I used a breadboard instead of directly wiring the pieces to each other. I thought I should upload a diagram of the wiring I used just in case anybody else wants to make this using a breadboard. I also made some adjustments in the code to work for the amount and size of keys I had (about 1" wide by 2" long), including mrizkalla's adjustments to stop the loop effect. I've uploaded it to my Google Drive and it can be found by clicking here: https://drive.google.com/folderview?id=0B53IAtyS3-7NbF9jTmFMd1U4eU0&usp=sharing

Wiring Diagram-01.jpg
author
Naz C (author)megantissington2017-03-27

Hey there! I made a keyboard piano today using your schematic. It worked amazingly! Thank you =)

author

Wonderful, thanks, Megan! I would love to see some pictures of the project!

author

Hi Tyler,

Here are some pics from when I did the project and a video of me playing Ode To Joy on it... worked pretty well!

Thanks for the comment!

_DSC0138.JPG_DSC0139.JPG_DSC0140.MOV
author
gjuyal (author)megantissington2016-03-17

Where can I see the video of this working circuit . I am having a little problem. Cant hear the change in notes.??

author

That's awesome! Thanks!

author
sososolame (author)2016-05-26

Our project almost worked! But, the buzzer won't stop playing until next key is pressed or unplugging Arduino. We tried to add a ELSE statement, to add delay, to remove for loop but none of these methods worked. Do you have any advice? We assemble circuits exactly like yours and used your code.

author
pgr23 (author)sososolame2016-06-08

Tyler, i have the same problem, please tell us how to repair it

author
nishthi (author)pgr232017-08-11

me 2

author
FernandoS116 (author)sososolame2016-06-16

You must add the time 250 in line "

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

"

author
TimmdB (author)2016-11-01

Hello,

I'm trying to rebuild this for a school project, however I've encountered a problem. I hae completely recreated the circuit and used the same code but somehow when the buzzer doesn't stop making a sound. When I touch a different key it will change the tone but when I release the key it will keep on going in that tone. Please can someone help me!

Tim

author
Maik 234 (author)TimmdB2017-01-31

You need to add a notone line

author
nishthi (author)Maik 2342017-08-11

where do i add the no tone line

author
nishthi (author)Maik 2342017-08-11

thanx..that helped

author
nishthi (author)2017-08-11

it's an amazing project.. got it all right ...just exchanged the buzzer to a speaker..clearer and better sound..and it was not working for me and I switched over the buzzer connections..attach the positive to GND n negative to A4...and it works perfectly.

all I didn't get is how it works...like how is the sound produced when I touch the key...

can someone help me I need to explain my project..

author
Lokesh kumar08 (author)2017-01-26

I have some problems in making the key can anyone help me out in making one

author
dsaywers made it! (author)2016-10-23

Hello,

Similar to megantissington, I used a breadboard, instead of directly wiring the pieces to each other. Instead of using the aluminum foil, or other metallic keys, I used apples and potatoes as my keys, and just inserted wires into them. I switched out the resistors to 20 MegaOhms, and had to turn up the capacitance threshold to 10000, but it works surprisingly well! I attached a picture of my setup as well as a video of it working.

WIN_20161023_14_04_17_Pro.jpgWIN_20161023_13_51_03_Pro.mp4
author
circumlocutus (author)2016-08-06

i had a bunch of students make this as a crash course in soldering and programming, it was a hit.

author
NishthaY (author)2016-06-22

Please tell me how you connected the buzzer.
I made the same as you did but the buzzer is continuously making sound.

author
pacotec made it! (author)2016-05-13

It works well, it was easy an funny.

IMG_20160513_100622.jpg
author
Baibhavs2 (author)2016-03-23

IT SHOWS FOLLOWING ERROR MESSAGE WHENEVER I UPLOAD IT...PLZZ HELP

Arduino: 1.6.8 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\Admin\AppData\Local\Temp\Rar$DIa0.942\CapSensePiano\CapSensePiano.ino:14:30: fatal error: CapacitiveSensor.h: No such file or directory

#include <CapacitiveSensor.h>

^

compilation terminated.

exit status 1

Error compiling for board Arduino/Genuino Uno.

This report would have more information with

"Show verbose output during compilation"

option enabled in File -> Preferences.

CaptureIUTUF.PNG
author
gjuyal (author)2016-03-17

I need some help.

1. My keyboard is playing only 1 note. I cant make out the difference.

2. How can I print the capacitance reading onto serial monitor so I know what values the arduino is picking and calculating.

3. What do the Number of samples mean.

I am using only 5 keys.

author
GrayF3 (author)2016-01-13

Im having a problem with the library for this project it keeps telling me that the "author" is missing inside the library i have gone to the location of the problem but i still don't know whats wrong i have also tried to add the author myself but it wont accept it i need help can anyone answer my question?

author
Like10daleks (author)2015-12-04

Hey! This is my first project on the arduino and i know nothing about coding, when i try to upload the sketch there is an error. i have the library downloaded. This is the error code:

Arduino: 1.6.6 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\Tardis2\AppData\Local\Temp\arduino_a6247a94239e09e2459cc5b5f1317b51\CapSensePiano.ino:17:21: fatal error: pitches.h: No such file or directory

#include "pitches.h"

^

compilation terminated.

exit status 1

Error compiling.

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences.

author
táron1 (author)Like10daleks2015-12-31

Hi!
When you are installing the code u have to (in the beginning)press and hold down the reset button until u see the less blinking on the arduino.
I hope that helped ;)

author
rokirunstar (author)2015-11-10

can any other sheet can be replaced for aluminium foil

author
MohammadG2 (author)2015-10-18

برنامه درست شد

author
MohammadG2 (author)2015-10-18

؟برنامه خطا می دهد . چه کار کنم

author
Ardutronico (author)2015-08-11

It's wrong that you can only get 18 on an Arduino Uno or 60 on Mega, because you can use shift registers like 74HC595. This one take 3 pins and give 8, but there is an instructable for giving 16 of 3 pins (https://www.instructables.com/id/Multiplexing-with-Arduino-and-the-74HC595)

author

Unfortunately, using a shift register to expand the pin count won't work here, since this project requires inputs rather than outputs. That "Multiplexing with Arduino" Instructable is focused on supporting an array of LEDs, rather than buttons.

It might be possible to do a similar "scanning" process for the piano that normal button matrices use, but that is another, more complicated project.

author

OK, that's true, they're inputs, but there also are shift registers for inputs. Take by example the CD4021B. What about this?

author
ChristineK2 (author)2015-06-12

when I tried to upload the code, it said :

avrdude: stk500_getsync(): not in sync: resp=0x00

can someone help?

author
JatinC1 (author)ChristineK22015-09-07

Try a different port like if it is COM1 then try COM3 or any other port

author
Atul N Yadav made it! (author)2015-07-06

I made Arduino Piano with 4 Keys....may be I have used 1 m ohm resistor that would have caused it not to play good, but results are encouraging.

Need to know how I can change the length of note to be played in program ?

It would be great help :)

IMG_0109.JPG
author

Awesome make! You can make the tones only play for a certain length of time by adding a timeout to the tone() function. For example, change this:

tone(BUZZER_PIN, notes[i]);

to this:

tone(BUZZER_PIN, notes[i], 500);

That will make the tone play for half a second (500 milliseconds) before cutting off.

Hope that helps!

author

Great, thanks mate !!!

author
KhoJ (author)2015-05-21

Hi! I have no idea why the note D4 can't be played properly when I touch it. It played a weird sound. The note B4 also cant' be played at all. Any idea how to fix this?

Thanks!

author
SarahG3 (author)2015-05-11

I'm new to programming, so I have a question regarding the code. If I plan on using momentary switches for the keys instead of aluminum foil, would you happen to know how the code should change for this? I feel like it might be simpler, because I don't think I would need the capacitive sensor with a switch like that. I don't really know hahaha Please help if you can! :-)

author
JeremyC6 made it! (author)2015-04-25

This is my first arduino project. Turned out great. I followed megantissington's breadboard diagram and used the code that she uploaded to google drive. The only change I made was adding C5 to complete the octave.

Here's a video:

https://www.youtube.com/watch?v=iNsObVeigsw

20150425_173526.jpg
author
LindsayK1 (author)2015-04-13

Has anyone tried dripping water on the touchpads. Do they respond the same to water as the do to a finger?

author
souvik.sarkar.56 (author)2015-04-05

Hi there,

Thank you for this beautiful update.

I made this up with little modification. Instead of buzzer I used LEDs, Means when I press a key corresponding LED glows.

I have set CAP_THRESHOLD to correct value by try-n-error method.

But I came across one strange issue.

1. When I connect Arduino UNO only to PC with USB cable it works absolutely fine.

2. When i connect both USB and POWER cable(12v, 1A) to Arduino, these LED keep blinking when I pressthe keys.

3. After programming the Arduino, I disconnect the USB cable , and connect the POWER cable, these does not works. While pressed keys, sometime LED blinks.

Now I am not getting any clue about this :( . Please update if you know any possible reason for this.

I will be updating my project details and PIC soon.

Thanks

Souvik

author
jrtumbles (author)2015-01-29

https://learn.sparkfun.com/tutorials/installing-an...

Try this tutorial for installing the libraries. It is much more clear than the arduino site explanation and code worked immediately after installing the libraries properly.

john

author
jrtumbles (author)2015-01-28

Hi Tyler,

I hate to bother you with this especially because you have addressed it so many times before. I am getting the following errors each time I try to compile the library and or sketches.

I have followed the arduino tutorial and tried following the instructions here, but to no avail. Can you help me get this code loaded and working? I am planning to teach this to my grandson as soon as I can get it working right. I absolutely love your projects and the fact that you are helping so many people is just amazingly generous and kind. I would greatly appreciate it if you could kind of walk me through the process and get this working.

Thank you,

John

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

author
lucia1001 (author)2015-01-14

Hi, just a quick question. When i press a key, the sound plays perfectly fine, and does what it is supposed to do. However whenever i let go of the key, it keeps playing the same note even though i am not touching the key. Is this supposed to happen? If it isn't, how would i fix this?

author
omadison (author)2013-08-05

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?

author
tylercrumpton (author)omadison2013-08-05

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.

author
amore2 (author)tylercrumpton2013-09-30

i am using arduino uno but am experiencing the same problem as omadison
12

author
tylercrumpton (author)amore22014-01-19

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.

author

sorry i want to ask you

both program and Material are right but when i tough tHe Aluminum foil

it don`t have sound
this is the program
/*

Capacitive-Touch Arduino Keyboard Piano

Plays piano tones through a buzzer when the user taps touch-sensitive piano "keys"

Created 18 May 2013

Modified 23 May 2013

by Tyler Crumpton and Nicholas Jones

This code is released to the public domain. For information about the circuit,

visit the Instructable tutorial at https://www.instructables.com/id/Capacitive-Touch-...

*/

#include <CapacitiveSensor.h>

#include "pitches.h"

#define COMMON_PIN 2 // The common 'send' pin for all keys

#define BUZZER_PIN A4 // The output pin for the piezo buzzer

#define NUM_OF_SAMPLES 10 // Higher number whens more delay but more consistent readings

#define CAP_THRESHOLD 150 // Capactive reading that triggers a note (adjust to fit your needs)

#define NUM_OF_KEYS 8 // Number of keys that are on the keyboard

// This macro creates a capacitance "key" sensor object for each key on the piano keyboard:

#define CS(Y) CapacitiveSensor(2, Y)

// Each key corresponds to a note, which are defined here. Uncomment the scale that you want to use:

int notes[]={NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5}; // C-Major scale

//int notes[]={NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5}; // A-Minor scale

//int notes[]={NOTE_C4,NOTE_DS4,NOTE_F4,NOTE_FS4,NOTE_G4,NOTE_AS4,NOTE_C5,NOTE_DS5}; // C Blues scale

// Defines the pins that the keys are connected to:

CapacitiveSensor keys[] = {CS(3), CS(4), CS(5), CS(6), CS(7), CS(8), CS(9), CS(10)};

void setup() {

// Turn off autocalibrate on all channels:

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

keys[i].set_CS_AutocaL_Millis(0xFFFFFFFF);

}

// Set the buzzer as an output:

pinMode(BUZZER_PIN, OUTPUT);

}

void loop() {

// Loop through each key:

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

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

if(keys[i].capacitiveSensor(NUM_OF_SAMPLES) > CAP_THRESHOLD) {

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

}

}

}

author

sorry i want to ask you

both program and Material are right but when i tough tHe Aluminum foil

it don`t have sound
this is the program
/*

Capacitive-Touch Arduino Keyboard Piano

Plays piano tones through a buzzer when the user taps touch-sensitive piano "keys"

Created 18 May 2013

Modified 23 May 2013

by Tyler Crumpton and Nicholas Jones

This code is released to the public domain. For information about the circuit,

visit the Instructable tutorial at https://www.instructables.com/id/Capacitive-Touch-...

*/

#include <CapacitiveSensor.h>

#include "pitches.h"

#define COMMON_PIN 2 // The common 'send' pin for all keys

#define BUZZER_PIN A4 // The output pin for the piezo buzzer

#define NUM_OF_SAMPLES 10 // Higher number whens more delay but more consistent readings

#define CAP_THRESHOLD 150 // Capactive reading that triggers a note (adjust to fit your needs)

#define NUM_OF_KEYS 8 // Number of keys that are on the keyboard

// This macro creates a capacitance "key" sensor object for each key on the piano keyboard:

#define CS(Y) CapacitiveSensor(2, Y)

// Each key corresponds to a note, which are defined here. Uncomment the scale that you want to use:

int notes[]={NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5}; // C-Major scale

//int notes[]={NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5}; // A-Minor scale

//int notes[]={NOTE_C4,NOTE_DS4,NOTE_F4,NOTE_FS4,NOTE_G4,NOTE_AS4,NOTE_C5,NOTE_DS5}; // C Blues scale

// Defines the pins that the keys are connected to:

CapacitiveSensor keys[] = {CS(3), CS(4), CS(5), CS(6), CS(7), CS(8), CS(9), CS(10)};

void setup() {

// Turn off autocalibrate on all channels:

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

keys[i].set_CS_AutocaL_Millis(0xFFFFFFFF);

}

// Set the buzzer as an output:

pinMode(BUZZER_PIN, OUTPUT);

}

void loop() {

// Loop through each key:

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

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

if(keys[i].capacitiveSensor(NUM_OF_SAMPLES) > CAP_THRESHOLD) {

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

}

}

}

About This Instructable

198,658views

414favorites

License:

More by tylercrumpton:Built-in Vertical/Horizontal Remote StandDesign and Print a Tripod-Mountable Smartphone CaseCapacitive-Touch Arduino Keyboard Piano
Add instructable to: