Arduino MIDI controllers - instructions

tags

Update as of 2/19/17: images are missing from this post, but will be replaced!

If you're interested in how using Arduino for MIDI works, here's how to make the simple controller I described in the previous post. These instructions are based on a number of tutorials, especially Arduino, Sensors, and MIDI. There are many methods of setting up an Arduino for MIDI, but this is what I found to be the most straightforward.

Wiring

First, hook up your Arduino as follows. The squiggly thing in the lower left is the photoresistor, used for pitchbend, and the large circle at the bottom is the push button for note on/off. Red and black cables are for power and ground, while the yellow cables are for sending input values to the Arduino (digital in 7 for the push button, analog in 0 for the photoresistor).

circuit sketch

Code

Arduino boards run code written in a modified version of Wiring, which was based on Processing. Copy the following code into the Arduino IDE (adapted from this tutorial):

int velocity = 100;
int noteON = 144;
int pitchBEND = 224;
const int BUTTON = 7;
const int SENSOR = A0;
int val = 0;

int old_val = 0;
int state = 0;
int old_state = 0;
int note = 50;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON, INPUT); 
}

void loop() {
  int lsr = analogRead(SENSOR);
  int bend_val = map(lsr, 0, 1023, 0, 127);
 
  old_val = val;
  val = digitalRead(BUTTON);
  old_state = state;
  if (val == HIGH) {
    state = 1;
  }
  else {
    state = 0;
  }
  delay(10);
  if (state == 1 && old_val == 0) {
    MIDImessage(noteON, note, velocity);
    delay(20);
  }
  else if (state == 1 && old_val == 1) {
    MIDImessage(pitchBEND, 0, bend_val);
    delay(20);
  }
  else if (old_state == 1 && state == 0) {
    MIDImessage(noteON, note, 0);
    delay(20);
  }
}

void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  Serial.write(command);
  Serial.write(MIDInote);
  Serial.write(MIDIvelocity);
}

Verify the code and upload it to your Arduino via the USB connection. Once it's uploaded without errors, it's time for some serial trickery!

Interpreting Serial as MIDI

There are a number of different ways to send the serial information and have your computer interpret it as MIDI, with pros and cons to each. The easiest method is the one I'm using, and the most commonly used in MIDI controller tutorials.

  1. Install a MIDI <-> Serial bridge program, like Hairless. This will interpret serial information coming from a given USB port and output correctly formatted MIDI messages through the selected MIDI output channel. This is easy, and the download requirement is minimal. However, you need to have Hairless running any time you want to use your device, and on low-grade PCs like mine, Hairless will crash if you accidentally send too many MIDI messages at an inhumanly fast rate (happened during testing).
  2. Deconstruct a MIDI -> USB adapter cable, detach all its cable, and use the board to do the converting for you. This requires extra parts, including the MIDI converter, and an extra USB outlet to put in your breadboard. This is a pretty good permanent solution, but in the short term, it costs more money to test and has more moving parts if you're still just starting out.
  3. Write custom drivers. (Not my wheelhouse!)

Install Hairless, and check that the baud rate is set to 9600 in the Preferences. Select the Arduino as your Serial port, and select your desired MIDI out port. MIDI out information depends on your OS. If you're on Windows, the only default MIDI out option will be the Microsoft Wavetable Synth. This is fine for initial tests, but isn't a channel you can select in a DAW. To set new MIDI channels, I installed loopMIDI, which lets you create new virtual MIDI channels that last as long as you are logged in. On Mac, most tutorials say to send MIDI information to IAC Driver Bus 1.

Test!

Open up your desired DAW, and give it a try. The code provided will pitch your note up a half step on each button press; if the pitches get annoyingly high, press the red RESET button on the Arduino board. Changing the light levels in your room or cupping your hands over the photoresistor will alter the pitchbend values sent. The below screenshot shows Ableton with Hairless and loopMIDI up for reference.

circuit screenshot

Written by Becky on Thursday January 29, 2015

Permalink - Category: programming - Tags: Arduino, MIDI controllers

« Easel controller: some background - Working paper pitchbend! »

Related content