# Circuit Playground Fruit Drums

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/037/285/medium800/circuit_playground_fruit_banner.jpg?1479071905)

In this guide we will explore the capacitive touch capabilities of the Circuit Playground and work our way towards putting together a USB MIDI drum machine driven by fruit. We'll also turn the Circuit Playground into a 'keyboard' so you can use it to play games or run an application that uses arrow keys

https://youtu.be/ooN6G6ZstN8

# Required Parts

This project will work mainly with the Circuit Playground attached to a computer via a USB cable. Alligator clips will be used to attach to the capacitive touch pads.

- [Circuit Playground](https://www.adafruit.com/product/3000)

![circuit_playground_item_cp.jpg](https://cdn-learn.adafruit.com/assets/assets/000/037/279/medium640/circuit_playground_item_cp.jpg?1479068813)

- [Alligator Clips](https://www.adafruit.com/products/1008)

![circuit_playground_item_alligators.jpg](https://cdn-learn.adafruit.com/assets/assets/000/037/280/medium640/circuit_playground_item_alligators.jpg?1479068865)

- [USB cable](https://www.adafruit.com/products/592) for programming and power.

![circuit_playground_item_usb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/037/283/medium640/circuit_playground_item_usb.jpg?1479069505)

# Other Items

Various other items will be used. These do not need to be exact, anything similar will be fine.

- Paper (6"x4" cards)
- Marker
- Ruler

![circuit_playground_item_paper.jpg](https://cdn-learn.adafruit.com/assets/assets/000/037/281/medium640/circuit_playground_item_paper.jpg?1479068983)

And of course..... **FRUIT!**

![](https://cdn-learn.adafruit.com/assets/assets/000/037/282/medium800/circuit_playground_item_fruit.jpg?1479069040)

# Before Starting

If you are new to the Circuit Playground, you may want to first read these overview guides.

- [Overview](../../../introducing-circuit-playground)
- [Lesson #0](../../../circuit-playground-lesson-number-0)

This project will use the Arduino IDE. Make sure you have added the board support for the Circuit Playground as well as installed the Circuit Playground library. **MUST DO BOTH.** This is covered in the guides linked above.

# Circuit Playground Fruit Drums

## Hello Capacitive Touch

Since this project uses the capacitive touch capabilities of the Circuit Playground, let's start by exploring how they work. You can read some technical details in the [Lesson #0 Guide](../../../../circuit-playground-lesson-number-0/alligator-pads-pinout#capacitive-touch). There are 8 pads total located on the edge of the Circuit Playground as shown below. Note the numbering scheme used to identify each pad: #3, #2, #0, #1, #12, #6, #9, and #10.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/229/medium800/circuit_playground_GPIO.jpg?1478912694)

The library function `readCap()` is all that is needed to use capacitive touch. It is used the same for all 8 pads. Simply call it with the pad number of interest and it returns a value. This value will be near zero when not touched and have some higher value when touched. We can use the simple sketch below to examine how this value behaves, focusing on just one pad, #1 in this case.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Circuit_Playground_Classic_Fruit_Drums/HelloCapTouch1/HelloCapTouch1.ino

 **This sketch only uses the #1 touch pad.**

![circuit_playground_touch_pin1.jpg](https://cdn-learn.adafruit.com/assets/assets/000/037/284/medium640/circuit_playground_touch_pin1.jpg?1479070363)

With this sketch loaded and running on the Circuit Playground, open the Serial Plotter.

**Tools -\> Serial Plotter**

The capacitive touch value from the #1 pad will be plotted like a strip chart as shown below. Try touching the pad quickly several times and also holding your finger on it.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/230/medium800/circuit_playground_cap_touch_time_hist1.png?1478913342)

The `readCap()` function is different from the push button functions `leftButton()` and `rightButton()` which return true when the button is pressed and false otherwise. However, we can take a very simple approach to create a similar behavior for the capacitive touch pads. We compare the value returned by `readCap()` to a preset threshold value which we'll call **CAP\_THRESHOLD**. If it exceeds this value, it is touched (pressed), otherwise, it is not. Here's a simple function to do just that.

```auto
boolean capButton(uint8_t pad) {
  if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) {
    return true;  
  } else {
    return false;
  }
}
```

The actual value for **CAP\_THRESHOLD** will be defined globally. But what is a good value? Well, that will depend on your setup. If the value is set too low, you may get incorrect detections due to noise. But if you set it too high, you may never detect any touches. You could use the Serial Monitor or Plotter with the above sketch to see what values your setup returns, and then choose a value based on that. **For the examples here, we will use a value of 50.**

Here is another simple sketch that uses the `capButton()` function along with a **CAP\_THRESHOLD** of 50 to detect when the #1 pad is touched.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Circuit_Playground_Classic_Fruit_Drums/HelloCapTouch2/HelloCapTouch2.ino

The additional value **DEBOUNCE** is used to prevent the output from happening too fast. With the above sketch loaded and running, open the Serial Monitor.

**Tools -\> Serial Monitor**

Whenever the #1 pad is touched, you should see a message printed out.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/231/medium800/circuit_playground_cap_touch_serial_monitor.png?1478916211)

Now let's move on to generalizing the code to detect all of the touch pads and take various actions depending on what pad is pressed.

# Circuit Playground Fruit Drums

## Basic Print

Let's start simple. This sketch prints a unique message for each touch pad. Download it and load it to the Circuit Playground.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Circuit_Playground_Classic_Fruit_Drums/CapTouchBasic/CapTouchBasic.ino

With this sketch loaded and running on the Circuit Playground, open the Serial Monitor.

**Tools -\> Serial Monitor**

Now press any of the 8 touch pads. You should see a message printed out for each one.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/242/medium800/circuit_playground_basic_serial_mon.png?1478971510)

Take a look at the `loop()` function in this sketch. The main change from the previous sketch is the addition of an outer `for` loop which scans over each of the touch pads on the Circuit Playground.

```auto
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);
    }
  }
}
```

The variable `numberOfPads` and the array `pads[]` have been defined globally near the top of the sketch. The sequence of the numbers in `pads[]` is such that they are scanned in a counter-clockwise fashion starting with pad #3.

```auto
uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10};
uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t);
```

Also, a new function called `takeAction()` has been created. This allows us to use this same `loop()` setup for all of the sketches. All we have to do is change the code in `takeAction()` to change the behavior of the Circuit Playground.

If you look at the code in `takeAction()` you will see it contains a `switch` statement. This is where the main plumbing between Circuit Playground touch pads and actual code takes place. The `case` statements correspond to pad numbers and the code inside the case statement defines what is executed for each pad.

For example, for pad #12, the following lines of code are executed:

```auto
      Serial.println("I'm Idaho!");
      break;
```

The `break` is needed to exit out of the `switch` statement.

Keen. Now let's do something a little more exciting than just printing out Simpsons quotes.

# Circuit Playground Fruit Drums

## Tone Piano

The following sketch uses the Circuit Playground speaker to play a different tone for each pad when touched.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Circuit_Playground_Classic_Fruit_Drums/CapTouchTones/CapTouchTones.ino

Info: 

Look in the `takeAction()` function and you will see a call to `playTone()` for each of the pads. The frequencies correspond to the basic musical notes. With this sketch loaded and running on the Circuit Playground, you should hear different tones played when a pad is touched.

We can use alligator clips and some paper to create a little piano. On a 6"x4" index card or other piece of paper, draw something similar to the picture below.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/254/medium800/circuit_playground_paper_piano.jpg?1478977383)

Now attach the alligator clips to the edge of the paper and the Circuit Playground as shown below.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/250/medium800/circuit_playground_tone_piano_wires.jpg?1478975394)

Once you've clipped the alligators, press the reset button on the Circuit Playground so it will re-calibrate the capacitive touch sensors

To play a note, just touch the alligator clip on the edge of the paper. For example, in the picture below, E is being played.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/251/medium800/circuit_playground_tone_piano_touch.jpg?1478975830)

Try out the following sequence of notes.

### **C-D-E-C-E-C-E &nbsp; &nbsp; D-E-F-F-E-D-F** &nbsp;
Do you recognize the tune? Here's a hint.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/252/medium800/circuit_playground_dorami.jpg?1478977100)

# Circuit Playground Fruit Drums

## Cirkey Cirkey

One of the more advanced features of the Circuit Playground is its ability to emulate a USB keyboard and mouse. To do this, we use the Arduino [Mouse and Keyboard libraries](https://www.arduino.cc/en/Reference/MouseKeyboard).

The following sketch uses these libraries to emulate a USB keyboard and mouse and send key and mouse events when the pads are touched.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Circuit_Playground_Classic_Fruit_Drums/CapTouchKeyboard/CapTouchKeyboard.ino

If this sounds familiar that's because this is essentially what a [Makey Makey](http://makeymakey.com/) does. The default key mapping in the sketch provided above is similar to the original Makey Makey.

- PAD #3: Left Arrow
- PAD #2: Up Arrow
- PAD #0: Down Arrow
- PAD #1: Right Arrow
- PAD #12: Space Bar
- PAD #6: Left Mouse Button Click
- PAD #9: Middle Mouse Button Click
- PAD #10: Right Mouse Button Click

We can use alligator clips and some paper to create a cheat sheet for the key mapping. On a 6"x4" index card or other piece of paper, draw something similar to the picture below.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/262/medium800/circuit_playground_paper_key_mouse.jpg?1478978352)

Now attach the alligator clips to the edge of the paper and the Circuit Playground as shown below.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/264/medium800/circuit_playground_keyboard_wires.jpg?1478978399)

To send the key press or mouse button click, touch the associated alligator clip.

Danger: 

Since there is a danger of flooding your computer with too many key presses or mouse clicks, the slide switch on the Circuit Playground is used to turn the keyboard/mouse emulation on and off. With the slide switch to the left (+), the emulator is active and the red LED will come on. **The Circuit Playground will send key and mouse events with every capacitive touch.** To turn the emulator off, set the slide switch to the right (-) position.

With this sketch loaded and running on the Circuit Playground, turn on the emulator using the slide switch. Then try out these Makey Makey apps:

- [Bongos](http://makeymakey.com/bongos/)
- [Piano](http://makeymakey.com/piano/)

# Circuit Playground Fruit Drums

## Fruit Drums

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](https://en.wikipedia.org/wiki/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.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/272/medium800/circuit_playground_midi1.jpg?1479055637)

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](https://youtu.be/NgIzSxTmQYA).

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.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/271/medium800/circuit_playground_midi2.jpg?1479055549)

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](https://en.wikipedia.org/wiki/General_MIDI#Percussion). 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](https://www.arduino.cc/en/Reference/MIDIUSB). To install it, open up the Library Manager.

**Sketch -\> Include Library -\> Manage Libraries...**

![](https://cdn-learn.adafruit.com/assets/assets/000/037/268/medium800/circuit_playground_ide_manage_lib.png?1479052792)

Filter the libraries by typing 'midi usb' in the search box in the upper right. Then select the **MIDIUSB** library and click **Install**.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/269/medium800/circuit_playground_midiusb_lib_install.png?1479053081)

# MIDI Drum Sketch

Here is the Arduino sketch to turn the Circuit Playground into a USB MIDI drum machine.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Circuit_Playground_Classic_Fruit_Drums/CapTouchMIDIDrums/CapTouchMIDIDrums.ino

Download it and use the Arduino IDE to upload it to the Circuit Playground.

Info: 

# MIDI Drum Machine Software

We will use the software application [Hydrogen](http://hydrogen-music.org/) to receive the MIDI signals from the Circuit Playground and play drum sounds. [Downloads](http://hydrogen-music.org/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.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/286/medium800/circuit_playground_hydrogen_cp_hookup2.png?1479083842)

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.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/275/medium800/circuit_playground_hydrogen_pref_midi.png?1479057580)

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.

The alligator clip can be connected directly to fruit with a stem.

![circuit_playground_fruit_with_stem.jpg](https://cdn-learn.adafruit.com/assets/assets/000/037/278/medium640/circuit_playground_fruit_with_stem.jpg?1479067455)

For fruit without a stem, a short jumper wire can be used.

![circuit_playground_fruit_no_stem.jpg](https://cdn-learn.adafruit.com/assets/assets/000/037/277/medium640/circuit_playground_fruit_no_stem.jpg?1479067301)

One end of the alligator clip goes to the fruit, the other end to the touch pad on the Circuit Playground.

![](https://cdn-learn.adafruit.com/assets/assets/000/037/288/medium800/circuit_playground_fruit_drum_setup.jpg?1479084089)

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.

https://youtu.be/ooN6G6ZstN8


## Featured Products

### Circuit Playground Classic

[Circuit Playground Classic](https://www.adafruit.com/product/3000)
Would you like to learn electronics, with an all-in-one board that has sensors and LEDs built in? **Circuit Playground** is here - and it's the best way to practice programming on real hardware with no soldering or sewing required!

This is the **Classic**...

In Stock
[Buy Now](https://www.adafruit.com/product/3000)
[Related Guides to the Product](https://learn.adafruit.com/products/3000/guides)
### Small Alligator Clip Test Lead (set of 12)

[Small Alligator Clip Test Lead (set of 12)](https://www.adafruit.com/product/1008)
Connect this to that without soldering using these handy mini alligator clip test leads. 15" cables with alligator clip on each end, color coded. You get 12 pieces in 6 colors. Strong and grippy, these always come in handy! We often use these in conjunction with a multimeter so we...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1008)
[Related Guides to the Product](https://learn.adafruit.com/products/1008/guides)
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

In Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
### 3 x AAA Battery Holder with On/Off Switch and 2-Pin JST

[3 x AAA Battery Holder with On/Off Switch and 2-Pin JST](https://www.adafruit.com/product/727)
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about 3.3-4.5V, a very similar range to Lithium Ion/polymer (Li-Ion) batteries and have an on-off switch. That makes...

In Stock
[Buy Now](https://www.adafruit.com/product/727)
[Related Guides to the Product](https://learn.adafruit.com/products/727/guides)

## Related Guides

- [Introducing Circuit Playground](https://learn.adafruit.com/introducing-circuit-playground.md)
- [Circuit Playground Digital Input](https://learn.adafruit.com/circuit-playground-digital-input.md)
- [Circuit Playground Sound-Controlled Robot](https://learn.adafruit.com/circuit-playground-sound-controlled-robot.md)
- [Simple and Beautiful NeoPixel Holiday Lights](https://learn.adafruit.com/simple-beautiful-color-changing-light-strand.md)
- [Wonder Woman Cosplay Bracers](https://learn.adafruit.com/wonder-woman-cosplay-bracers.md)
- [Circuit Playground Quick Draw](https://learn.adafruit.com/circuit-playground-quick-draw.md)
- [Circuit Playground Wearable](https://learn.adafruit.com/circuit-playground-wearable.md)
- [Circuit Playground Bike Light](https://learn.adafruit.com/circuit-playground-bike-light.md)
- [Circuit Playground Kaleidoscope](https://learn.adafruit.com/circuit-playground-kaleidoscope.md)
- [Dear Diary Alarm](https://learn.adafruit.com/dear-diary-alarm.md)
- [NeoPix Arcade Kit - 1D Arcade Game System - Circuit Playground](https://learn.adafruit.com/neopix-arcade-kit-1d-arcade-game-system-circuit-playground.md)
- [Ever-Burning Flame Painting](https://learn.adafruit.com/ever-burning-flame-painting.md)
- [Light Paintbrush with Circuit Playground](https://learn.adafruit.com/lightpaint-cplay.md)
- [Circuit Playground's Motion Sensor](https://learn.adafruit.com/circuit-playgrounds-motion-sensor.md)
- [Circuit Playground or Hallowing Jack-o'-Lantern](https://learn.adafruit.com/circuit-playground-jack-o-lantern.md)
- [Circuit Playground PZ-1: Pizza Box DJ Controller](https://learn.adafruit.com/circuit-playground-pizza-box-dj-controller.md)
