midi-piano.ino

Teensy 4.0 · File : midi-piano.ino · Language : Arduino · Use case : Envoyer des notes MIDI sur USB (Teensy reconnu comme instrument)

The Teensy enumerates as a USB MIDI instrument with no extra circuit: three buttons play a C major chord received straight by a DAW (Ardour, LMMS, GarageBand). The core MIDI library handles the whole USB class.

Hardware

A Teensy 4.0 (NXP i.MX RT1062, Cortex-M7 @ 600 MHz, 3.3 V logic) powered over USB. 3 push buttons (to GND).

Wiring

One button per pin (2, 3, 4) to GND, internal PULLUP. USB cable to the computer.

How it works

MIDI.sendNoteOn/Off emits MIDI messages over USB (USB MIDI key). The Arduino IDE MIDI monitor or a DAW displays them; Serial stays free for debugging.

Running it

With Teensyduino in the Arduino IDE, select « Teensy 4.0 », Upload then confirm in the Teensy Loader. Pins are 3.3 V; the 4.0 is pin-compatible with the 3.2.

Variants

Add a potentiometer for volume (Control Change), or drive the Teensy Audio library to synthesize sound on board.

midi-piano.ino

// Piano 3 touches : le Teensy est reconnu comme peripherique MIDI USB.
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

const int BROCHES[3] = {2, 3, 4}; // touches vers GND
const int NOTES[3] = {60, 64, 67}; // do, mi, sol
bool enfonce[3] = {false, false, false};

void setup() {
  for (int i = 0; i < 3; i++) pinMode(BROCHES[i], INPUT_PULLUP);
  MIDI.begin(MIDI_CHANNEL_OMNI);
  Serial.begin(9600); // le MIDI passe par l'USB, Serial reste libre
  Serial.println(F("MIDI pret (voir le moniteur MIDI de l'IDE)"));
}

void loop() {
  for (int i = 0; i < 3; i++) {
    bool appui = digitalRead(BROCHES[i]) == LOW;
    if (appui && !enfonce[i]) {
      enfonce[i] = true;
      MIDI.sendNoteOn(NOTES[i], 100, 1); // note, vélocité, canal
      Serial.print(F("noteOn ")); Serial.println(NOTES[i]);
    } else if (!appui && enfonce[i]) {
      enfonce[i] = false;
      MIDI.sendNoteOff(NOTES[i], 0, 1);
      Serial.print(F("noteOff ")); Serial.println(NOTES[i]);
    }
  }
}

← Back to the board