Tuesday, September 08, 2009

Multiplexing MIDI Output Streams with Arduino

Overview
Arduino can easily generate MIDI output data for use with external synths and modules or to take physical events and turn them into control data for use with software synths and applications etc.

Although the Arduino only has one Serial output, it is still possible to use a multiplexer to send multiple streams of MIDI data to multiple devices.

The idea is pretty simple. The Arduino still uses its TX pin for sending serial data, but uses an analog multiplexer to choose where that data is going (say, one of two MIDI outputs for example). The analog multiplexer that I have chosen is a 4051 and can 'route' the data to up to eight different places.


Hardware
The hardware side is very basic - RX goes to the common in/out on the multiplexer, and out/ins 0 and 1 are connected to two MIDI outputs. Digital pins 8, 9 and 10 control the 4051's address pins.

In the above schematic, PORTD refers to Arduino pins 0 to 7. PORTB refers to Arduino 8 to 15. Naturally, the Arduino boards still require a 5V power source (not shown in the schematic).

The above schematic can be easily adapted to accommodate up to eight MIDI outputs simply by adding additional DIN-5 sockets and mimicking the pin destinations of the two that are already shown.


Software
Here is some very basic Arduino code to test out the above schematic. Naturally, functions that goup together MIDI functions nested within functions that handle the multiplexing can be added to simplify the code for more complex operations.
void setup() {
Serial.begin(31250);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}

void loop() {
PORTB = 0;
Serial.print(0x90, BYTE);
Serial.print(60, BYTE);
Serial.print(127, BYTE);

delay(500);

Serial.print(0x90, BYTE);
Serial.print(60, BYTE);
Serial.print(0, BYTE);
delay(500);

PORTB = 1;
Serial.print(0x90, BYTE);
Serial.print(67, BYTE);
Serial.print(127, BYTE);

delay(500);

Serial.print(0x90, BYTE);
Serial.print(67, BYTE);
Serial.print(0, BYTE);
delay(500);
}

2 comments:

rcaceres said...

Good stuff. If you are really ambitious you can hook up 8 more multiplexers into the first multiplexer and get 64 outputs.

My question is what are some applications for having multiple midi outputs from an arduino?

El Artista said...

By sample if you need to manage more than 16 midi chanell in your performance or project.