Arduino to MIDI: Difference between revisions
More actions
Created page with "# Arduino to MIDI guide ## 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..." |
No edit summary |
||
| Line 1: | Line 1: | ||
# Arduino to MIDI guide | <nowiki>#</nowiki> Arduino to MIDI guide | ||
## Setup | <nowiki>##</nowiki> Setup | ||
1. Install MIDIUSB library <nowiki>https://docs.arduino.cc/libraries/midiusb/</nowiki> | |||
2. Add the MIDIUSB.h Library: Go to Tools < Manage Libraries... | 2. Add the MIDIUSB.h Library: Go to Tools < Manage Libraries... | ||
3. Search for "MIDIUSB" by Gary Grewal | 3. Search for "MIDIUSB" by Gary Grewal | ||
4. Install the latest version | 4. Install the latest version | ||
5. Select your board: Tools > Board > Arduino Leonardo (or Pro Micro) | 5. Select your board: Tools > Board > Arduino Leonardo (or Pro Micro) | ||
## 🎛️🎚️ Potentiometers / Sliders | <nowiki>##</nowiki> 🎛️🎚️ Potentiometers / Sliders | ||
### What you need: | <nowiki>###</nowiki> What you need: | ||
- 1 potentiometer or slider | - 1 potentiometer or slider | ||
- 3 wires | - 3 wires | ||
### How to connect: | <nowiki>###</nowiki> How to connect: | ||
``` | ``` | ||
Potentiometer: | Potentiometer: | ||
├─ Left pin / GND | |||
├─ Left pin / GND → GND | |||
├─ Middle pin / Signal → A0 (or A1, A2, A3...) | ├─ Middle pin / Signal → A0 (or A1, A2, A3...) | ||
└─ Right pin / VCC / | |||
└─ Right pin / VCC / Power → 5V | |||
``` | ``` | ||
### Code | <nowiki>###</nowiki> Code | ||
```cpp | ```cpp | ||
#include <MIDIUSB.h> | |||
<nowiki>#</nowiki>include <MIDIUSB.h> | |||
int lastValue = 0; | int lastValue = 0; | ||
void setup() { | void setup() { | ||
pinMode(A0, INPUT); // ← Change A0 to your pin | |||
} | } | ||
void loop() { | 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); | |||
} | } | ||
``` | ``` | ||
**To customize:** | <nowiki>**</nowiki>To customize:** | ||
- Change A0 to A1, A2, A3 etc. for different pins | - Change A0 to A1, A2, A3 etc. for different pins | ||
- Change the CC number 1 a different one for every input | - Change the CC number 1 a different one for every input | ||
<nowiki>##</nowiki> 🔘 Note button | |||
## | <nowiki>###</nowiki> What you need | ||
- 1 push button | |||
- 2 wires | - 2 wires | ||
### How to connect | <nowiki>###</nowiki> How to connect | ||
``` | ``` | ||
Button: | Button: | ||
├─ One | |||
├─ One side → Pin 2 (or 3, 4, 5...) | |||
└─ Other side → GND | └─ Other side → GND | ||
``` | ``` | ||
### Code | <nowiki>###</nowiki> Code | ||
```cpp | ```cpp | ||
#include <MIDIUSB.h> | |||
<nowiki>#</nowiki>include <MIDIUSB.h> | |||
int lastState = HIGH; | int lastState = HIGH; | ||
void setup() { | void setup() { | ||
pinMode(2, INPUT_PULLUP); // ← Change 2 to your pin | |||
} | } | ||
void loop() { | 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); | |||
} | } | ||
``` | ``` | ||
**To customize:** | <nowiki>**</nowiki>To customize:** | ||
- Change pin `2` to `3`, `4`, `5` etc. | - Change pin `2` to `3`, `4`, `5` etc. | ||
- Change note number `60`: | - 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) | - 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 | - Wires as needed for your sensor | ||
### How to connect: | <nowiki>###</nowiki> How to connect: | ||
``` | ``` | ||
Sensor: | Sensor: | ||
├─ VCC/Power → 5V | ├─ VCC/Power → 5V | ||
├─ GND | |||
└─ | ├─ GND → GND | ||
└─ Signal → A0 (or A1, A2, A3...) | |||
``` | ``` | ||
### Code to copy: | <nowiki>###</nowiki> Code to copy: | ||
```cpp | ```cpp | ||
#include <MIDIUSB.h> | |||
<nowiki>#</nowiki>include <MIDIUSB.h> | |||
int lastValue = 0; | int lastValue = 0; | ||
void setup() { | void setup() { | ||
pinMode(A0, INPUT); // ← Change A0 to your pin | |||
} | } | ||
void loop() { | 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); | |||
} | } | ||
``` | ``` | ||
### To customize: | <nowiki>###</nowiki> To customize: | ||
- Change `A0` to your sensor pin | - Change `A0` to your sensor pin | ||
- Change `0, 1023` in the `map()` function if your sensor gives different values | - Change `0, 1023` in the `map()` function if your sensor gives different values | ||
- Change CC number `74` to control different parameters | - 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` | `A0`, `A1`, `A2`, `A3`, `A4`, `A5` | ||
### Digital Pins (for buttons): | <nowiki>###</nowiki> Digital Pins (for buttons): | ||
`2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13` | `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13` | ||
### Don't use: | <nowiki>###</nowiki> Don't use: | ||
- Pin `0` and `1` (used for USB communication) | - Pin `0` and `1` (used for USB communication) | ||
## Sending notes | <nowiki>##</nowiki> Sending notes | ||
```cpp | ```cpp | ||
C4 = 60 (Middle C) | |||
C#4 = 61 | C#4 = 61 | ||
D4 = 62 | |||
D#4 = 63 | D#4 = 63 | ||
E4 = 64 | |||
F4 = 65 | |||
F#4 = 66 | F#4 = 66 | ||
G4 = 67 | |||
G#4 = 68 | G#4 = 68 | ||
A4 = 69 | |||
A#4 = 70 | A#4 = 70 | ||
B4 = 71 | |||
C5 = 72 | |||
``` | ``` | ||
| Line 214: | Line 308: | ||
- C3 = 48 (one octave lower) | - C3 = 48 (one octave lower) | ||
- C5 = 72 (one octave higher) | - C5 = 72 (one octave higher) | ||
## CC | <nowiki>##</nowiki> CC | ||
You can use any CC from 0-119, but these are the most common! | You can use any CC from 0-119, but these are the most common! | ||
Revision as of 12:32, 19 January 2026
# Arduino to MIDI guide
## 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
```
### Code
```cpp
#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);
}
```
**To customize:**
- Change A0 to A1, A2, A3 etc. for different pins
- Change the CC number 1 a different one for every input
## 🔘 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
```
### Code
```cpp
#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);
}
```
**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)
-
## 📏🪢 Pull sensor / distance sensor
### What you need:
- Any sensor with analog output
- Wires as needed for your sensor
### How to connect:
```
Sensor:
├─ VCC/Power → 5V
├─ GND → GND
└─ Signal → A0 (or A1, A2, A3...)
```
### Code to copy:
```cpp
#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);
}
```
### 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
### ☎️ CC on/off / Momentary
## 📝 Common Pin Reference
### Analog Pins (for sensors & potentiometers):
`A0`, `A1`, `A2`, `A3`, `A4`, `A5`
### Digital Pins (for buttons):
`2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`
### Don't use:
- Pin `0` and `1` (used for USB communication)
## 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)
## CC
You can use any CC from 0-119, but these are the most common!