🛠 Contrôleur MIDI USB : un potard qui joue du piano (MIDIx4/MIDIx16)
Objectives
MIDI is one of the Teensy's favorite playgrounds: the USB MIDI class is class-compliant (no drivers needed on Windows, Mac, Linux) and PJRC pushes the concept to MIDIx4 and MIDIx16 — 4 or 16 virtual MIDI cables over a single USB cable, at 480 Mbit/s. You will build a minimal controller: a potentiometer on A0 that plays a note (C4 to B5) in any DAW.
Hardware
- 1 Teensy 4.0 + USB cable to a computer
- 1 10 kΩ potentiometer: 3.3 V / GND / wiper to A0
- A DAW, GarageBand, or a MIDI monitor (MIDI-OX, midiMonitor, the web app webmidi.com)
- MIDI Library via the IDE's library manager
Procedure
- Tools > USB Type : MIDI BEFORE uploading — this is the step everyone forgets.
- Upload the sketch below, unplug/replug the USB if needed (no hardware reset on the 4.0).
- Open your DAW: a "Teensy MIDI" device appears.
Complete Code
// Potard A0 -> note MIDI. USB Type: MIDI requis.
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE;
uint8_t derniereNote = 0;
void setup { MIDI.begin(MIDI_CHANNEL_OMNI); }
void loop {
// ADC 10 bits effectifs (plage 0-3,3 V) -> 24 demi-tons
uint8_t note = 60 + map(analogRead(A0), 0, 1023, 0, 23); // C4..B5
if (note != derniereNote) {
if (derniereNote) MIDI.sendNoteOff(derniereNote, 0, 1); // lâche l'ancienne
MIDI.sendNoteOn(note, 100, 1); // vélocité 100
derniereNote = note;
}
delay(10);
}
Expected Verification
Turn the pot: notes change cleanly (the previous note is always released before the new one — no "stuck" notes). The MIDI monitor displays Note On/Off on channel 1. To go further: change USB Type to MIDIx16 and transmit on 16 distinct logical channels, or combine with Keyboard + MIDI mode (the Teensy becomes both a USB keyboard and a MIDI device).
Common Errors
- Forgetting USB Type : MIDI: the sketch compiles and runs, but no MIDI device appears — the USB descriptor is chosen AT COMPILE TIME.
- The Serial Monitor disappears in MIDI mode: the MIDI class does not carry serial data; to debug, use a combined USB Type (e.g., "Serial + MIDI") if available, or use the LED.
- Ghost notes after replugging: without a hardware reset, state is lost upon unplugging; send an All Notes Off (0xB0, 123) at startup if your synth is prone to this.
- Potentiometer on 5 V: again — the ADC range is 0–3.3 V, powering it with 5 V saturates the reading at the top end (and approaches the input limit).
- delay(10) in a real drum machine: for real-time MIDI, sync on the ARM_DWT_CYCCNT cycle counter or an IntervalTimer; 10 ms remains acceptable for a controller.
- Multiple Teensy MIDI devices on the same PC: DAWs list them all as "Teensy MIDI" — name your virtual cables (MIDIx4) to keep track.
Other tutorials for this board
- Premiers pas sur le Teensy 4.0 : Teensyduino, Loader et premier Blink
- GPIO et PWM sous 3,3 V : les règles d'or du Teensy 4.0
- Entrées analogiques et capteur de température interne : le ADC et ses limites
- Piloter un ruban WS2812 sans bloquer le CPU : WS2812Serial et OctoWS2811
- Comprendre la mémoire : 2 Mo de flash, 1 Mo de RAM TCM/DMA, EEPROM émulée et LittleFS
- Deux ports USB simultanés : périphérique 480 Mbit/s ET hôte USB sur les pastilles
Code examples for this board
clignotante.inoArduinotouche-capacitive.inoArduinoanalogique-12-bits.inoArduinomidi-piano.inoArduinopwm-respiration.inoArduinoservo-angle.inoArduino