The Circuit Playground can also emulate a USB MIDI drum kit. This will let us play drums using the capacitive touch pads, or anything we hook up to them, like a bunch of fruit. But first, a bit of info on MIDI.
MIDI stands for Musical Instrument Digital Interface and encompasses a whole world of various hardware and software products. For our purposes, we can think in terms of a very simple setup as shown below.
The MIDI PRODUCER generates MIDI SIGNALS which are received by a MIDI CONSUMER that processes the command in the MIDI SIGNAL and takes some kind of action.
For a general overview of the structure of MIDI signals and the commands they contain, check out this episode of Collin's Lab.
For our setup, the Circuit Playground will be the MIDI PRODUCER and a computer will be the MIDI CONSUMER. The MIDI SIGNALS will be transmitted over USB. Software running on the computer will receive and process the MIDI commands.
Since we are going to turn the Circuit Playground into a drum kit, we can use a special set of MIDI commands defined in the General MIDI Specification. These commands use channel 10 to specify percussion (drums) and note numbers 35-81 to specify a specific instrument (kick, snare, hi-hat, etc.). For example, note number 56 is a cowbell.
Ok. Let's put the pieces together.
MIDI USB Library Install
In order to emulate a USB MIDI device, we will use the Arduino Library MIDIUSB. To install it, open up the Library Manager.
Sketch -> Include Library -> Manage Libraries...
Filter the libraries by typing 'midi usb' in the search box in the upper right. Then select the MIDIUSB library and click Install.
MIDI Drum Sketch
Here is the Arduino sketch to turn the Circuit Playground into a USB MIDI drum machine.
// SPDX-FileCopyrightText: 2020 Carter Nelson for Adafruit Industries // // SPDX-License-Identifier: MIT //////////////////////////////////////////////////////////////////////////// // Circuit Playground Capacitive Touch USB MIDI Drums // // Send MIDI percussion commands over USB. // https://www.arduino.cc/en/Reference/MIDIUSB // https://en.wikipedia.org/wiki/General_MIDI#Percussion // // Author: Carter Nelson // MIT License (https://opensource.org/licenses/MIT) #include <Adafruit_CircuitPlayground.h> #include "MIDIUSB.h" #define CAP_THRESHOLD 50 #define DEBOUNCE 100 uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10}; uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t); //////////////////////////////////////////////////////////////////////////// void takeAction(uint8_t pad) { Serial.print("PAD "); Serial.print(pad); Serial.print(". Sending MIDI: "); switch (pad) { case 3: Serial.println("36"); drumHit(36); break; case 2: Serial.println("37"); drumHit(37); break; case 0: Serial.println("38"); drumHit(38); break; case 1: Serial.println("39"); drumHit(39); break; case 12: Serial.println("40"); drumHit(40); break; case 6: Serial.println("41"); drumHit(41); break; case 9: Serial.println("42"); drumHit(42); break; case 10: Serial.println("43"); drumHit(43); break; default: Serial.println("THIS SHOULD NEVER HAPPEN."); } } //////////////////////////////////////////////////////////////////////////// boolean capButton(uint8_t pad) { // Check if capacitive touch exceeds threshold. if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) { return true; } else { return false; } } //////////////////////////////////////////////////////////////////////////// void noteOn(byte channel, byte pitch, byte velocity) { // Send a MIDI Note On command. midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity}; MidiUSB.sendMIDI(noteOn); } //////////////////////////////////////////////////////////////////////////// void noteOff(byte channel, byte pitch, byte velocity) { // Send a MIDI Note Off command. midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity}; MidiUSB.sendMIDI(noteOff); } //////////////////////////////////////////////////////////////////////////// void drumHit(byte drum) { // Send Note On/Off command for given drum (note) number. noteOn(9, drum, 125); MidiUSB.flush(); noteOff(9, drum, 0); MidiUSB.flush(); } //////////////////////////////////////////////////////////////////////////// void setup() { // Initialize serial. Serial.begin(9600); // Initialize Circuit Playground library. CircuitPlayground.begin(); } //////////////////////////////////////////////////////////////////////////// void loop() { // Loop over every pad. for (int i=0; i<numberOfPads; i++) { // Check if pad is touched. if (capButton(pads[i])) { // Do something. takeAction(pads[i]); // But not too often. delay(DEBOUNCE); } } }
Download it and use the Arduino IDE to upload it to the Circuit Playground.
MIDI Drum Machine Software
We will use the software application Hydrogen to receive the MIDI signals from the Circuit Playground and play drum sounds. Downloads are available for Linux, Windows, and OS X.
The Hydrogen application is a full featured drum machine that let's you create drum patterns and full songs. For our simple Circuit Playground drum machine, we will ignore most of the user interface. The drum sounds are shown on the left side of the window and the corresponding Circuit Playground touch pad is shown in the figure below.
To enable the Circuit Playground in the Hydrogen software, first load the above sketch and make sure the Circuit Playground is connected via USB. Now, open the preferences window:
Tools -> Preferences
and select the Midi System tab. In the Input drop down, select the option with Circuit Playground in it. Also, check the Ignore note-off box. Then click OK.
At this point, if you touch one of the pads on the Circuit Playground you should hear a drum sound.
Making the Fruit Drums
Let's connect some fruit! Pretty much anything should work. Just get a bunch of fruit and hook them up using the alligator clips.
One end of the alligator clip goes to the fruit, the other end to the touch pad on the Circuit Playground.
Once you've got your fruit all connected and arranged, it's time to rock out. The video below shows a brief demo of the Circuit Playground USB MIDI Fruit Drum Machine in action. Note that the Hydrogen app is running on the monitor in the background. The Circuit Playground is attached to the same PC via the USB cable.
Page last edited January 22, 2025
Text editor powered by tinymce.