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: Difference between revisions

From I/M/D Wiki
Nikilia (talk | contribs)
No edit summary
Nikilia (talk | contribs)
Replaced content with "thumb|PDF"
Tags: Replaced Visual edit
 
Line 1: Line 1:
<nowiki>##</nowiki> Setup
[[File:Arduino to MIDI guide.pdf|thumb|PDF]]
 
1. Install MIDIUSB library <nowiki>https://docs.arduino.cc/libraries/midiusb/</nowiki>
 
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
 
<nowiki>###</nowiki> What you need:
 
- 1 potentiometer or slider
 
- 3 wires
 
<nowiki>###</nowiki> 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);
}
 
 
 
<nowiki>##</nowiki> 🔘 Note  button
 
<nowiki>###</nowiki> What you need
 
- 1 push button
 
- 2 wires
 
<nowiki>###</nowiki> 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);
}
 
 
<nowiki>###</nowiki> ☎️ 2 potmeters 2 buttons code
 
```cpp
 
<nowiki>#</nowiki>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);
 
}
 
```

Latest revision as of 09:48, 21 January 2026