I'm trying as a starter to my project to read incoming MIDI messages from a 5 din midi connector to any of my serial Rx pin of my arduino Mega (ideally I'd like to have it on serial 1 2 or 3).
Neither I see something on my screen nor I see the Rx led blinking or showing any activity.
I'm using a PC817 optocoupler I've read this one isn't ideal but I saw some threads with people making it work.
Here's what I tried so far to make it work :
I added a LED between the optocoupler and the wire going to Rx pin to see if something is happening, and I see something : the led blinks regularly and if I press a key or turn a know on my midi keyboard this LED blink very fast but nothing happens on the screen.
I also tried to switch the wiring of pin 3 and 4 of the optocoupler without success. (The led blinks the same but with much less intensity if I connect pin4 to GND and pin 3 to the LED / Resistor / Rx wire stuff
I tested the Rx1 port of my Mega on pin 19 by sending messages from another arduino and it worked.
I'm also attaching a picture of what I did and the relevant part of my wiring diagram (without the led)
Your schematic is definitely wrong. At least you must interchange R1 and R4, and D1 is the wrong way round.
And the PC817 isn't a good choice - it may or may not work. Dependig on your individual unit. Usually it is too slow.
Thanks for your answers !
I rewired everything from scratch as per the diagram you showed me (didn't wired PIN2 to anything) with no more luck. Therefore I ordered a 6N13x optoisolators and I'll update this post as soon as I'll have tested it.
Regarding the setting of serial baud rate I'm following the doc of the midi library which nevers sets it, therefore I assume either MIDI_CREATE_INSTANCE or MIDI.Begin does that for me.
But just to be sure, I changed my code to something simplier which starts serial1 and read bytes in a do while loop but it doesn't worked neither. (and this code works I can test it with another arduino which sends bytes)
Meanwhile I receive my ne optoisolator I'm curious of one thing :
As the LED I set at the output of the PC817 definitely lights up as I press notes or mode knobs and faders why don't I even any single byte on my serial entry ? Shouldn't I see at least some gibbershish stuff on my screen?
Might be a stupid question but as an IT guy I understand quite well the programming part but the electronic part is quite obscure for me.
No, we were talking about baud rate, and baud rate is fixed to 31250 as defined by the MIDI standard. So it doesn't depend on which UART you're using, and it could mean a MIDI library should already and autonomously set the baud rate of the device you pass to the constructor. And the library examples clearly show the baud rate is never externally set.
The lib has to know that in any case, not only to set the baud rate. How would it be able to receive MIDI bytes if it didn't know where they come in? And if it knows at which serial line the bytes come in, it's also able to set the baud rate at that line. No need to do that in the sketch by yourself.
Here is a quick example of setting up a serial input. Note how you have to call the ".begin instant in your code. I see no use of this structure in any code referred to in any example on this thread.
In my second sketch where I don't use MIDI.h this is exaclty what I'm doing, and this code works as I can read bytes sent from another arduino. It's just that my "custom made midi shield" is silent.
If you use structs and classes from midi.h lib you're not supposed to use "serial. begin()" as it's done by the library.
Here's the content of begin() method from "SerialMidi.h" file included in the lib :
struct DefaultSerialSettings
{
/*! Override the default MIDI baudrate to transmit over USB serial, to
a decoding program such as Hairless MIDI (set baudrate to 115200)\n
http://projectgus.github.io/hairless-midiserial/
*/
static const long BaudRate = 31250;
};
void begin()
{
// Initialise the Serial port
#if defined(AVR_CAKE)
mSerial. template open<Settings::BaudRate>();
#else
mSerial.begin(Settings::BaudRate);
#endif
}
So either I fully use Midi.h and MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midi6pin); knows by definition that midi6pin will use the pin 19 of my Mega (defined by HardwareSerial and Serial1 in the call with a baudrate of 31250 as defined in the lib) as in my first example.
Or I totally forget about this lib and use serial1.begin(xxx) and so on as in my second example (which works in other context that midi).
I'm quite convinced the issue I have comes from the electronic part of my "setup".
What exactly do you not "get", about what I posted?
Yes you are.
My example was taken from my working code where I was using the MIDI library to get another serial port for a project I made to get another output.
Yes that is one of your problems. This is another you will come across once / if you can get your hardware right. Which given the opto couplers you have decided to use is very debatable.