Arduino MIDI Clock Tutorial for Beginners

by Doug

 October 18, 2016
Arduino MIDI Clock Tutorial

Arduino, as we know is capable of carrying out complex tasks than a simple microcontroller. Sometimes, you may want to synchronize a self-made sequencing circuit to MIDI Clock Signals. In Order to do so, you need to get a hold of MIDI communication and Arduino is one of the best ways to do so.

First of all, you must be aware of the difference between a byte and a bit. While both are units for storage, a byte is a packet of data made up of 8 bits. A MIDI protocol, like many others, use the 8-bit byte convention. A bit may store a 0 or 1, e.g. 11010111

The left most bit is called the most significant bit or MSB while the right most bit is called the least significant bit. A binary number can be converted into a decimal number using a variety of converters; you don’t need to grind yourself with this:

Binary to decimal converter

While you can write application specific code to deal with MIDI clock signals, a skeleton code will do the trick and clear your concepts, helping you to synchronize several MIDI hardware like Ableton Live, Pro Tools, etc. to a MIDI clock.

A MIDI beat clock or clock is a signal that is sent through the MIDI protocol to make sure that the plethora of MIDI powered devices stay in sync. MIDI messages usually comprise of 2 – 3 bytes but unlike these messages, MIDI clock messages are only a byte in length.

The following are the major MIDI clock bytes in use:

 start = 0xfa
 stop = 0xfc
 clock tick = 0xf8
 continue = 0xfb

When the host sequencer is running, a total of 24 clock tick bytes are sent per quarter note. When the sequencer comes to a halt or pause, the stop byte is sent, while when the sequencer starts after getting stopped, the start byte is sent again. In the case where the sequencer is paused and then started, the continue byte is sent.

 

Code

For every clock tick, “something” can be done, using the information devised above. All you need to know is what to do after every clock tick, and then add the functionality to the code below:

byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;

void setup() {
 Serial.begin(31250);
}

void loop() {
 if(Serial.available() > 0) {
 data = Serial.read();
 if(data == midi_start) {
 play_flag = 1;
 } else if(data == midi_continue) {
 play_flag = 1;
 } else if(data == midi_stop) {
 play_flag = 0;
 } else if((data == midi_clock) && (play_flag == 1)) {
 Sync();
 }
 } 
}

void Sync() {
 // do something for every MIDI Clock pulse when the sequencer is running
}

 

Diagram

ardunio

The IC used is 4N28, optocoupler. You may employ the help of the datasheet for modifying the existing design.

 

 Conclusion

All you need to do now is fill in the “Sync” block, and you’ll be good to go.



What do you think?


  • Peter Lutek April 26, 2017

    thanks for this! two questions:

    1. is it necessary to use the 4N28, or is there a way to integrate MIDI clock into a more generalized MIDI controller, like we’re building in your course? (i’d like to send MIDI clock AND other MIDI CC data simultaneously.)

    2. this code, above, generates ONE clock message, right? so, to generate a useful stream of MIDI clock signals, we just have to put it in a loop or whatever which repeats this block of code at the desired rate – and then optionally put whatever else we want to have happen at each clock tick in the “sync” function? just want to make sure i’m understanding correctly….

    thanks… cheers!
    .pltk.

    • Doug (Admin) April 26, 2017

      Glad you enjoyed!

      1. In the course, there’s a section where I teach how to use the HIDUINO firmware. HIDUINO will make the Arduino show up as a MIDI controller when plugged in with USB. This will get rid of any need to use the optocoupler, as you can simply send MIDI clock signals over to the Arduino.

      2. I’m not sure I follow you on this question. The loop function continuously loops over, checking for any MIDI clock signals being sent. Whenever it discovers a clock signal, it will execute the sync function.

      For example, if you wanted to create a MIDI clock powered metronome, all you would have to do is edit the sync function and add code to output a clicking sound to a speaker. The result would be a speaker that would click in time with the MIDI clock you are receiving.

    • Peter Lutek April 26, 2017

      thanks, doug!

      oh, right… you’re READing MIDI clock! oops. i’m looking to WRITE it out from the arduino, along with other things. i’ve found some other resources that help me out with that.

      also, in the course it seems like you only deal with note and pitchbend messages. what about continous controllers (MIDI CC messages)?

    • Doug (Admin) April 26, 2017

      Ah, gotcha. So with sending MIDI clock, you’ll want to output a signal in a similar way that you’d output a regular note, but use “0xf8” (MIDI clock message) as the signal.

      Just your comment on the course about CC. I’ll reply to it there and you should get an email.

    • Peter Lutek April 26, 2017

      thnx for your quick responses… great resource you’ve got here!