Category Archives: Arduino / Teensy

Testing USB Midi on Teensy 2.0++

A week ago I purchased the official Arduino Starter Kit (ASK). I don’t know very much about electronics and felt that I should. Having a lot of vintage stuff will sooner or later require that!

I’m planning to build a custom Midi interface and realized that the ASK was very good for learning, but it didn’t have all the parts / functionality needed for both midi in and out. Fixing midi out is very simple, it only requires a resistor and a midi cable. Everything except the midi cable is in the ASK. There are a lot of examples on how to play simple midi notes. More about that some other time.

Since midi in is a must I purchased a Teensy 2.0++ board which is a variant of the Arduino. It uses the same development tools as the Arduino with some added plugins. The reason for choosing a Teensy for a project like this is because that it has USB HID functionality. This means that you can tell the Teensy what the device you connect it to should interpret the Teensy as. You can set the Teensy to be discovered as a midi device, and that’s exactly what I need. The special USB Midi library is called usbMIDI and there’s no good getting started code for it. Therefore I decided to share my first test with the Teensy using the usbMIDI library.

View video on YouTube (opens external site in new window)

Hooking up the components

First, the diagram. As you can see it’s very simple, digital out 4 (you can choose whatever digital out you want) is connected to a LED with a 220 Ω resistor in between.

The software that I made the image with didn't have a Teensy board so I took an Arduino board instead. You need a Teensy for USB HID to work.
The software that I made the image with didn’t have a Teensy board so I took an Arduino board instead. You need a Teensy for USB HID to work.

A photo of the build:

This is what the circuit look like. I haven't soldered any pins on the Teensy yet, this is a temporary solution!
This is what the circuit looks like. I haven’t soldered any pins on the Teensy yet, this is a temporary solution!

The code


void setup(){
pinMode(4, OUTPUT); //Use digital out 4 for the LED
usbMIDI.setHandleNoteOn(OnNoteOn); //Specify which function to handle NoteOn events
usbMIDI.setHandleNoteOff(OnNoteOff); //Specify which function to handle NoteOff events
}

void loop(){
usbMIDI.read(); //Start listen for MIDI events from USB
}

void OnNoteOn(byte channel, byte note, byte velocity){ //This is the function that handles NoteOn events
if(note == 60){ //If the note was C4 = 60
digitalWrite(4, HIGH); //5V ON
}
}

void OnNoteOff(byte channel, byte note, byte velocity){ //This is the function that handles NoteOff events
digitalWrite(4, LOW); //5V OFF, doesn't matter what note you release
}