🛠 Contrôleur MIDI USB : un potard qui joue du piano (MIDIx4/MIDIx16)

Teensy 4.0 · intermediaire · Schéma : IMG-teensy-40-intermediaire-5-teensy40-controleur-midi-usb.svg — Teensy 4.0 relié par USB à un ordinateur affichant un DAW ; potentiomètre sur A0, encart « Tools > USB Type : MIDI », bulles note ON / note OFF vers la piste logicielle.

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

Procedure

  1. Tools > USB Type : MIDI BEFORE uploading — this is the step everyone forgets.
  2. Upload the sketch below, unplug/replug the USB if needed (no hardware reset on the 4.0).
  3. 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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials