This post is part of a series of notes and exercises for a summer camp on making musical instruments with Arduino and Pure Data.
It’s time to assemble our instrument that plays chords. Let’s call it the chorder! What hardware shall we use for input? We’ve already used buttons, and we’ve already used potentiometers. We haven’t used fruits and vegetables yet, so let’s give them a turn.
You wouldn’t normally think of a tomato as an electronic component. But a tomato can act as a capacitor, a component in an electric circuit that can store up electricity and release it later. Most other components pass their electrons through immediately, but the capacitor is different. It hoards. It banks. It capacitates.
When a human finger touches a tomato-capacitor, the electricity stored within in it discharges along the circuit. We’ll exploit this phenomenon in the hardware for our chorder. Don’t worry. The electricity doesn’t actually pass through your hand.
The baseline chorder will play the seven major chords of a scale, so we will need seven capacitors. To plug all of these into our circuit, we’ll use a 12-touch capacitance sensor—an MPR121 from Adafruit. The hardware looks like this:
Go ahead and assemble your chorder. The voltage and ground connections behave the same way as they did in our other instruments. Pins 0 and beyond on the capacitance sensor will connect to our produce by way of an alligator clip. There are only six in the diagram, because there were only six different colors of clips available in the circuit drawing tool. Feel free to use seven in your actual circuit.
The wires going from SCL to A5 and from SDA to A4 are new. This sensor uses a standard electronic protocol called I2C. This protocol describes how a master device can control a bunch of slave devices with just two wires, which have the following roles:
Arduino “speaks” the I2C protocol on pins A4 and A5.
We will test our hardware with the smallest program we can write that shows the sensor is working. To handle the business of talking to the capacitor, we’ll use a helper library from Adafruit. Follow these steps to get it installed:
Now create a new Arduino sketch. Insert this line at the top of your file to make the Adafruit code available:
setup, declare and initialize a global variable that models the capacitor. Its type comes from the helper library:
Adafruit_MPR121 sensor = Adafruit_MPR121();
setup, start up the Serial port just like we’ve done in the past. But we also want to initialize the sensor with this line:
0x5A is a base-16 number that identifies the slave device. The master uses this identifier to direct messages to the right I2C device. If we had a second capacitance sensor in our circuit, we’d need to pass a different identifier. Otherwise they’d get each other’s mail.
loop, we can get an integer that tells us the state of all 12 capacitors using the
touched function. It returns an 16-bit integer (a
short) that we can print:
short bits = sensor.touched(); Serial.println(bits);
Upload your program, open the Serial Monitor, and touch the alligator clips with your hands. Does the output change?
After you get the chorder hardware set up, answer the following questions on a piece of scratch paper.
Serial.printlnfunction. Change your code to print in base-2 or binary. What change did you make?
000000110101There’s no builtin function in Arduino to do this, but we can write our own. Follow these steps:
printBitsthat accepts a
shortparameter for your bitfield.
forloop that counts from 11 through 0. (You don’t need to start at 11. Replace 11 with the highest number whose pin has an alligator clip.)