Introduction: Quick Fruit Piano With MIDI
This is a really simple capacitive-touch piano. Tap on fruit, cans of soda, bottles of water, strips of aluminum foil, etc., and you get polyphonic piano music from your computer. Now that the software is written, the project shouldn't take more than 10 minutes to put together with the Arduino version.
- an Arduino (Mega for 8 keys, Uno for 6 keys) or a black pill STM32F103C8 board ($2 on Aliexpress) plus a UART-to-USB converter (e.g., an Arduino or a CH340)
- USB cable
- some aluminum foil
- test clips ideally, but jumpers and paperclips will do (idea for that from here)
- fruit, or playdough, or pieces of paper with areas colored with a pencil
No additional components needed, as this uses code from martin2250's brilliant ADCTouch library and the Arduino sends MIDI commands over serial to the computer. The Arduino's audio playback is either via an included python script on the computer or hairless-midiserial.
If you use the STM32F103C8 development board, then it's even nicer: the piano becomes a real USB MIDI controller, without the need for hairless-midiserial.
Note: To prevent damage to the Arduino/STM32F1 from static electricity, especially on a dry day or on carpet, I suggest not touching the aluminum foil or test clips when the device is operating. Instead, touch the fruit, playdough, etc., whose resistance should provide some ESD protection.
Step 1: Arduino Libraries
In the Arduino IDE, choose Sketch | Include Library | Library Manager. Search for my ADCTouchSensor. Install the library.
Then choose File | Examples | ADCTouchSensor | CapacitivePiano.
If you have an Arduino, plug your Arduino into your computer's USB port and upload the CapacitivePiano sketch to your Uno or Mega. The sketch will send data to the computer via USB serial as to which of the 8 (Mega) or 6 (Uno) capacitive sensors was triggered.
If you have an STM32F103C8, first install the bootloader and set up the Arduino IDE for it (with my branch of the addMidiHID branch) by following the first three steps here. Then go back to the Library Manager, and search for my USBHID_stm32f1 library. Install it.
Step 2: Attach Leads to Piano Keys
Run wires from the A0-A7 (A0-A5 on Uno) pins on your Arduino or STM32F103C8 to whatever you want to function as piano keys. For instance, I used jumpers with alligator clips or test clips going to pieces of aluminum foil on each of which I put either a can of soda or an orange as the key. The aluminum foil can be used as a key directly. Or one can stick a wire into a piece of fruit or vegetable or playdough.
Step 3: Install Software: Option A: Arduino and Hairless MIDI to Serial Bridge
You can download and install the Hairless MIDI to Serial Bridge (Win/OSX/Linux) to link the Arduino's USB-serial output with your computer's synthesizer software.
If you have Windows 7 or newer, you can use the built-in Microsoft Wavetable GS Synth, so all you need to install is Hairless. Some other playback software will require loopMIDI.
On other systems, you may need a MIDI synthesizer like VirtualMidiSynth or Garageband. You can also use VirtualMidiSynth on Windows if you like.
Step 4: Install Software: Option B: Arduino and Python
You can also just use a simple included Python script that plays back MIDI notes.
Make sure you have Python installed on your computer. Either 2.7 or 3.x will do.
Make sure you have the pygame Python package installed. If not, run this from the commandline:
python -m pip install pygame
Find out what address your Arduino's serial port is. Your Arduino IDE will have the serial ports listed under Tools | Port.
In the CapacitivePiano package that you downloaded, you will find the music.py script that is the PC side of the project.
Step 5: Install Software: Option C: STM32F103C and MIDI Synthesizer Software
If you have the STM32F103C, you can use any software that works with a MIDI keyboard controller.
On a computer, I use the Virtual MIDI Piano Keyboard (VMPK). With the STM32F103C plugged in, choose Edit | MIDI Connections | Input MIDI Connection, and then choose Maple MIDI or Diro Synth.
Step 6: Play!
Plug the Arduino with the CapacitivePiano sketch into the computer. Do not touch any of the "buttons" until only the power LED is run, to allow the capacitive buttons to be calibrated.
For the Hairless option, run the Hairless MIDI bridge, choose the serial port from the dropdown list. Then choose a synthesizer. On Windows, if you choose Microsoft Wavetable GS Synth, everything should just work.
If you want to use the Python script instead, make sure you are in the directory where you have piano.py, and run:
python piano.py serialport
where serialport is the serial port from Arduino IDE (e.g., COMx on Windows).
If you use the STM32F103C8, then use your favorite MIDI synthesizer software on a computer or mobile device.
Now press your "buttons" and have fun!
Educational ideas for kids to experiment with:
- Experiment with different items on the aluminum foil and see which ones work and which one's don't--and then talk about why these work and others don't.
- Playdough works really well (and then you don't even need foil and clips--you can just stick wires into the playdough, though admittedly it looks like a bomb!).
- You can even take a piece of paper and shade in areas with a pencil (it doesn't have to super dark, but cross-hatching is good) and clip those in.
- You can attach an alligator clip to a really large metal item, like the rim of whiteboard, and make giant keys.
- An experiment to try is to have a smaller person hold an alligator clip attached to one of the wires, and then have a larger person shake hands with or high-five the smaller person, and see if that registers. Then try again with a larger person holding it and a smaller one touching them.
Just make sure that after each change of what is attached to the Arduino, you reset the Arduino, either by pressing the "reset" button or by replugging the USB connection (and then re-run the python code, as it will probably crash), in order to recalibrate the sensors.
One can discuss electrical capacity and capacitive sensing.