Step 9: Receive MIDI Messages with Arduino
an Arduino synthesizer that uses MIDI messages to construct audio waveforms
a device which uses MIDI to trigger mechanical events, like the ringing of different sized bells
a MIDI to control voltage(CV) device- communication between MIDI and analog synthesizers
MIDI connector Digikey CP-2350-ND
220Ohm 1/4watt resistor Digikey CF14JT220RCT-ND
1N4148 diode Digikey1N4148-TAPCT-ND
10kOhm 1/4watt resistor Digikey CF14JT10K0CT-ND
470 Ohm 1/4watt resistor Digikey CF14JT470RCT-ND (I used 2x220 instead)
6N138 optocoupler Digikey 751-1263-5-ND
The hardware setup is slightly more complicated for receiving MIDI than it is for sending. As you can see in the schematic above, you have to set up an optoisolator in between the MIDI jack and the Arduino. If you are confused about the MIDI pin connections, refer to fig 1. I set this circuit up on a breadboard in figs 4 and 5.
The following code receives these messages, reads them, and stores them appropriately. See the comments for more information.
To make sure that everything is working properly, try the code below. This code turns the led at pin 13 on briefly when it receives a note on message for MIDI note 60 (middle C). Notice how I included "&& velocityByte>0" in the if statement- this makes sure that we are dealing with a note on statement, if you don't include this the light will blink for both note on and note on with velocity = 0 (note off) messages.
If you want to do a lot of stuff in the main loop, or if you are expecting to receive a lot of MIDI data and timing is important to you, you can also try using a timer interrupt to periodically pause the main loop and check if there is incoming MIDI. It will look something like this: