Arduino MIDI Basics Part 1 -- MIDI Out

Discussion, tutorials, code, and schematics about do-it-yourself projects related to electronic music.

Arduino MIDI Basics Part 1 -- MIDI Out

Postby chysn » Thu May 07, 2015 2:14 am

The genius of MIDI isn't that it's high-tech. The genius is that it's low-tech. If you have an Arduino microcontroller, you can put together a working MIDI interface with about US $2.50 worth of parts. These include:

2 5-pin DIN jacks (about 70 cents each)
1 high-speed octocoupler (about 90 cents)
2 220-ohm resistors (about 10 cents each)

And, of course, some wire to hook everything up.

The introductory project here is going to be to build and program the simplest possible MIDI sequencer using an Arduino microcontroller. This sequencer will have a MIDI Out only, and will send a random sequence of notes on MIDI channel 1.

Hardware

MIDI.org is a great starting point for the MIDI electrical specification. See Figure 1 here: http://www.midi.org/techspecs/electrispec.php. Since this project has only a MIDI Out, we only need to look at the MIDI Out portion of the diagram at the bottom.

First, make sure you know how your DIN jack is wired. Hopefully your jack's pins will be numbered. If not, it's a pretty safe bet that the pins are numbered--from left to right, when looking at the jack from the rear, and the jack oriented so that the pins in the front are curved like a smiley face--1, 4, 2, 5, 3.

If you don't have a breadboard-friendly jack, you'll need to solder some breadboard jumper wire onto your jack. Pins 1 and 3 are not used with MIDI, so just wire up the middle three pins.

The Arduino hookup of a MIDI Out is simple. Pin 2 goes to the ground of the Arduino board. For this connection, I connected my MIDI jack to the breadboard, and then wired the same row of the breadboard to the Arduino's ground.

Pin 4 goes to the +5V of the Arduino, through one of the 220-ohm resistors.

Pin 5 goes to one of the Digital Transmit (TX) pins of your Arduino, through the other 220-ohm resistor. On an Arduino Uno, this is Digital 1. Arduino Mega has more than one TX pin, but use the lowest-numbered one for this project. MIDI uses the Arduino's serial I/O, so you need to use one of the serial TX pins.

MIDI_Out.png
MIDI_Out.png (19.96 KiB) Viewed 826 times


Go ahead and grab a MIDI cable and connect your MIDI Out to the MIDI In of a synth. Make sure the synth will respond to notes on Channel 1.

Software

Here's the sketch. If you send this to your Arduino, and everything is hooked up right, your synth should start playing a random tune.

Code: Select all
void setup() {
  Serial.begin(31250); /* MIDI 31250 baud */
}

void loop() {
  int note = random(50, 100);
  int vel = random(60, 127);
  int dur = random(2, 16) * 25;
  int del = random(4, 20) * 10;
 
  MIDInote(note, vel);
  delay(dur);
  MIDInote(note, 0);
  delay(del); 
}

/* Send a MIDI note-on message on channel 1 */
void MIDInote(int note, int vel) {
  Serial.write(0x90); // Note on, channel 1
  Serial.write(note);
  Serial.write(vel);  // Note off = 0
}


The heart of this code is the MIDInote function, which uses the Arduino's serial I/O to send a note message. This is exceedingly simple, with no error checking on the value ranges, and only a single MIDI command on a single channel. If you're reading this page, you probably have your own ideas about what sort of MIDI data you want to send, and it's a pretty trivial task to send other kinds of messages. In future posts, we'll work through the process of generalizing software to construct MIDI messages.

If you have any questions or problems, go ahead and post here.
DSI: Evolver #1431
Other Synths: Moog Little Phatty Stage II (Red), Arturia MicroBrute
Other Hardware: Alesis MMT-8, Korg Volca Beats
DAW: Reaper for OSX through PreSonus AudioBox USB
chysn
Site Admin
 
Posts: 256
Joined: Sun Jun 22, 2014 12:58 am
Location: Metro Detroit, Michigan, USA

Return to Do It Yourself



Who is online

Users browsing this forum: No registered users and 1 guest

cron