Teensy MIDI Controller and Pure Data

Introduction

Today we’re using our feet to flip bits!  Using a foot switch pedal, similar to those for guitar and bass amps, we’ll be able to communicate with Pure Data (Pd) by means of a super-sweet microcontroller development board called PJRC’s Teensy.  Though this time we’ll only be using a single switch to toggle a switch in Pd, there are endless possibilities to dream up.

Hardware

PJRC’s Teensy line of microcontroller development boards are powerful little buggers, offering the speed and features of high-end Arduinos with much smaller footprints.  Oh, and you can run Arduino code on them.  Pretty sweet, eh?

The hardware setup for this project is extra simple:

  • 1x Teensy board + USB cable
  • 1x SPST, DPDT, 3PDT, etc., on/off foot switch
  • 1x 10kΩ, 1/4W resistor
  • 3x 330Ω, 1/4W resistors [optional]
  • 3x bicolor, 3-lead LEDs [optional]
  • 1x 1/8″, TRS jack [optional]
footswitch_dpdt

DPDT foot switch, courtesy All Electronics.

The flavor of Teensy I’ll be using is version 3.2, but any should work — PJRC says that ‘Teensy 2.0, Teensy++ 2.0, Teensy LC and Teensy 3.0 to 3.6 are supported,’ for the library we’ll be using (more on that in the Firmware section).

The foot switches are widely available on eBay, and I’ve seen them on All Electronics, too.  SPST (single-pole, single-throw) is all we need here, but I recommend DPDT or 3PDT (double- or triple-pole, double throw) switches for their versatility.  For example, my pedal exploits the extra pole to display whether the pedal is ‘on’ or ‘off’ with bicolor LEDs.  Also, be wary of the on/off types versus momentary — both would work here, but you may be unhappily leaning on your pedal for minutes at a time if you choose incorrectly.

We’re just using the switch to send a +5V signal to digital pin D0 when on, and not when off.  The 10kΩ pulldown resistor is crucial here — with it absent the pin is floating and the Teensy won’t know what to do!  (Actually, in my experience, the pin floats at ~2.5V and is interpreted as still high.)

The schematic for this project is shown below:

footswitch_schematic

Foot switch schematic.

Recall that the TRS jack, and the LEDs with resistors, are all optional.  You’ll need some way to provide +5V and GND to the foot switch, and some way to return the on/off signal; I chose to use an 1/8″ TRS jack — not because it’s the most appropriate connector (there’s no way I’d accidentally plug my iPod into it), but because I found a sweet deal on an electronics surplus website.  I also had a bunch of bicolor LEDs hanging around, so voilà.

The 330Ω resistors are used to limit the current in the LEDs.  Higher (470~1kΩ) or lower (150~300Ω) values will do, but it’s best to experiment (using a DMM and your eyeballs) if you choose to deviate too much.  Note that it may be tempting to use a single ~100Ω resistor tied to all of the LED cathodes, but this is not a good idea.

Once your hardware is all set up, it may resemble something like this:

Firmware

Luckily, the firmware for the Teensy is almost as simple as the hardware.  But don’t be fooled by our leisure — there is a ton going on under the hood.  We’ll be riding on the Arduino bus for the IDE and code to keep things quick and concise since performance isn’t a big deal.  And we’ll also be relying on PJRC’s USB MIDI library to do all the heavy lifting.

This library gives us over a dozen different ways to send MIDI messages through the USB connection, including note changes, control changes, pitch bends, and a whole mess of others.  Since we’re toggling a simple switch in Pd, the control change function seems most appropriate.  If you’d like to expand to creating a musical instrument, this is where you may deviate into or integrate note changes, pitch bends, etc.  But we digress.

Before flashing the code to our Teensy, we must make sure our settings are correct.  (See here on how to set up Teensyduino if you haven’t already.)

  1. The Teensy board of choice should be selected in Tools >> Board >> Teensy 3.2 (or your flavor of Teensy).
  2. USB type (Tools >> USB Type) should include MIDI — e.g. MIDI, Serial + MIDI, or Serial + MIDI + Audio.
  3. CPU speed (Tools >> CPU Speed) needn’t be overclocked — the lowest speed without disallowing USB communication should do.
  4. Make sure the Teensy device is found and selected in Tools >> Port.

