🛠 Boîte à rythmes USB MIDI : classes MIDI, MIDIx16 et port hôte USBHost_t36
Objectives
The Teensy 4.1 is the reference brain for DIY synthesizers: it presents itself as a USB MIDI class-compliant device (no drivers to install) with up to 16 virtual MIDI cables (MIDIx4/MIDIx16), and can simultaneously act as a USB host on its second port — both at 480 Mbit/s. This tutorial builds a trigger pad, then discusses reading an external USB keyboard.
Hardware
- 1 Teensy 4.1 + micro-USB data cable
- 1 push button on pin 24 (to GND)
- Optional for host mode: the special PJRC USB host cable and a USB device (keyboard, MIDI controller)
Key points
- Tools > USB Type > MIDI (or MIDIx4 / MIDIx16 for multiple cables).
- The USB serial port disappears when USB Type = MIDI: to debug, use a second port or change the type.
- Documented requirement: drain inputs with
while (usbMIDI.read) {}in every loop, otherwise USB buffers fill up and block. - In host mode, the USBHost_t36 library reads keyboards, MIDI controllers, and audio DACs on the second port (480/12/1.5 Mbit/s, hubs supported) via the dedicated host cable.
Complete code
#include <Bounce.h>
Bounce padBtn(24, 10); // anti-rebond 10 ms sur une broche du 4.1
void setup {}
void loop {
padBtn.update;
if (padBtn.fallingEdge) usbMIDI.sendNoteOn(60, 127, 1);
if (padBtn.risingEdge) usbMIDI.sendNoteOff(60, 0, 1);
// vider les entrées MIDI, sous peine de saturer les tampons USB
while (usbMIDI.read) { }
delay(1);
}
Expected verification
In your DAW (or a MIDI monitor like MIDI Monitor / MIDI-OX), the Teensy appears as a class-compliant MIDI device; each press sends a Note On 60 velocity 127 on channel 1. With MIDIx16, sixteen distinct virtual instruments can be addressed via a single USB cable.
Common errors
- Forgetting
while (usbMIDI.read): after a few seconds, no output is sent anymore — the classic MIDI issue on Teensy. - Staying in USB Type = Serial:
usbMIDIsimply does not exist at compile time. - Looking for the serial monitor after switching to MIDI: the USB serial interface has been replaced by the MIDI interface, this is normal.
- Plugging a standard USB device into the host port without the special host cable: the connector does not match, the PJRC cable is required.
- For complete controllers (encoders, faders, LED matrices), Josh Nishikawa's MIDIcontroller library relies on these classes and the 8 independent serial ports.
Other tutorials for this board
- Premiers pas sur le Teensy 4.1 : Teensyduino, Teensy Loader et premier Blink
- GPIO et PWM sur le Teensy 4.1 : 55 broches en 3,3 V, ni 5 V tolérées ni traîne
- Entrées analogiques du Teensy 4.1 : 18 broches, 2 ADC, 0-3,3 V fixes et zéro DAC
- Logger sur carte SD via le port SDIO natif du Teensy 4.1
- Serveur web embarqué : l'Ethernet natif 10/100 du Teensy 4.1 (DP83825)
- Ingénierie mémoire du Teensy 4.1 : TCM, DMAMEM, EXTMEM, flash QSPI et EEPROM
Code examples for this board
clignotante.inoArduinotouche-capacitive.inoArduinoanalogique-12-bits.inoArduinomidi-piano.inoArduinopwm-respiration.inoArduinoservo-angle.inoArduino