How to make a MIDI output using a microcontroller
Posted: Fri Jun 24, 2011 7:00 am
This tutorial explores the basics of MIDI (Musical Instrument Digital Interface) communication. MIDI was developed in early 1980's to be an industry standard protocol to be used for musical instruments, computers and other devices to communicate. MIDI's use has continued to expand and is now used not only as the standard protocol for musical instruments, but for applications such as cell phone ring tones and lighting control systems.
A standard MIDI message (following General MIDI or GM Standard) will contain a Status Byte followed by either one or two Data Bytes (Status, Data 1, Data 2). Following are some of the most common MIDI messages and are the ones used in this example:
note on number channel 1(0x90), note number (0-127), velocity (0-127)
note off number channel 1(0x80), note number (0-127), velocity (0-127)
program change number channel 1(0xC0), instrument number (0-127)
In this example, we connect the MIDI standard 5-Pin DIN connector from our Arduino circuit (MIDI OUT) to the MIDI IN of a standard sound module (we use an Alesis Nanosynth, but any standard MIDI sound module will work.)
Courtecy of UMASS AMHERST
A standard MIDI message (following General MIDI or GM Standard) will contain a Status Byte followed by either one or two Data Bytes (Status, Data 1, Data 2). Following are some of the most common MIDI messages and are the ones used in this example:
note on number channel 1(0x90), note number (0-127), velocity (0-127)
note off number channel 1(0x80), note number (0-127), velocity (0-127)
program change number channel 1(0xC0), instrument number (0-127)
In this example, we connect the MIDI standard 5-Pin DIN connector from our Arduino circuit (MIDI OUT) to the MIDI IN of a standard sound module (we use an Alesis Nanosynth, but any standard MIDI sound module will work.)
Code: Select all
//MIDI OUTPUT (SERIAL)
//GENERATE MIDI DATA USING RANDOM TO DEMONSTRATE
//SENDING MIDI DATA USING THE ARDUINO SERIAL OUT
//AND THE USE OF THE RANDOM FUNCTION
//DECLARE VARIABLES
int note = 0;
void setup() {
//SET BAUD RATE FOR MIDI
Serial.begin(31250);
}
void loop (){
//USE RANDOM FUNCTION TO SELECT NOTES BETWEEN C1 AND C7
//NOTE C = MIDI NOTE NUMBER 12
//NOTE C7 = MIDI NOTE NUMBER 96
note = random(12,96);
// SEND THE MIDI COMMAND TO PLAY A NOTE ON CHANNEL 1 (0X90), PLAY NOTE VALUE (note), HIGH VELOCITY
sendMidiNote (0x90, note, 0x7F);
delay (50); //LEAVE NOTE ON FOR 50 MILLISECS
//SEND THE MIDI NOTE OFF COMMAND, NOTE PREVIOUSLY PLAYED, HIGH VELOCITY
sendMidiNote (0x80, note, 0x7F);
delay (50); //REST FOR 50 MILLISECS
}
//THIS FUNCTION PLAYS A MIDI NOTE WHEN midiCommand = 0x90
//THIS FUNCTION TURNS OFF THE NOTE WHEN midiCommand = 0x80
void sendMidiNote (byte midiCommand, byte noteValue, byte velocityValue){
Serial.print(midiCommand, BYTE);
Serial.print(noteValue, BYTE);
Serial.print(velocityValue, BYTE);
}