Instructables

Send and Receive MIDI with Arduino

Featured
Picture of 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.
 
Remove these adsRemove these ads by Signing Up

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:

11010111

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.
AmpOwl2 months ago

Is there a way you could send the signal through USB directly, without having to use a separate adapter?

AmpOwl AmpOwl2 months ago

To be more clear, I want to use a digital pin to send the signal, instead of the arduino's USB interface. I'm using a barebones arduino that requires an FTDI adapter (that I only have one of).

amandaghassaei (author)  AmpOwl2 months ago

if you're sending MIDI to your comp, you could run an application like this to convert serial to MIDI and pass the MIDI messages through the USB connection. You could also get your Arduino to act as a USB/MIDI device through firmware.

I see; are there any necessary changes (using the first method) to the code described in the instructable? How would you wire the USB connection from the Arduino pin? Thanks for the help.

amandaghassaei (author)  AmpOwl2 months ago
You can use the USB on the arduino, I added some info in step 5

Ok, thanks. Would it be possible to use a digital pin on the arduino to send the serial signal, instead of the programming port? Again, I am using barebones arduinos that require a separate FTDI adapter, and I only have one of those.

amandaghassaei (author)  AmpOwl2 months ago

no, I don't think so.

What if you used this library I found; it's called SoftwareSerial, and it says it allows you to send serial data through a digital pin. Would it be compatible with Hairless MIDI?

http://arduino.cc/en/Reference/SoftwareSerial#.Uv8...

http://arduino.cc/en/Tutorial/SoftwareSerial#.Uv8g...

amandaghassaei (author)  AmpOwl1 month ago

you can use this to send serial data to electronics, but I'm not sure it will communicate with your computer.

I want to making you know that I had some trouble making my arduino speaking MIDI. Finally I got it with Hairless Serial<>MIDI converter and a MIDIPort, but I must set the serial baudrate at a different speed (38400). In Hairless' Settings MENU I must choose at which speed is Arduino sending serial information and there isn't 31250.
I can't solve this issue and I need to because MIDI Library work at 31250 by default and because I imagine a physical USB converter (as yours) expect to receive data at that frecuency.
I tried to use a Midiman MIDISPORT 2x2 without success. I don't know how to solve this problem and also It get me a bit frustrated because all DIY projects use that MIDI default MIDI without problems.

I haven't tried to receive, manage and process incoming MIDI data. I'm going step by step.

Anyway I found your channel really well made with detailed informations and clear explications. I'm also having benefits from the other instructables about Timer Interrupts. It's great!

