Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Arduino to MIDI

From I/M/D Wiki
Revision as of 14:42, 19 January 2026 by Nikilia (talk | contribs)

## Setup

1. Install MIDIUSB library https://docs.arduino.cc/libraries/midiusb/

2. Add the MIDIUSB.h Library: Go to Tools < Manage Libraries...

3. Search for "MIDIUSB" by Gary Grewal

4. Install the latest version

5. Select your board: Tools > Board > Arduino Leonardo (or Pro Micro)


πŸŽ›οΈπŸŽšοΈ Potentiometers / Sliders

### What you need:

- 1 potentiometer or slider

- 3 wires

### How to connect:

```

Potentiometer:

β”œβ”€ Left pin / GNDΒ  Β β†’ GND

β”œβ”€ Middle pin / Signal β†’ A0 (or A1, A2, A3...)

└─ Right pin / VCC / PowerΒ  β†’ 5V

#include <MIDIUSB.h>

int lastValue = 0;

void setup() {
  pinMode(A0, INPUT);  // ← Change A0 to your pin
}

void loop() {
  int value = analogRead(A0) / 8;  // ← Change A0 to match setup

  if (valueΒ != lastValue) {
    midiEventPacket_t event = {0x0B, 0xB0, 1, value};
    //                                      ↑ ← CC number
    MidiUSB.sendMIDI(event);
    MidiUSB.flush();
    lastValue = value;
  }

  delay(10);
}


## πŸ”˜ NoteΒ  button

### What you need

- 1 push button

- 2 wires

### How to connect

```

Button:

β”œβ”€ One sideΒ  β†’ Pin 2 (or 3, 4, 5...)

└─ Other side β†’ GND

```

#include <MIDIUSB.h>

int lastState = HIGH;

void setup() {
  pinMode(2, INPUT_PULLUP);  // ← Change 2 to your pin
}

void loop() {
  int state = digitalRead(2);  // ← Change 2 to match setup

  if (stateΒ != lastState) {
    if (state == LOW) {
      // Button pressed
      midiEventPacket_t noteOn = {0x09, 0x90, 60, 127};
      //                                       ↑   ↑
      //                                    note velocity
      MidiUSB.sendMIDI(noteOn);
    } else {
      // Button released
      midiEventPacket_t noteOff = {0x08, 0x80, 60, 0};
      //                                        ↑
      //                                      note
      MidiUSB.sendMIDI(noteOff);
    }
    MidiUSB.flush();
    lastState = state;
  }

  delay(10);
}


### ☎️ 2 potmeters 2 buttons code

```cpp

#include <MIDIUSB.h>

int lastValueA0 = 0;

int lastValueA1 = 0;

int lastState2 = HIGH;

int lastState3 = HIGH;

void setup() {

Β  pinMode(A0, INPUT);Β  Β  Β  Β  Β // Pot 1

Β  pinMode(A1, INPUT);Β  Β  Β  Β  Β // Pot 2

Β  pinMode(2, INPUT_PULLUP);Β  Β // Button 1

Β  pinMode(3, INPUT_PULLUP);Β  Β // Button 2

}

void loop() {

Β  // ══════════════════════════════════════════════════════════

Β  // POTENTIOMETER 1 (A0) β†’ CC 1

Β  // ══════════════════════════════════════════════════════════

Β  int valueA0 = analogRead(A0) / 8;

Β  if (valueA0Β != lastValueA0) {

Β  Β  midiEventPacket_t event = {0x0B, 0xB0, 60, valueA0};

Β  Β  MidiUSB.sendMIDI(event);

Β  Β  MidiUSB.flush();

Β  Β  lastValueA0 = valueA0;

Β  }

Β 

Β  // ══════════════════════════════════════════════════════════

Β  // POTENTIOMETER 2 (A1) β†’ CC 2

Β  // ══════════════════════════════════════════════════════════

Β  int valueA1 = analogRead(A1) / 8;

Β  if (valueA1Β != lastValueA1) {

Β  Β  midiEventPacket_t event = {0x0B, 0xB0, 61, valueA1};

Β  Β  MidiUSB.sendMIDI(event);

Β  Β  MidiUSB.flush();

Β  Β  lastValueA1 = valueA1;

Β  }

Β 

Β  // ══════════════════════════════════════════════════════════

Β  // BUTTON 1 (Pin 2) β†’ Note 60 (Middle C)

Β  // ══════════════════════════════════════════════════════════

Β  int state2 = digitalRead(2);

Β  if (state2Β != lastState2) {

Β  Β  if (state2 == LOW) {

Β  Β  Β  midiEventPacket_t noteOn = {0x09, 0x90, 60, 127};

Β  Β  Β  MidiUSB.sendMIDI(noteOn);

Β  Β  } else {

Β  Β  Β  midiEventPacket_t noteOff = {0x08, 0x80, 60, 0};

Β  Β  Β  MidiUSB.sendMIDI(noteOff);

Β  Β  }

Β  Β  MidiUSB.flush();

Β  Β  lastState2 = state2;

Β  }

Β 

Β  // ══════════════════════════════════════════════════════════

Β  // BUTTON 2 (Pin 3) β†’ Note 61 (C#)

Β  // ══════════════════════════════════════════════════════════

Β  int state3 = digitalRead(3);

Β  if (state3Β != lastState3) {

Β  Β  if (state3 == LOW) {

Β  Β  Β  midiEventPacket_t noteOn = {0x09, 0x90, 61, 127};

Β  Β  Β  MidiUSB.sendMIDI(noteOn);

Β  Β  } else {

Β  Β  Β  midiEventPacket_t noteOff = {0x08, 0x80, 61, 0};

Β  Β  Β  MidiUSB.sendMIDI(noteOff);

Β  Β  }

Β  Β  MidiUSB.flush();

Β  Β  lastState3 = state3;

Β  }

Β 

Β  delay(10);

}

```