Send and Receive MIDI with Arduino

This instructable will show you how to use an Arduino to send and receive a variety of MIDI messages so you can start building your own MIDI controllers and instruments.  First I’ll talk a little bit about MIDI protocol, if you’re just looking for sample code skip ahead to steps 5-9.

If you know absolutely nothing about MIDI note, velocity, and pitchbend or are confused about what MIDI does and why you would want to use it, check out my What is MIDI? instructable.

Step 1: Bytes and Bits

To understand MIDI communication, you have to understand a little about bytes and bits.  A byte is a packet of data used to store information.  In MIDI protocol, each byte is made up of 8 bits; bits can only equal to 0 or 1.  A sample byte is given below:


Each 1 or 0 in this byte is a bit.  The leftmost bit is called the most significant bit (or MSB) and the rightmost bit is called the least significant bit (or LSB).

Bytes of the form above are binary numbers because they are expressed using only 1’s and 0’s.  We can convert this number to base ten as well:

11010111 in binary (base 2) = 215 in decimal (base 10)

If you need help converting numbers from binary to decimal or vice versa check out Wolfram Alpha.  Type in a binary number followed with “from binary to decimal” to get the decimal equivalent.  Wolfram Alpha is also great for converting to and from hexadecimal.

Wikipedia is a good resource for more information about bytes and binary.

Send and Receive MIDI with Arduino

Step 2: A Bit About MIDI Protocol

Picture of A Bit About MIDI Protocol
A really basic overview of MIDI terms and concepts is given here.

MIDI messages are comprised of two components: commands and data bytes.  The command byte tells the MIDI instrument what type of message is being sent and the subsequent data byte(s) store the actual data.  For example a command byte might tell a MIDI instrument that it going to send information about pitchbend, and the data byte describes how much pitchbend.

MIDI data bytes range from 0 to 127.  Convert these numbers to binary and we see they range from 00000000 to 01111111, the important thing to notice here is that they always start with a 0 as the most significant bit (MSB).  MIDI command bytes range from 128 to 255, or 1000000 to 11111111 in binary.  Unlike data bytes, MIDI command bytes always start with a 1 as the MSB.  This MSB is how a MIDI instrument differentiates between a command byte and a data byte.

MIDI commands are further broken down by the following system:

The first half of the MIDI command byte (the three bits following the MSB) sets the type of command.  More info about the meaning on each of these commands is here.
10000000 = note off
10010000 = note on
10100000 = aftertouch
10110000 = continuous controller
11000000 = patch change
11010000 = channel pressure
11100000 = pitch bend
11110000 = non-musical commands

The last half of the command byte sets the MIDI channel.  All the bytes listed above would be in channel 0, command bytes ending in 0001 would be for MIDI channel 1, and so on.

All MIDI messages start with a command byte, some messages contain one data byte, others contain two or more (see image above).  For example, a note on command byte is followed by two data bytes: note and velocity.

I’m going to explain how to use note on, note off, velocity, and pitchbend in this instructable, since these are the most commonly used commands.  I’m sure you will be able to infer how to set up the others by the end of this.

Step 3: Send MIDI Messages with Arduino- Hardware

Parts List:
MIDI connector Digikey CP-2350-ND
220Ohm 1/4watt resistor Digikey CF14JT220RCT-ND

Following the schematic above, solder a 220Ohm resistor to MIDI pin 4.  Connect ground to MIDI pin 2 and 5V to MIDI pin 5.  If the pin numbering is unclear, refer to the pictures above.

Step 4: Plug in MIDI Out

Connect the MIDI socket to a MIDI cable and plug the other end of the cable into your MIDI instrument of choice.  I used a MIDI to USB cable and connected to my computer.

Step 5: Software Solution: Serial to MIDI Application

You can bypass the MIDI adapter setup from the last two steps by using the Ardiuno’s USB connection to send Serial messages to your computer, then run an app like Hairless MIDI to convert this the Serial messages to MIDI and route them to other applications on your computer (Ableton, Garageband, etc). The only difference in the code is that you will need to set the baud rate of your Serial connection to something that Hairless MIDI will accept, so be sure that the number in this line in the Arduino’s setup() function:


is the same number specified under Hairless MIDI >> Preferences >> Baud Rate (I used 9600, see the image above, I had to replace line Serial.begin(31250) with Serial.begin(9600) in all the example Arduino sketches in this instructable). Normally when you create MIDI with a MIDI connector you need to set the baud rate to 31250, but if you’re connecting via USB to a Serial to MIDI application, you can use whatever baud rate you like.

To use Hairless MIDI you will need to select your board (something like usbmodemfd121) from the Serial Port menu and select the MIDI channel that you would like to send or receive MIDI to/from. Make sure you have the same MIDI channel selected in the preferences of whatever other MIDI applications you are running on your computer.

Another thing to be aware of is that you cannot program the Arduino while it is connected to Hairless MIDI, because the port is occupied (see the error in the second image). A quick way to bypass this without needing to quit Hairless MIDI each time you want to change your code is to select a different Serial Port from the Hairless MIDI interface, upload your new Arduino code, and then set the Serial Port in Hairless MIDI back to the correct one.

Send and Receive MIDI with Arduino schematic

Step 6: Basic Note On, Note Off with Arduino

This code sends MIDI messages out Arduino digital pin 1 using note on and note off commands.

As I explained in step 3, the MIDI commands for note on and note off are as follows:
noteON = 10010000 = 144
noteOFF = 10000000 = 128

Both of these commands are followed by two more bytes to make a complete MIDI message, the first is note and the second is velocity (for more info about what “note” and “velocity” mean check out my introductory MIDI instructable).  Note and velocity can range from 0 to 127.  In this example I used notes ranging from 50 to 69 (D3 to A4):
for (int note=50;note<70;note++){}
and I set the velocity to 100:
int velocity = 100;

So when the function MIDImessage() is called in the loop() of the arduino sketch, it sends the three bytes:

if the “command” in the MIDImessage() function is noteON then the note will start, if it is noteOFF the note will stop.


For more detail: Send and Receive MIDI with Arduino

This Post / Project can also be found using search terms:

  • download Hairless Midi Serial
Clip to Evernote

Leave a Comment

(Spamcheck Enabled)

Read previous post:
Arduino – A Basic Theremin

Theremins are cool. Fact. You may also have heard of them as either aetherphone/ etherophone or termenvox/ thereminvox. Essentially its...

Scroll to top