Go Down

Topic: 'midi in" project.... (Read 28181 times) previous topic - next topic


hey there,

just wanted to know if anyone has some good news regarding this matter.
i'd really like to help out and try my luck with midi-in for arduino. but i don't want get stuck at the same point.

does anyone know a different midi<>microProc circuit or has an idea why the above wouldn't work?
after all this shouldn't be an issue!?!

best, kuk_


i got this circuit working recently with a pic to see if it was an issue with the parts i was using but it wasnt, so still no joy on the arduino side of things.  :P


It is a bit off-topic, but I was wondering if anyone had a schema to interface an Arduino with a MIDI out port. This way, we could send data to a computer using not so many resources as when we use the serial port emulation.

An other interesting project would be to interface it with an Ethernet port. Then, it could be powered over Ethernet, and send and receive data.

USB is good, but Ethernet and MIDI can be very interesting too. But this is getting much off-topic.  ;)


wondering if anyone had a schema to interface an Arduino with a MIDI out port.


it's usually much less finicky to do MIDI output than MIDI input...


although i did not have a try on the midi in circuit posted above, i searched a bit in the net and found this pdf:
it's a circuit based on the atmega8, and uses a slightly different midi input circuit. maybe it's worth a try to use this circuit?
it has a 1,2k resistor between VCC and the optocoupler (which is a cny17 - has a different pinning than the 6n138).



i'm working on it now. i'm starting with midi-in on arduino, simply because i don't have midi-in interface but a keyboard with midi-out.

i'm trying my luck with a 4n28 optocoupler. so far i have an LED blinking when i hit a key on the keyboard. the led circuit is seperated from the midi circuit by the 4n28.
(actually its always blinking and just gets brighter when i hit a key !? any idea, someone?).

i wanted to route the serial midi signal through an arduino with the atmega taken out (tip from massimo). the problem is that zterm (my serial read/write application of choice) doesn't allow 31250 bps. so debugging the optocoupler setup won't work this way :-(

the question now is whether arduino serial settings (8-n-1) are compatible to midi-serial-communication which is 10-bit long. containing one start bit and one stop bit.
are arduino's settings changable using "secret power"-c ? or should it just work? i'm only understanding half of this.

/ kuk

Kristian Gohlke

hi, did you read this one as well?



i got it working.
that means im only able to read the second byte of the midi byte stream. like:
if (serbyte == 61) {
   digitalWrite(ledPin2, HIGH);
 if (serbyte == 73) {
   digitalWrite(ledPin2, LOW);
if (serbyte == 62) {
   digitalWrite(ledPin3, HIGH);
 if (serbyte == 74) {
   digitalWrite(ledPin3, LOW);

..... and so on
that means its just the note number wich is recognized, neither channel nor volume/noteOn/noteOff bytes.
in this code example i use one note to turn on the LED, anotherone to turn it off...
but im already a little bit happy, like this i can read notes. only problem at the moment: there must not be some other midisignals on the wire if yes--big trouble! because theres no channel separation.



cool. nice to know, you're working on it, too.

i had similar semi-satisfying results. since i couldn't get that optocoupler from the schematics i created a similar circuit with an 4N28 (which is basically slower i think). Still it worked until i blew it up by not spending attention to the wiring :-( .

stupid over-sensitive electrical devices :P

but i got myself some more of those 4N28 yesterday.

concerning the midi messages, i lost several hours waiting for a "Note off" (i mixed up "note on" and "note off", because of a wrong table somewhere on the net). which is never sent by my keyboard. instead most midi devices send a "note on with zero velocity" to stop a note...
maybe you're experiencing something similar?



oh, here is the code i'm working on.
Code: [Select]
/* ################################ Basic Midi-in for arduino 0.1
* reads midi-data from the serial line and lights individual LEDs for "Note on" messages
* i am still working on the best wiring:
* it's based on a 4N28 optocoupler to seperate circuits and invert the logic levels
* the main problem here is the speed of the optocoupler to retain a clear digital signal.

  22/09/2006, kuk


//######################### DEFINE PINS ON ARDUINO
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;
int led8 = 9;
int midiEnable = 10; //this one "powers" the 4N28 to avoid having it on all the time, which interferes with bootloading
int statusLed = 13;

//######################### DECLARE VARIABLES
byte incomingByte;
byte action = 2; // 0=note off , 1= note on
byte note;
byte velocity;

//######################### SETUP EVERYTHING
void setup() {
 pinMode(statusLed,OUTPUT);   // declare the LED's pin as output
 Serial.begin(31250);        //midi baudrate
 //turn midi input on
 digitalWrite(midiEnable, HIGH);
 //do fancy startup signal

//######################### MAIN LOOP
// read serial data, and collect bytes that belong to the same midi message
void loop () {

     if (Serial.available() > 0) {
           incomingByte = Serial.read();

               // collect data: we receive at least 3 bytes when a key gets hit: 1 statusbyte(channel & on/off/etc), and 2 databytes (key + velocity)
               if (incomingByte== 144){ // note on message channel 1
               }else if (incomingByte== 128){ // note off message channel 1
               }else if (incomingByte== 208){ // aftertouch message channel 1
                // not implemented yet
               }else if (incomingByte== 160){ // polypressure message channel 1
                // not implemented yet
               }else if ( (action==0)&&(note==0) ){ // if we received a "note off", we wait for which note (databyte)
                playNote(note, 0); //turns off the corresponding LED
               }else if ( (action==1)&&(note==0) ){ // if we received a "note on", we wait for the note (next databyte)
               }else if ( (action==1)&&(note!=0) ){ // ...and then the velocity data byte
                // now we have our bytes complete, let's make some visual noise
                playNote(note, velocity);
                //reset values


void blink(){
   digitalWrite(statusLed, LOW);
 digitalWrite(statusLed, HIGH);


void blinkfast(){
   digitalWrite(statusLed, LOW);
 digitalWrite(statusLed, HIGH);


void playNote(byte note, byte velocity){
 int value=LOW;
 if (velocity >10){
 switch (note){
  case 60: //mid-C
   digitalWrite(led1, value);
  case 62: // D
   digitalWrite(led2, value);



i just finished building the midi in circuit with the 6n138 on breadboard and tested it with your code, kuk.
seems to work fine.  ;D




i'm short on time right now, but i will post my schemetics (with 4n28) this evening, too. i got it working pretty reliable now!



Sep 25, 2006, 10:31 pm Last Edit: Sep 25, 2006, 10:32 pm by leKuk Reason: 1


Oct 23, 2006, 06:47 pm Last Edit: Oct 23, 2006, 06:59 pm by haesslich Reason: 1
as i reported the circuit with the 6n138 worked here on breadboard. but now i soldered the circuit on stripboard and nothing is working anymore :(
i tested the optocoupler (0V on the input -> LED light up on the output. seems to work). no idea what might be wrong now.

update: working now again! ;)


I've been trying for sometime to implement a midi input on Arduino : I'm planning to build a midi module out of the MEGADRIVE (Genesis) sound chip
I stuck with this midi input...
I made a optocoupler interface no problem, with a 6N138 that seem to receive every midi byte OK but with the code below from Kuk, it sort of work execpt it doen't when you make chords.
It seems that only the first 3 bytes (one note bytes) are seen, which I don't interstand because the program should read all the byte until there is no more in the serial buffer.
how can I use the midi polyphonically ??(excuse my french;-)

thanks for your help

Go Up