We can use Arduino for processing MIDI input and use Virtual MIDI Piano Keyboard freeware on PC for testing purpose. MIDI controller is hardware or software which generates and transmits Musical Instrument Digital Interface (MIDI) data to electronic or digital MIDI-enabled device. We can directly connect the MIDI jack with Arduino with minimally complex wiring or use IC. We can program, create circuit to blink the LED connected to Pin 13 on the Arduino Board upon press of a note on a Midi Keyboard to confirm that we are receiving MIdI commands with Arduino.
MIDI is a micro-to-micro digital communication bus. MIDI ues a circular 5-pin DIN connector which could not be misconnected. While the connectors have five pins, only three of them are used. Two MIDI cables are connected in the following manner :
Pin 1 — > no Connection — > Pin 1
Pin 2 — > shield — > Pin 2
Pin 3 — > no Connection — > Pin 3
Pin 4 — > Voltage Reference Line — > Pin 4
Pin 5 — > Data Line — > Pin 5
---
The MIDI specification recommendeds a circuit for the ports and calls for an opto-isolator. The reference design specifies obsolete Sharp PC-900 which currently substitued with the 6N138 IC (reference design is of TOSHIBA).
The PC-900 or the 6N138 IC are called optocoupler or optical isolator. An opto-isolator (also known as optocoupler, photocoupler, or optical isolator) is an electronic component which transfers electrical signals between two isolated circuits by using light. This prevents high voltages affecting the system receiving the signal. We have seen such optical isolators to be used on 4 channel relay.
For easiest connection, we need an 220 Ohm resistor, one MIDI jack, jumper wires, breadboard and one USB MIDI adapter. We need some headers to solder with the MIDI jack’s pins. MIDI jacks are easy for wiring – middle one ground, left hand side data pin (digital pin of Arduino, like pin 1 – RX), right hand side +5V with a 220 Ohm resistor in-between.
Now, we can download this Virtual MIDI Piano Keyboard software and install :
1 | http://vmpk.sourceforge.net/ |
We need to go to the Edit menu, choose MIDI Connections and tick mark :
Enable MIDI input
Enable MIDI thru on MIDI output
options. Also change the base octave setting in the main window to 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | void setup() { Serial.begin(31250); } void loop() { for (int note = 0x18; note < 0x53; note ++) { noteOn(0x90, note, 0x45); delay(300); noteOn(0x90, note, 0x00); delay(100); } } void noteOn(int cmd, int pitch, int velocity) { Serial.write(cmd); Serial.write(pitch); Serial.write(velocity); } |
Reference of the above can be found on Arduino’s official documentation :
1 | https://www.arduino.cc/en/Tutorial/Midi |
There are MIDI Note Tables with corrosponding hex values.
Now, if we want to follow the MIDI standard’s circuit then we need to use the 6N138 optocoupler, 1N914 diode. We can install this library :
1 | https://github.com/FortySevenEffects/arduino_midi_library |
And this circuit :
The above circuit and below code is from notesandvolts.com
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <MIDI.h> #define LED 13 void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { digitalWrite(LED,HIGH); if (velocity == 0) { digitalWrite(LED,LOW); } } void setup() { pinMode (LED, OUTPUT); MIDI.begin(MIDI_CHANNEL_OMNI); MIDI.setHandleNoteOn(MyHandleNoteOn); } void loop() { MIDI.read(); } |