The following Arduino code is a slimmed-down version of the Transmit Example from the USB MIDI page, substituting note changes with control changes.  (Similar example code can be found in Arduino’s examples under Teensy >> USB_MIDI >> Buttons.)  As an added bonus, we’re utilizing the Bounce library that lends us a software debounce for our buttons!  Joy!

#include <Bounce.h>

#define BUTTON            0
#define DEBOUNCE_MS       5

#define LED               13

#define CHANNEL           1
#define CONTROL           1
#define ON                1
#define OFF               0

Bounce footswitch = Bounce( BUTTON, DEBOUNCE_MS );

void setup()
{
  pinMode( BUTTON, INPUT );
  pinMode( LED, OUTPUT );
}

void loop()
{
  footswitch.update();

  if( footswitch.fallingEdge() )
  {
    usbMIDI.sendControlChange( CONTROL, OFF, CHANNEL );
    digitalWrite( LED, LOW );
  }

  if( footswitch.risingEdge() )
  {
    usbMIDI.sendControlChange( CONTROL, ON, CHANNEL );
    digitalWrite( LED, HIGH );
  }
}

A few things to note here:

  • We could use the INPUT_PULLDOWN pin mode for pin D0, forgoing the external 10kΩ resistor.  I’m no fan of this method, for a few cents for a resistor and a few seconds of soldering is worth the peace-of-mind, checking that off our ‘no need to think about that later’ box.
  • Having the Teensy LED (pin D13) follow the switch input state is only nice to have during the first setup; once things are rolling these associated lines of code can be removed.
  • The second argument of the Bounce() function is the debounce time in milliseconds.  If your switch states appear sporadic try increasing this number to 10 or so, or more if necessary.
  • Our on/off states are edge-triggered, meaning the Teensy only sends out MIDI control value changes when the voltage on the pin changes from +5V to GND or vice versa.  Triggering when the switch is in steady-state at +5V or GND would cause the Teensy to send out control messages every 5ms (or whatever the BOUNCE_MS value is set to).

Software

Because of the Teensy’s magical ways, we won’t need any additional software to interface with Pd — we can stroll on right into it!

Make sure you have (a somewhat up-to-date version of) Pd Vanilla or other distribution.  We won’t be using any fancy atoms from Extended or anything — just basic MIDI atoms.  And if you’ve just flashed your firmware into the Teensy, make sure to reopen Pd so that it will check for new MIDI devices.

From the main window, choose Media >> MIDI Settings…  Under Input device 1: click the button that says ‘none’ (or another MIDI controller) and there should be a device called ‘/dev/midi1’ or similar, based on your operating system.

midi_device

Now we’re ready to test!  We could just go into Media >> Test Audio and MIDI…, see the little bang atom go black (or not, and go troubleshooting), and rejoice, but since we’ll be using this pedal later let’s begin with a skeleton patch:

toggle_off

Pd test patch for Teensy MIDI controller.

Here, the [ctlin] atom outputs, from left to right, the value, control number, and MIDI channel.  Since we set the MIDI channel to 1, we can add that to the atom’s parameters (and we could add the control number, too, if we’d like).  The [t b] and below atoms simply keep track of how many messages we receive, and the toggle box will display the last value we receive.

Now we’re ready to stomp our feet!  Given that everything is working properly, and our foot switch is initially off, our patch and log may look something like below after a couple stomps:

Success!

Going Further

Seems like a lot of work to toggle a switch, no?  But now we have the framework to move into bigger and better things.

For example, I began this project as a way for my significant other to toggle a chorus effect for when we jam to That’s Not My Name, and soon (time is relative) I’ll add a looping effect, too, for vocal layering.  Such a project will require a bit more hardware (i.e. several switches) but the heavy lifting is mostly done.

A quick search on DuckDuckGo yields quite a few neato projects for inspiration, like a Monster MIDI Controller, a much larger MIDI foot controller, and even bizarre yet wonderful MIDI woodwind controller.  Now get some inspiration and make something cool!

Alternative Means: Pduino and Firmata

If you don’t have US$20 to burn on a new dev board, and you’ve got an old Arduino or two lying around, there is an alternative way to speak to Pd.  Pduino is a patch that uses a serial connection to communicate with your board instead of acting like a MIDI device.  Combined with the Firmata firmware, this seems like a very similar way to mate hardware with software for our application.  (Note that I have only tested this combination once, thought, ‘Huh, that’s cool,’ and left it at that.  But I’m sure it’s great!)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s