Alberto
amandaghassaei (author)  albertocarlassare6 months ago
which arduino board are you using?
Yes! I'm sorry!
I'm Using:
-Arduino Uno Rev3
-Midiman MIDISPORT 2x2 (old version -> http://www.wikizic.org/Midiman-Midisport-2x2/)
-Recently downloaded Arudino IDE

-Hairless MIDI<>Serial Bridge (very good tool)
-LoopBe1 as primary Virtual Port (because your Max patch doesn't recognize me MIDI Yoke - Thanx also for the patch, very useful to).
-MIDI-OX for monitoring MIDI activity (before checking in DAW - Mainly ProTools/Cubase/Traktor/Live)

On Hairless FAQ section I found out that Hairless can't work at 31250. Also they explain that isn't standard recuency for serial ports. So they say to use a 'compatible' serial baudrate. That's ok for me just for building up code, classes and libraries and switch to 31250 at the final stage; My doubt is: Why MIDISPORT doesn't react to incoming messages at 31250 and Serial-to-Midi software neither. Serial-to-Midi is supposed to work at that baudrate.
amandaghassaei (author)  albertocarlassare2 months ago

I ended up using Hairless MIDI for a class and I came across the problem you were describing, I think I did not understand the problem before. You can change the baud rate in all the Arduino sketches to something that Hairless MIDI accepts. The baud rate only needs to be 31250 if you are connecting to something that is expecting MIDI, not Serial.

Just be sure that you set the rate in Hairless MIDI to whatever you set in Arduino, I added a section about this (step 5). I used 9600 baud and it worked great.

amandaghassaei (author)  albertocarlassare6 months ago
if you want to send midi into a piece of hardware like a synthesizer, you will have to output at 31250, it's possible to send messages at other baud rates and then convert it to a midi signal in your comp, that's probably what's happening
http://spikenzielabs.com/SpikenzieLabs/Serial_MIDI.html
Use this link from Spikenzie Labs, which has a Serial <> MIDI sketch made in processing already set-up. It does have the correct baud rate for me, you just have to know which serial port to use, what device to output TO, and what device to input FROM

I'm thinking can I make my midi cotroller to cotrol any sound generator.

One idea is with PWM

MIDI controller -> Arduino -> PWM output -> analog output

X1L33 months ago
Thanks amanda. Appreciate the help. Really cool project and a brilliant and easy to grasp introduction to midi.
e.ma.niak3 months ago
I'm building my own midi controler, now i know how to handle incoming midi. Great instructable.
amandaghassaei (author)  e.ma.niak3 months ago

awesome!

X1L33 months ago
Hi. I'm just wondering how you'd go about filtering out unwantwd midi controler date. The project I've built with this works fine but any pitch/modwheel data etc freezes / unfreezes it. Is it a case of telling it to read these messages and then do nothing?
amandaghassaei (author)  X1L33 months ago

yes definitely, you should add an if statement that checks to the value of commandByte to see if it's what you want. commandByte = 144 is note on, you can find the rest of the commands here

Team3G3 months ago
The shake to clear feature is genius. Inspiration points++
jhenrique35 months ago
Hello, Amanda! Your projects are awesome :D I don't have an arduino yet, but I'm planning to use two of your instructables (Arduino Audio Output and Send and Receive MIDI with Arduino) and some other projects to modify a toy casio keyboard, so that I can control the arduino with the toy keyboard's keys or another keyboard to play some NES-like music. I would like to know if it is possible to change the pin where the arduino receives MIDI data, because the D0 pin of the arduino is already used in the Arduino Audio Output project, as I plan to do these projects with a single arduino. Thanks in advance.
It is not possible to change the MIDI send/receive pins because MID requires serial data and pin D0 is the serial receive while pin D1 is the serial transmit pin. If you need more than one serial TX/RX pin, I would recommend and Arduino MEGA 2560, which has 4 of each.
amandaghassaei (author)  kipfan234 months ago
actually, you can use software serial to change the Serial outs.
Very clear explanation. Excellent work! Great!
soeren10 months ago
Hi Paulsoulsby

I'm really having issues getting midi input as well. I've tried many different setups and opto couplers, including this exact instructable - without luck.

I tried your code, but you haven't defined NOTE_ON, NOTE_OFF, PITCH_WHEEL and CONTROLLER anywhere. What did you intend to put into these variables, or are they #define instructions that you forgot to include here?

all the best
Søren
amandaghassaei (author)  soeren6 months ago
I updated my code, I made a little typo with serial.available(), I'm curious to see if it works for you now, does it?
amandaghassaei (author)  soeren10 months ago
hey, Paulsoulsby won't get your comment unless you reply to them directly. I'm sorry this MIDI input issue has been causing you trouble, I had some trouble getting my input circuit to work at first too. Do you have an oscilloscope? I found that really helpful in figuring out where the problem was.
pulykamell6 months ago
Doesn't Serial.available() return the number of bytes and not bits in the serial buffer? Could that be the issue for folks here?

I got it to work from the schematic, but I had my yellow and green wires flipped around initially. Once I flipped those and changed around the code, it worked fine. Also, there is an issue with "running status" as some MIDI devices, to save data, will not resend the command byte if the last command is the same. For example, on my keyboard, if I hit G then C quickly, the bytes sent will look like: 144, 55, 70, 60, 70, with the 144 only being sent the first time, so you have to watch out if you're reading in threes. I'll try to post the code I got working later, as I have it currently set to output MIDI data onto an LCD screen (which helps for diagnosing this stuff.)
amandaghassaei (author)  pulykamell6 months ago
good catch, I'll update the code to reflect that
Hi Amanda,

Referring to step 9. I understand that digital pin 0 (RX0) must be disconnected when uploading program to Arduino via USB cable.

After uploading the program to arduino, pin 0 (RX0) will be reconnected to receive input from MIDI jack. Can I also reconnect the USB cable to power the arduino? Or do I have to power the arduino using external power source such as battery pack?

Thanks and regards,
electronicrookie
amandaghassaei (author)  electronicrookie10 months ago
I think you can keep the usb plugged in w/o a problem, have you tried?
kylehlogan11 months ago
I'm really interested in doing something that requires the tempo from another device via MIDI In but I didn't see anything pertaining to tempo in the article. I know you can send tempo map information via MIDI, but how would you do it via Arduino?
amandaghassaei (author)  kylehlogan11 months ago
you might want to incorporate this into your setup:
http://en.wikipedia.org/wiki/MIDI_beat_clock
scroll down to the "system real-time messages" in this table:
http://www.midi.org/techspecs/midimessages.php
you just need to check to see what type of messages you are receiving with the midi in, if they are equal to 11111000, then you know it is a timing clock message and you can have this modulate the timing of your midi out. does that make sense?
I'm planning on making a real proper midi controller for ableton soon. Its going to be an arcade style controller, I've never built something like this besides a ghetto "sequencer" for ableton i made which is based on a keyboard encoder. Any tips for me?
amandaghassaei (author)  mastermakoko11 months ago
breadboard everything first, and if you're feeling ambitious, multiplexing your buttons will allow you to connect more stuff to the arduino.
paulsoulsby11 months ago
Hi,
Thanks for this instructable.  It's been really handy.  I had problems with the MIDI input, with some MIDI devices.  I discovered that this was to do with MIDI active sensing.  Here's my code for reading the MIDI port that takes into account for MIDI active sensing.

byte incomingByte=0;
byte notebyte=0;
byte velocitybyte=0;
byte statusbuffer=0;
boolean arp_triggernext=false;
boolean firstbyte;
void MIDI_Poll(){
  if (Serial.available() > 0) {
    do {
      // read the incoming byte:
      incomingByte = Serial.read();
      if (incomingByte>247) {
        // this is where MIDI clock stuff is done
        switch (incomingByte){
        }
      }
      else if (incomingByte>240) {
        statusbuffer = 0;
        //sysex stuff done here
      }
      else if (incomingByte>127) {
        statusbuffer = incomingByte;
        firstbyte = true;
        notebyte = 0;
        velocitybyte = 0;
      }
      else if (statusbuffer!=0) {
        if (firstbyte == true) {
          // must be first byte
          notebyte = incomingByte;
          firstbyte = false;
        }
        else {
          // so must be second byte then
          velocitybyte = incomingByte;
          //process the message here
          if (statusbuffer == NOTE_ON && velocitybyte != 0) {
            //MIDI note on subroutine
          }
          else if (statusbuffer == NOTE_OFF || (statusbuffer == NOTE_ON && velocitybyte == 0)) {
            //MIDI note off subroutine
          }
          else if (statusbuffer == PITCH_WHEEL){
             //pitch bend wheel
          }
          else if (statusbuffer == CONTROLLER){
            if (notebyte==1) {
              //MIDI_modwheel_level = velocitybyte;
            }
          }
          //now clear them for next note
          notebyte = 0;
          velocitybyte = 0;
          firstbyte = true;        
        }
      }
    } while (Serial.available() > 0);
  }
}
amandaghassaei (author)  paulsoulsby11 months ago
that's really interesting! thanks for sharing.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!