Midi Input

For the life of me I cant manage to make an input midi circuit to work. I rearranged it a dozen times, tying different guides but get that led to turn on. I managed to make a midi keyboard receive notes but not output. I rechecked the midi connector pins, so what else could I be doing wrong?

Attached is a picture of my circuit.

Couple things to check:

  1. Assume you're using 5V power supply to 6N138?
  2. Check the resistor from Pin 6. The photo looks like it's only 47 ohms but the photo color could be off. Make sure it's 470 ohms (yellow purple brown / 471 )
  3. Check the input diode between pin 2 and 3. I don't see the band in the photo. Make sure the band is towards pin 2

antman49443:
Couple things to check:

  1. Assume you're using 5V power supply to 6N138?
  2. Check the resistor from Pin 6. The photo looks like it's only 47 ohms but the photo color could be off. Make sure it's 470 ohms (yellow purple brown / 471 )
  3. Check the input diode between pin 2 and 3. I don't see the band in the photo. Make sure the band is towards pin 2

Hi,
Its 5v to pin 8. Multimetar read 450 ohms from the resistor, and i checked it online its 470 (yellow, purple, black, black, brown). The diode is towards pin 2. Dont know what else it can be, maybe output midi on my keyboard is not working although input port did.

Can you post a schematic of what you are trying to make please.

Grumpy_Mike:
Can you post a schematic of what you are trying to make please.

Essentially what I am trying to accomplish is playing a specific few notes in a row on a keyboard will open a solenoid.
I tried to make this by following this guide as well as this one. Couldn't make them work. Schematics are in the links.

We need to know the schematic you were trying to follow in those pictures you posted. Then we need to see the code you are using when you use that schematic.

Ofocurse, here is the schematic, and the code.

/*Receive MIDI and check if note = 60
By Amanda Ghassaei
July 2012
<a href="http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/">

http://www.instructables.com/id/Send-and-Receive-...>

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.

*/

byte commandByte;
byte noteByte;
byte velocityByte;

byte noteOn = 144;

//light up led at pin 13 when receiving noteON message with note = 60

void setup(){
  Serial.begin(31250);
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
}

void checkMIDI(){
  do{
    if (Serial.available()){
      commandByte = Serial.read();//read first byte
      noteByte = Serial.read();//read next byte
      velocityByte = Serial.read();//read final byte
      if (commandByte == noteOn){//if note on message
        //check if note == 60 and velocity > 0
        if (noteByte == 60 && velocityByte > 0){
          digitalWrite(13,HIGH);//turn on led
        }
      }
    }
  }
  while (Serial.available() > 2);//when at least three bytes available
}
    

void loop(){
  checkMIDI();
  delay(100);
  digitalWrite(13,LOW);//turn led off
}

That code comes with a warning, it says Instructables, this is code for what follows is a load of crap written by someone with an inflated idea of their own ability. Use only if you know way more than the person who wrote it.

That code shows that the writer had no idea about MIDI.

There is just so much wrong with it. Unless your MIDI is composed entirely of three byte messages it will get stuck and never show anything.

Scrap the crap.

Just write a program that changes the state of an LED each time anything is received, that will be enough to check out your hardware. Get that blinking first before worrying about trying to make sense of your message.

I also tried with this one, didnt work for me.

#include <MIDI.h>  // Add Midi Library

#define LED 13    // Arduino Board LED is on Pin 13

//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();

void setup() {
  pinMode (LED, OUTPUT); // Set Arduino board pin 13 to output
  MIDI.begin(MIDI_CHANNEL_OMNI); // Initialize the Midi Library.
  // OMNI sets it to listen to all channels.. MIDI.begin(2) would set it 
  // to respond to notes on channel 2 only.
  MIDI.setHandleNoteOn(MyHandleNoteOn); // This is important!! This command
  // tells the Midi Library which function you want to call when a NOTE ON command
  // is received. In this case it's "MyHandleNoteOn".
  MIDI.setHandleNoteOff(MyHandleNoteOff); // This command tells the Midi Library 
  // to call "MyHandleNoteOff" when a NOTE OFF command is received.
}

void loop() { // Main loop
  MIDI.read(); // Continuously check if Midi data has been received.
}

// MyHandleNoteON is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 
  digitalWrite(LED,HIGH);  //Turn LED on
}

// MyHandleNoteOFF is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { 
  digitalWrite(LED,LOW);  //Turn LED off
}

OK what part of:-

Just write a program that changes the state of an LED each time anything is received, that will be enough to check out your hardware. Get that blinking first before worrying about trying to make sense of your message.

Are you having trouble with?

You have to prove the hardware first before delving into decoding it. No matter how good you are you have to take the smallest step you can at a time, test it and get it working. Otherwise there is too much that can go wrong and without test equipment you will find it hard to work out what is wrong.

If you have an oscilloscope the look at the signal on the RX input, does it change? If not can you see any change in the voltage level of the input side of the opto isolater.

Grumpy_Mike:
OK what part of:-Are you having trouble with?

You have to prove the hardware first before delving into decoding it. No matter how good you are you have to take the smallest step you can at a time, test it and get it working. Otherwise there is too much that can go wrong and without test equipment you will find it hard to work out what is wrong.

If you have an oscilloscope the look at the signal on the RX input, does it change?

I'm quite new at this actually. That last piece of code is from another guide i tried just in case it might work. Don't have an oscilloscope.

Grumpy_Mike:
If not can you see any change in the voltage level of the input side of the opto isolater.

I'm sorry can you expand on this further?

That was what to do if you did have an oscilloscope.

If you do not then do what I said in the previous post. You can not just go round steeling code that you don't understand.

You have an error somewhere and I am trying to help you find it. But I can only help if you do the things I ask because only you have the none functioning system.

If you don't understand what I am asking you then ask me.

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.