As soon as I had the components in my hands, I was so excited that took the soldering pen and in a hurry I have mounted this:
I felt lucky and I was so anxious to have everything working in my hands, that I have mounted everything directly on a perfboard, in order to see those leds blinking. Well, don´t do something like that. It´s simply stupid. If it´s not working (which is very probable when it´s a first attempt), then what you have is a mute soldered brick in front of you, and no easy way to find the problem. Changes in the circuit require removing already soldered components, you can burn the pads, burn the components, break the pins, get annoyed, waste time. Basically I wanted to test the MIDI circuit first. My initial idea was to build the MIDI interface and to connect it to the UART of the Arduino. I only wanted to light up different LEDs and practice a little with the code.
The MIDI interface
For the MIDI interface I have used this diagram, which comes straight from the Midi Manufacturer Association http://www.midi.org/
The components are not so critical and the circuit is pretty simple (we need only the first part of the circuit for the MIDI-in). The UART of the CPU is electrically isolated from the MIDI cable with the optoisolator, the backward diode protects the internal LED of the optoisolator from reverse voltage and the resistor limits its current. I have used a 4N28 for the optoisolator and a 1N4148 for the diode. You can see the complete diagram here
Oh, I have always had problems fitting the connectors in the perfboard. The holes are too tight for those pins. So I have posted a workaround here 🙂
Going back to our story,
for instance, my main problem was the midi connector. I could not understand from which side are you watching the connector in the schematic above. Male and female connectors are numbered differently. To make it short, I made a mistake with the midi connector, but it was not easy to figure out why and where, especially because I don´t have a good instrumentation such as an oscilloscope, etc…
Finally, this is what I have learnt and can be useful for others:
Four basic suggestions to make your circuits work
- Breadboard everything first; sometimes you have to discover something by trial and error (like the right numbering of the midi connector);
- Add feedbacks to your circuit. There´s nothing like a mute circuit to make you waste hours in finding where´s the problem. Maybe you blame your circuit and your MIDI device is not sending anything.
- Build your things LEGO style, brick after brick, and make sure your last step is solid enough before proceeding with the next.
- Invent your instrumentation. Even if you don´t have professional instrumentation in your house, you can still find a way to make the measurement you need in your circuit. A simple LED can show you activity on a serial line, and you may also be able to recognize patterns. It´s not a logic analyzer, but still better than nothing!
After reversing a couple of times the MIDI connections,changing diode orientation, etc, I had to put aside my soldered shit. Then I started again from scratch applying the 4 golden rules above.
- First I checked the polarity of the MIDI cable. I opened the cable, and saw that only two pins were actually used in it (+ shield);
- I have connected a LED to the two cables, in the same way the LED of the optoisolator is connected. I have double-checked the right polarity of the connector by seeing that the led was flashing. I haven´t investigated it in depth, but actually you can see from the blinking when a MIDI device is initialized and when it is transmitting messages.
- After that, I have removed the LED and put the optoisolator. Instead of using only the resistor Rd shown in the diagram, I have put a LED + a resistor. I have left it there. It does not harm and it´s useful to display the status of the MIDI. Thanks to this now I am able to detect when I need to reset the MIDI driver in the PC. Sometimes the PC goes in stand-by and the MIDI interface is stuck. In that case, no blinking.
- When all of above was stably working, I could connect the serial interface of the Arduino and start programming 🙂
For the sake of completeness, this is the right polarity of the female MIDI connector. Numbers were confusing for me. Male and female have different numbering, I’ll show you where to connect the positive (cathode) and the anode of the ouptocoupler’s led: