Arduino to MIDI
From I/M/D Wiki
More actions
## 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);
}