Go Down

Topic: Make Arduino Uno read MIDI timing clock signal and turn on a LED (Read 2041 times) previous topic - next topic


Oct 24, 2013, 02:00 am Last Edit: Oct 24, 2013, 06:39 am by Nick Gammon Reason: 1
It is my first time on the Arduino forum.
I need some help please.
Just so you know, english is not my first language, I'll do my best.

Here's my situation :
I have an Arduino Uno v3.
I'm a musician and I work with a Korg Electribe drum machine which sends midi timing clock signal thru it's MIDI out.
I play with a drummer/percussionist and would like him to see midi timing clock signal outputted by the drum machine.
I thought of using a LED plugged in pin 13 of the Arduino.
I made the connection with a MIDI cable on the Arduino as explained in the MIDI tutorial.
I did some research over the web to find some ideas for the code.
Here's the code I'm using right now but it does not work :

Code: [Select]
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;

void setup() {

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)) {

void Sync() {
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);

Would you have any idea what is wrong with my code?
Thank very much for your help.

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)


That code is from little-scale if i remember well, and it does work.
What's happening? The LED doesn't work?
First you need to make the pin 13 an output. Without pinMode(13, OUTPUT); it's acting like an input. Digitals are input by default.

Second, is the MIDI communication working? Are the RX/TX leds flashing?

Thirrrd, your Electribe is sending standard MIDI clock i suppose. What do you want to do with that data? Make the LED go on and off? How often? MIDI clock sends 24 pulses per 1/4 note. If you have a BPM of 120, you have a 1/4 note of 500ms, and each MIDI clock pulse is 20.833ms. So you're calling Sync(); at a rate of 20ms, I doubt your drummer will see that ahah. Even with the 10ms delay, if your MIDI connection is working ok, and the LED is ok(declared pin 13 as output) you'll surely see it always ON.

You need to make a counter. Say, you want your LED to blink each 1/4 note, or 1/8? or 1/16?
Declare a variable that increases each time Sync() is called, and once it reached 24(the last MIDI clock pulse of the 1/4 note) reset the counter to 0 and make the LED go on, else, LED off. Maybe the blink will be to fast, the LED will be on for only a MIDI clock pulse. So instead of the else LED go off, you can wait let's say 4 pulses, and then LED off.


What capicoso about making the pin output.

Plus, I agree that the MIDI timing clock may not prove much. Do you want something else, like the drum beat? That could be quite different from the MIDI timing signal.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up