On the lower left-hand portion of every Adafruit Circuit Playground Express, there is a small onboard speaker and class D amplifier. This allows you to make some pretty loud sounds! The amplifier is also connected to the true analog output on pin A0, which is marked with a wavy line symbol. You can turn off the speaker and only output through the pin if you like. For this tutorial, we are going to use the speaker and the capacitive touchpads to make a capacitive touch keyboard!
There are seven capacitive touchpads on the Circuit Playground Express which means we have a pad for each note in a scale! You can either touch the pads directly or use alligator clips or jumpers to connect to any capacitive/conductive item you like. You can make the keys out of tools, fruit, or even liquid! I have used small amounts of copper tape connected with alligator clips. If you decide to connect to something with jumpers, be aware that allowing the wires to touch each other may give you false readings. Either route them in a way that none touch each other, or I found that if the wires are not moved after the board is powered up everything works even if they touch.
The Code
Here is the complete program in MakeCode.
To make this sketch work properly we need to use while logic blocks. Each while loop contains one note and a light colour. While the capacitive pin is touched it runs the code within the respective while loop. The last note is played by touching both A3 and A2. For this to work properly we need to specify that A2 is played when only A2 is played and not with A3. On start, we initialize the volume and the brightness. We use the “pin X is pressed” input for capacitive touch sensing. If a pin is touched, then it outputs a unique sound and turns all the NeoPixels to a unique colour. If nothing is touched, then all sounds are stopped, and the lights are cleared.
Now you have a functional capacitive touch keyboard on your Circuit Playground Express. Try playing around with the code to make multiple tones play at once! Another element that you could add is some sharp/flat or minor notes by touching two keys at once. If you want to learn how to make the same project using CircuitPython, check out our Circuit Playground Tutorials!