Source: https://mokxi.com/libraries/midiusb
Updated: 2026-11-22

Libraries / Communication

Arduino library · Communication

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

Yes, you can build with the MIDIUSB library online in Mokxi with nothing to install. Its hardware is not simulated, so a sketch says plainly that it cannot reach the device. The example below, "MIDIUSB_write: a note on and off every two seconds, from a Leonardo", runs on the Arduino Leonardo.

Run the example in Mokxi See the calls

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

## What MIDIUSB does

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.

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 name | MIDIUSB

Author | Arduino

Include | #include <MIDIUSB.h>
#include <MIDIUSB_Defs.h>
#include <pitchToNote.h>

Boards | Arduino Leonardo, Arduino Micro, SparkFun Pro Micro

In the simulator | Builds 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.”

- Arduino Leonardo
- Arduino Micro
- SparkFun Pro Micro

## 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.

### 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

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.

## Keep going

### Learn

- Arduino Leonardo Pinout: I2C, SPI on ICSP, A6-A11
- Pro Micro Pinout: 32U4 Pins for Arduino and QMK

### More communication libraries

- OneWire
- TinyGPS
- TinyGPSPlus
- MFRC522
- SoftwareSerial
- AltSoftSerial
- i2cdetect
- Keyboard
- RF24Mesh
- SoftWire
- MicroNMEA
- NeoGPS
- RadioHead (RH_NRF24 driver)

Every built-in library · Open the editor

## Try MIDIUSB without installing anything

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

Start building free

Open the editor
