Go Down

Topic: works: MIDI-IN: code + schematics (Read 56070 times) previous topic - next topic


Ok, you're right. I'll look that later.
My project is not pure notes midi I/O, but I want to be sure to understand the mechanism of reading the serial input.
Thanks !


I just got MIDI-IN working with the CNY17-2 optocoupler. I used the MIDI library from the arduino playground to make the led on pin 13 flash on receiving a MIDI message. You can find the modified scheme below.


this will also work without the 100k resistor btw


Can anyone help me with the code for simply reading 3 (20,21,22) program change and 1 control change (36) and simply activate 1 led for each program change and 1 led for the control change?


Apr 02, 2009, 02:51 am Last Edit: Apr 02, 2009, 02:53 am by tetracon Reason: 1
Can anyone help me with the code for simply reading 3 (20,21,22) program change and 1 control change (36) and simply activate 1 led for each program change and 1 led for the control change?

The following might work for you.

Code: [Select]

#include <MIDI.h>

byte ledPC = 11;
byte ledCC = 12;
byte channel = 1;

void setup() {
 pinMode(ledPC, OUTPUT);
 pinMode(ledCC, OUTPUT);

void loop() {
 if (MIDI.read(channel)) {
   if (MIDI.getType() == PC) {
     byte dat1 = MIDI.getData1();
     if (dat1 > 19 && dat1 < 23) {
       digitalWrite(ledPC, HIGH);
   else if (MIDI.getType() == CC) {
     if (MIDI.getData1() == 36) {
       digitalWrite(ledCC, HIGH);

Edit: You might want to download the MIDI library from the playground first ;)


Thank'you very mutch,this night I try all and reply this.



your code seems to work,but I need some things:

I need 3 separate leds for 3 program change,and when is active the program 20 is active the ONLY led1,when active the program 21 is ONLY active the led2,and obviousily the PC22 activate only led3.

When I'm using Control Change,I need to activate it with CC36,and DEACTIVATE with CC36 (or for example CC00)

Is complicated do this?
Thank's in advance,




You might want to try adapting the code to your needs. If you find it hard to understand you might want to do some more research on programming.
Basically all you need to do is add some vars in the top part for the additional leds.

Code: [Select]
if (dat1 > 19 && dat1 < 23) {
       digitalWrite(ledPC, HIGH);
       digitalWrite(ledPC, LOW); // forgot this last time

This part finds out what the PC value is, you might want to change this to more if statements so that you can turn on or turn off the led of your choice.


Apr 06, 2009, 03:55 am Last Edit: Apr 06, 2009, 03:57 am by quackmaster7000 Reason: 1
I am EXTREMELY frustrated! I have spend my ENTIRE Sunday debugging this, but I can't get it to work. I have all the connections correct, I'm pretty sure. I am using a split-end cable which goes right into the female jack on my microKorg synth. I am using a 4N28 opto-isolator. The 220ohm resistor connects to pin 2 on the other end of the cable (the circular, male metal connector end end). To help myself visualize, when looking at the end of the male MIDI jack, I number the pins:
  2.  3.  4.  
1.             5.
pin 4 on this cable connects to pin 2 of the opto. I am NOT using pin 3 (which is GND for the MIDI connector). I shouldn't be, correct? Anyways, with or without, it hasn't worked.

I read the details on using a different value resistor on pin 6 of the opto. For some reason, I SOMETIMES will get a '1' returned with Serial.available(), IF i mess with a potentiometer between 100Kohms and 200Kohms (usually ~170 does it), but it isn't reliable at all. I've got this all set up on a breadboard for now.

It is my understanding that this is supposed to connect to the jack on the MIDI device labeled "MIDI OUT", correct? I am also getting a little confused with perspectives here lol (flip-flopping cable/jack pins 4 & 5 to match male/female jacks/sockets). Have I possibly blown out the optoisolator? I don't see what I'm doing wrong! :'(

FYI: I have been referring to the original schematic on page 1 of this topic.


Apr 06, 2009, 08:31 am Last Edit: Apr 06, 2009, 08:32 am by tep Reason: 1
quackmaster7000, I didn't follow everything, but don't stay frustated!
How to number de pins:
Myself I made a modification to the schematic that is posted by kuk:

I didn't connect the 100kOhm resistor to anything and it worked, perhaps your solution too?
I connected the 4th pin of the DIN connector to the 220Ohm (and the 5th is the other).

Good luck!  ;)

Captain Credible

great instructable ! i am using your code now to drive relays via transistors so that i can program beats using old radios as the sound source (turned on and off by eight relays) I have come across an issue though that i dont understand at all,

When i plug the midi input straight into the midi out of my korg ms2000 everything works fine and dandy, however if i control the arduino with my edirol ua25 sound cards midi out the arduino will not handle two simultaneous notes. If i dont send a note of for the first note, i have to wait approx 1 sec until i can press a new note that it will handle, this happens with my edirol PCR-500 midi keyboard too. What is going on ? :-?


HOORAY! It works!  ;DI finally got it to work lol. I DIDN'T need the 100Kohm resistor, and I had a few things mixed up it seems. Now I just have to debug all these MIDI messages, and figure out a way to receive, store, and retrieve them quickly and efficiently...


been trying to get this schematic alongside a few other MIDI IN ones to work but so far no results. Apologies if this is a stupid question but am I right in thinking that with this schematic pins 6 and 4 go to GND? and pin 5 goes first to PIN 0 and then to +5v (via 3.3k resistor)?

I have been trying to find a suitable solution and have also come across this in the process:


which is a Serial to Midi converter - haven't fully tested yet but could be a solution also - however this requires the MIDI data to be sent over USB and that also means a computer, I am interested in that to an extent but I would also like to be able to build standalone units that can be controlled by simple (and cheap) midi keyboards.



It's not very clear what you wish to do with this, but from what I have gotten out of it, you want to make a MIDI Controller/Sampler that will receive MIDI messages from a MIDI keyboard and then produce sound assigned to each key?

I didn't use the 100K resistor; I didn't even connect pin 6 to anything and it works. If you have trouble reading schematics, google "schematic symbols"; there's a huge wealth of information out there on that.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131