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)
No edit summary
Line 1: Line 1:
<nowiki>#</nowiki> Arduino to MIDI guide
<nowiki>##</nowiki> Setup
<nowiki>##</nowiki> Setup


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


<nowiki>##</nowiki> 🎛️🎚️ Potentiometers / Sliders
 
🎛️🎚️ Potentiometers / Sliders


<nowiki>###</nowiki> What you need:
<nowiki>###</nowiki> What you need:
Line 32: Line 31:


└─ Right pin / VCC / Power  → 5V
└─ 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> Code
```cpp
<nowiki>#</nowiki>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>To customize:**
- Change A0 to A1, A2, A3 etc. for different pins
- Change the CC number 1 a different one for every input


<nowiki>##</nowiki> 🔘 Note  button
<nowiki>##</nowiki> 🔘 Note  button
Line 99: Line 75:
```
```


<nowiki>###</nowiki> Code
#include <MIDIUSB.h>
 
```cpp
int lastState = HIGH;
 
<nowiki>#</nowiki>include <MIDIUSB.h>
void setup() {
 
  pinMode(2, INPUT_PULLUP); // ← Change 2 to your pin
int lastState = HIGH;
}
 
void setup() {
void loop() {
 
  int state = digitalRead(2); // ← Change 2 to match setup
  pinMode(2, INPUT_PULLUP);  // ← Change 2 to your pin
 
  if (state != lastState) {
}
    if (state == LOW) {
 
      // Button pressed
void loop() {
      midiEventPacket_t noteOn = {0x09, 0x90, 60, 127};
 
      //                                       ↑  ↑
  int state = digitalRead(2);  // ← Change 2 to match setup
      //                                   note velocity
 
      MidiUSB.sendMIDI(noteOn);
  if (state != lastState) {
    } else {
 
      // Button released
    if (state == LOW) {
      midiEventPacket_t noteOff = {0x08, 0x80, 60, 0};
 
      //                                      
      // Button pressed
      //                                     note
 
      MidiUSB.sendMIDI(noteOff);
      midiEventPacket_t noteOn = {0x09, 0x90, 60, 127};
    }
 
    MidiUSB.flush();
      //                                       ↑   ↑
    lastState = state;
 
  }
      //                                    note velocity
 
  delay(10);
      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>To customize:**
 
- Change pin `2` to `3`, `4`, `5` etc.
 
- Change note number `60`:
 
  - `60` = C (middle C)
 
  - `62` = D
 
  - `64` = E
 
  - `65` = F
 
  - `67` = G
 
  - `69` = A
 
  - `71` = B
 
  - `72` = C (one octave higher)
 
- Change velocity `100` (0-127, higher = louder)
 
-
 
<nowiki>##</nowiki> 📏🪢 Pull sensor / distance sensor
 
<nowiki>###</nowiki> What you need:
 
- Any sensor with analog output
 
- Wires as needed for your sensor
 
<nowiki>###</nowiki> How to connect:
 
```
 
Sensor:
 
├─ VCC/Power → 5V
 
├─ GND       → GND
 
└─ Signal    → A0 (or A1, A2, A3...)
 
```
 
<nowiki>###</nowiki> Code to copy:
 
```cpp
 
<nowiki>#</nowiki>include <MIDIUSB.h>
 
int lastValue = 0;
 
void setup() {
 
  pinMode(A0, INPUT);  // ← Change A0 to your pin
 
}
 
void loop() {
 
  int sensorValue = analogRead(A0);  // ← Change A0 to match setup
 
  int midiValue = map(sensorValue, 0, 1023, 0, 127);
 
  //                               ↑   ↑
 
  //                         adjust these if needed
 
  if (abs(midiValue - lastValue) > 1) {  // Ignore tiny changes
 
    midiEventPacket_t event = {0x0B, 0xB0, 74, midiValue};
 
    //                                      ↑ ← CC number
 
    MidiUSB.sendMIDI(event);
 
    MidiUSB.flush();
 
    lastValue = midiValue;
 
  }
 
  delay(10);
 
}
 
```
 
<nowiki>###</nowiki> To customize:
 
- Change `A0` to your sensor pin
 
- Change `0, 1023` in the `map()` function if your sensor gives different values
 
- Change CC number `74` to control different parameters
 
<nowiki>###</nowiki> ☎️ CC on/off / Momentary
 
<nowiki>##</nowiki> 📝 Common Pin Reference
 
<nowiki>###</nowiki> Analog Pins (for sensors & potentiometers):
 
`A0`, `A1`, `A2`, `A3`, `A4`, `A5`
 
<nowiki>###</nowiki> Digital Pins (for buttons):
 
`2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`
 
<nowiki>###</nowiki> Don't use:
 
- Pin `0` and `1` (used for USB communication)
 
<nowiki>##</nowiki> Sending notes
 
```cpp
 
C4  = 60  (Middle C)
 
C#4 = 61
 
D4  = 62
 
D#4 = 63
 
E4  = 64
 
F4  = 65
 
F#4 = 66
 
G4  = 67
 
G#4 = 68
 
A4  = 69
 
A#4 = 70
 
B4  = 71
 
C5  = 72
 
```
 
Each octave adds or subtracts 12:
 
- C3 = 48 (one octave lower)
 
- C5 = 72 (one octave higher)
 
<nowiki>##</nowiki> CC
 
You can use any CC from 0-119, but these are the most common!

Revision as of 12:35, 19 January 2026

## 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);
}