Midi Input

I appreciate the help, I know explaining seemingly simple things to a beginner requires patience, but its all still magic to me.

You said I need to test the hardware with a blinking LED. How do I start with this?

I told you in reply #7 and again this was quoted in reply #9.

Write code that whenever there is a serial input byte in the buffer read it and change the state of the led. Just that.

int incomingByte = 0;  


void setup() {
        Serial.begin(9600);     
        pinMode(13,OUTPUT);
       
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                digitalWrite(13, HIGH);
}
                else
{
                digitalWrite(13, LOW);
}
        }

I am not sure if I am doing this right. Pin 13 doesn't go high when I press a keyboard key.

I am not sure if I am doing this right. Pin 13 doesn't go high when I press a keyboard key.

Well with that code it will only go high for less than a millisecond before it puts it low again.

I didn't say set it high, I said:-
change the state of the led

int incomingByte = 0; 
boolean ledState = LOW;

void setup() {
        Serial.begin(9600);     
        pinMode(13,OUTPUT);
     }

void loop() {
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();
               ledState = !ledState;
               digitalWrite(13, ledState);
          }
}

If the input hardware is working then then LED will change state on every key press.

So the led stays low on button presses, so obviously I'm doing something wrong or there is a problem with the hardware. Even though I managed to output to the keyboard, I wonder can the out port on the keyboard be faulty.

I wonder can the out port on the keyboard be faulty.

OK so now we can do some tests to find out.
First off make a logic test probe, sounds complex but is just a resistor and an LED, like this:-

logic_probe.jpg

Now connect this to the output of your keyboard, try it both ways round. You should see a flicker when you press a key.
Now put that in parallel with the input side of your opto isolator. With A to the anode of the opto's LED and B to the cathode. Again it should flicker to show the signal is getting that far.

Finally connect it to the output transistor of the opto. With A to +5V and B to the RX input of the Arduino. You should see a flicker on a key press.

If any of these fails then you know the signal is not getting that far and look to the wiring, connections and components from that point, to the last point where it worked.

Grumpy_Mike:
Now connect this to the output of your keyboard, try it both ways round. You should see a flicker when you press a key.

So A goes to pin 4 or 5 on the midi connector and b to ground?

So A goes to pin 4 or 5 on the midi connector and b to ground?

No there is no ground on a MIDI output.

Connect between the two MIDI signals, that is the middle pin either side of the center.

Grumpy_Mike:
No there is no ground on a MIDI output.

Connect between the two MIDI signals, that is the middle pin either side of the center.

Alright the led flickers constantly, but when I press a key it flickers more bright.

Grumpy_Mike:
OK so now we can do some tests to find out.

Now put that in parallel with the input side of your opto isolator. With A to the anode of the opto's LED and B to the cathode. Again it should flicker to show the signal is getting that far.

Finally connect it to the output transistor of the opto. With A to +5V and B to the RX input of the Arduino. You should see a flicker on a key press.

If any of these fails then you know the signal is not getting that far and look to the wiring, connections and components from that point, to the last point where it worked.

I am supposed to put the probe while the whole circuit is still in place right? If i put a and b on pins 2 and 3 of the optocoupler it doesnt flicker. I tried rearanging a and b, the leads from midi pins 4 and 5 to the opto and the diode, nothing changed.

You are trying to track down the fault.
You have established that data is coming from your keyboard.

If yo get nothing connecting the probe LED to the back of the input socket then remove the diode, still noting remive the opto. Still nothing then your cable or socket is faulty.

So, i removed the diode to test the input side of optoisolator. The led flickers only when everything is the other way around: A side of the Led probe to pin3 (anode) and B to pin 2 (cathode), and when the leads from the midi conector are the opposite then they should be: pin 5 midi to pin2 opto and pin 4 midi to pin 3 opto.

The led flickers only when everything is the other way around:

That sounds to me like the classic MIDI pins are the wrong way round. There is confusion as to whether the pins are numbers looking into the plug or looking into the socket. So reverse the connections to the socket and see what happens.

I already checked that and made sure I'm looking at it right. I already tried reversing them, this is the only way the led flickers. When I put the diode in the right way, led stops flickering and if I reverse it so that the black part (cathode i guess) is to pin 3 on optocoupler it flickers again.

If the fitting of the diode stops the serial signal either the diode, or connections are the wrong way round.
The job of that diode is to protect the LED in the opto from reverse voltage and if fitting it stops the signal then the signal is inverted.

Grumpy_Mike:
If the fitting of the diode stops the serial signal either the diode, or connections are the wrong way round.
The job of that diode is to protect the LED in the opto from reverse voltage and if fitting it stops the signal then the signal is inverted.

Ok, I understand that, but the midi connections on input side of optocoupler are reversed and so if I put the diode according the schematics led stops flickering. If the diode is reversed aswell the led flickers. Led flickers if the input side is completely reversed. Reversing it to the proper place according to the schematics stops flickering.

The little circle in the up right corner on optoisolator should designate pin 1 right? If the input side of opto is arranged according to schematics led doesnt flicker but if I reverse just the optoisolator so input is connected to pins 6 and 7 the led flickers? Should that happen, or am I starting to be desperate here for finding reasons why doesnt this work.

EDIT: I got input side of opto working according the schematics, but now dont have any response with the probe on the output side of opto. 5v and rx pins do not flicker, tried reversing.

The little circle in the up right corner on optoisolator should designate pin 1 right?

Correct.
this is when looking down on that spot. The pins are numbered anti clockwise from this point.

if I reverse just the optoisolator so input is connected to pins 6 and 7 the led flickers?

The LED flickering means that something is not shorting an output, so there are lots of useless configurations where the LED will flicker.

Grumpy_Mike:
The LED flickering means that something is not shorting an output, so there are lots of useless configurations where the LED will flicker.

I'm supposed to test the output of opto with the probe at 5v and rx. Got no repsonse, reversed either way. The signal stop in the optocoupler?

So either a faulty opto or not wired like you think. Like the bread board is not making good connections.

I got a pack of 1o optoisolators and even though I tried replacing them before, this time it worked. Led on pin 13 flickers even if no key is pressed just like the test probe but when I press a key on the keyboard the pin 13 responds with a brighter flicker. I have to mention there is no resistor between pin 5 and 7, there should be a 10k resistor but when i put one in flicker response stops. Is that weird, how does that affect the circuit?

Thank you very much for your guidance and patience.