Arduino library · Communication

MIDIUSB: a Leonardo or Pro Micro as a USB MIDI device

MIDIUSB is Arduino’s own library for the boards built on the ATmega32U4: the Leonardo, the Micro and the Pro Micro. That chip has USB inside it, so with this library the board shows up on the computer as a class-compliant MIDI device. Music software or a browser synth finds it with no driver and no serial-to-MIDI bridge program in between. That is what sets it apart from the MIDI library by FortySevenEffects, which sends serial MIDI out of a TX pin to a DIN socket.

Built in on Mokxi: #include <MIDIUSB.h> and it builds, with nothing to install. Free, and no account needed.

What MIDIUSB does

Every message is a four-byte USB MIDI event packet. The first byte is the code index number (0x09 for note on, 0x08 for note off, 0x0B for control change); the other three are the ordinary MIDI bytes, a status byte with the channel in its low four bits followed by two data bytes. sendMIDI() queues a packet and flush() sends what is queued. pitchToNote.h names the note numbers, so pitchC4 is 60 and pitchA4 is 69.

In Mokxi the sketch builds and runs on the Leonardo, Micro and Pro Micro, but no USB host is simulated, so the packets go nowhere: the first sendMIDI() prints a one-line notice on the serial monitor and the sketch carries on. That still lets you check a controller’s logic (the button scan, a potentiometer mapped to a CC value, the timing of the notes) before you flash the real board. On an Uno or an ESP32 the build stops with the library’s own error, because those boards have no USB the sketch can use.

Library Manager nameMIDIUSB
AuthorArduino
Include#include <MIDIUSB.h>
#include <MIDIUSB_Defs.h>
#include <pitchToNote.h>
BoardsArduino Leonardo, Arduino Micro, SparkFun Pro Micro
In the simulatorBuilds and runs, and says plainly that the hardware behind it is not simulated.

Install it

It builds on Arduino Leonardo, Arduino Micro, SparkFun Pro Micro.

In Mokxi it is built in: #include <MIDIUSB.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "MIDIUSB" by Arduino from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add MIDIUSB to lib_deps in PlatformIO. Mokxi follows the calls of release 1.0.5, so a sketch written here builds there too.

What to know in the simulator: No USB host is simulated, so the messages go nowhere: the first sendMIDI() says so once on the serial monitor, and the sketch runs on. On a real board the computer sees a MIDI device. Other boards stop with upstream’s “MIDIUSB can only be used with an USB MCU.”

The calls you will use

In the order a sketch meets them.

midiEventPacket_t noteOn = {0x09, 0x90 | 0, 48, 64}

Builds a note on packet: code index, status with channel 0 to 15 in the low bits, note number and velocity. The example casts the status byte to byte so the braces do not narrow an int.

MidiUSB.sendMIDI(noteOn)

Queues one packet for the USB MIDI endpoint. It returns nothing; on a real board the packet waits in the buffer until flush() or until the buffer fills.

MidiUSB.flush()

Sends everything queued so far. Call it after each burst of messages so the computer hears a note when you play it, not a moment later.

midiEventPacket_t cc = {0x0B, 0xB0 | 0, 10, 65}

A control change packet: controller 10 (pan) set to 65 on channel 0. Map a knob to it with analogRead() / 8 for the 0 to 127 range.

byte value = analogRead(A0) / 8;
midiEventPacket_t cc = {0x0B, 0xB0, 1, value};
MidiUSB.sendMIDI(cc);
MidiUSB.flush();

midiEventPacket_t rx = MidiUSB.read()

Returns the next packet the computer sent. A header of 0 means nothing came in, so loop until it is 0 to empty the queue.

Example: MIDIUSB_write: a note on and off every two seconds, from a Leonardo

On the Arduino Leonardo. It runs in the browser exactly as printed.

sketch.ino · Arduino Leonardo
/*
 * MIDIUSB_test.ino
 *
 * Created: 4/6/2015 10:47:08 AM
 * Author: gurbrinder grewal
 * Modified by Arduino LLC (2015)
 */

#include "MIDIUSB.h"

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).
// (Mokxi: the (byte) casts are this copy's only change. Upstream's braces
// narrow an int, which builds with a warning, here as in the IDE.)

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, (byte)(0x90 | channel), pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, (byte)(0x80 | channel), pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

void setup() {
  Serial.begin(115200);
}

// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, (byte)(0xB0 | channel), control, value};
  MidiUSB.sendMIDI(event);
}

void loop() {
  Serial.println("Sending note on");
  noteOn(0, 48, 64);   // Channel 0, middle C, normal velocity
  MidiUSB.flush();
  delay(500);
  Serial.println("Sending note off");
  noteOff(0, 48, 64);  // Channel 0, middle C, normal velocity
  MidiUSB.flush();
  delay(1500);

  // controlChange(0, 10, 65); // Set the value of controller 10 on channel 0 to 65
}

Open it in the editor, wired and ready to run

Common errors and how to fix them

MIDIUSB can only be used with an USB MCU.

The board you picked has no native USB, so the library refuses to build. Choose a Leonardo, Micro or Pro Micro. The Uno’s USB chip is a separate serial bridge that the sketch cannot reach without reflashing that chip.

A note keeps sounding after the key is released

Every note on needs a matching note off with the same channel and note number. Check that the code path for a released button always sends one, and that you call flush() after it, since a note off left in the buffer never reaches the synth.

Note 48 plays an octave lower than the comment says

The upstream example calls 48 middle C, but most software labels note 60 as C4, the middle C on a piano, and pitchToNote.h agrees. Use pitchC4 or 60 when you want middle C, and check which octave naming your DAW uses.

[Mokxi] MIDIUSB: no USB host is simulated, so these MIDI messages go nowhere.

That line is expected in the simulator and appears once. The sketch keeps running, so test your buttons, knobs and timing there, then upload to a real 32U4 board to hear the notes in your music software.

Questions and answers

Can the board be a MIDI device and a serial port at once?

Yes. A 32U4 board with MIDIUSB is a composite USB device, so Serial still works for debugging while the computer sees the MIDI port beside it.

Which boards can I pick for a MIDIUSB sketch in Mokxi?

The Leonardo, the Micro and the Pro Micro, the three ATmega32U4 boards. Any other board stops the build with the library’s USB MCU error, so switch the board before you start.

Try MIDIUSB without installing anything

Open the example, change the sketch, and keep your version in a free account.