Projects / Mini MIDI keyboard

Mini MIDI keyboard

Intermediate4 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. ARDUINO

    Plan the matrix

    Place the 25 switches in a 5 by 5 grid on the perfboard.

  2. ARDUINO

    Add diodes

    Solder one diode per switch with the bar towards the row.

  3. ARDUINO

    Wire rows and columns

    Join rows and columns with wire and run them to the Micro pins.

  4. ARDUINO

    Test with a serial print

    Upload a simple scan sketch first and print the key index.

  5. ARDUINO

    Upload the MIDI sketch

    Open a MIDI monitor and play keys.

  6. ARDUINO

    Try a chord

    Hold three keys at once and check all three notes sound.

  7. ARDUINO

    Test the octave buttons

    Press up and down and check the note numbers move by 12.

  8. ARDUINO

    Test pitch bend

    Turn the pot and watch the bend value change.

In short

How do you build a mini MIDI keyboard?

Twenty-five mechanical switches in a diode matrix become a two-octave USB MIDI keyboard with an octave shift. You need Arduino Micro, Mechanical keyboard switch (MX style) × 25, Signal Diode 1N4148 × 25, Tactile push button 6 mm × 2, Potentiometer 10 kΩ and Perfboard (dot PCB) 7 × 9 cm. It takes 8 steps and about 240 minutes.

Level
Intermediate
Time
About 240 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Row lines 0 to 4Arduino Micro pins D2, D3, D4, D5, D6Scan outputs
Column lines 0 to 4Arduino Micro pins D7, D8, D9, D10, D11Inputs with pull-ups
Each key switchOne side to its column line, other side to a diode anodeKey in the matrix
Each 1N4148 diodeAnode to its switch, cathode (bar) to its row lineStops ghost notes
Octave down buttonD12 and GNDShifts down 12 notes
Octave up buttonA0 and GNDShifts up 12 notes
Pitch-bend potentiometer wiperA1, ends on 5V and GNDPitch bend

Code

Each diode must point from the switch towards the row line, with the bar (cathode) on the row side. The scan pulls one row low at a time and reads the columns; a pressed key connects its column to that row through the diode. The diodes let you hold chords without ghost notes. Notes start at C3 (note 48) and the octave buttons move them by twelve. The Micro appears as a USB MIDI device, so no driver is needed; open any DAW or a free MIDI monitor and play. Arduino IDE, board "Arduino Micro".

Runs on a virtual Arduino Leonardo in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Library: MIDIUSB (Library Manager: "MIDIUSB"). Board: Arduino Micro.
#include <MIDIUSB.h>

const byte ROWS = 5, COLS = 5;
const byte rowPins[ROWS] = {2, 3, 4, 5, 6};
const byte colPins[COLS] = {7, 8, 9, 10, 11};
const byte DOWN_BTN = 12, UP_BTN = A0, BEND = A1;

bool state[ROWS * COLS];
byte noteSent[ROWS * COLS];
unsigned long changedAt[ROWS * COLS];
int octave = 0;                    // -2 to +2
int lastBend = 512;

void note(bool on, byte pitch) {
  midiEventPacket_t e = { (byte)(on ? 0x09 : 0x08), (byte)((on ? 0x90 : 0x80) | 0), pitch, (byte)(on ? 100 : 0) };
  MidiUSB.sendMIDI(e);
}

void setup() {
  for (byte r = 0; r < ROWS; r++) { pinMode(rowPins[r], OUTPUT); digitalWrite(rowPins[r], HIGH); }
  for (byte c = 0; c < COLS; c++) pinMode(colPins[c], INPUT_PULLUP);
  pinMode(DOWN_BTN, INPUT_PULLUP);
  pinMode(UP_BTN, INPUT_PULLUP);
}

void loop() {
  for (byte r = 0; r < ROWS; r++) {
    digitalWrite(rowPins[r], LOW);
    delayMicroseconds(30);
    for (byte c = 0; c < COLS; c++) {
      byte k = r * COLS + c;
      bool pressed = (digitalRead(colPins[c]) == LOW);
      if (pressed != state[k] && millis() - changedAt[k] > 8) {   // 8 ms debounce
        changedAt[k] = millis();
        state[k] = pressed;
        if (pressed) { noteSent[k] = 48 + k + 12 * octave; note(true, noteSent[k]); }
        else { note(false, noteSent[k]); }   // release the note that was pressed
      }
    }
    digitalWrite(rowPins[r], HIGH);
  }

  static bool downWas = false, upWas = false;
  bool d = (digitalRead(DOWN_BTN) == LOW), u = (digitalRead(UP_BTN) == LOW);
  if (d && !downWas && octave > -2) octave--;
  if (u && !upWas && octave < 2) octave++;
  downWas = d; upWas = u;

  int b = analogRead(BEND);
  if (abs(b - lastBend) > 6) {
    lastBend = b;
    int v = map(b, 0, 1023, 0, 16383);
    midiEventPacket_t e = {0x0E, 0xE0, (byte)(v & 0x7F), (byte)(v >> 7)};
    MidiUSB.sendMIDI(e);
  }
  MidiUSB.flush();
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
One keyOne note on and one off
Three keys at onceThree notes, no extra ghost notes
Octave upNotes 12 higher
Pot turnedPitch bend follows

If it does not work

Keys give the wrong note

Check the row and column order against the pin arrays.

A key never registers

Check the diode direction for that key.

Ghost notes

A diode is missing or reversed.

Notes hang

Check the octave was not changed while a key was held; release first.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the mini MIDI keyboard?

Arduino Micro, Mechanical keyboard switch (MX style) × 25, Signal Diode 1N4148 × 25, Tactile push button 6 mm × 2, Potentiometer 10 kΩ and Perfboard (dot PCB) 7 × 9 cm.

How long does the mini MIDI keyboard take to build?

About 240 minutes for a intermediate maker, in 8 steps.

How do I know the mini MIDI keyboard works?

One key: One note on and one off. Three keys at once: Three notes, no extra ghost notes. Octave up: Notes 12 higher.

Keys give the wrong note: what should I check?

Check the row and column order against the pin arrays.

A key never registers: what should I check?

Check the diode direction for that key.

Ghost notes: what should I check?

A diode is missing or reversed.

Notes hang: what should I check?

Check the octave was not changed while a key was held; release first.