I asked this question also in Electrical Engineering (stack exchange), however on question is more appropriate on this board (question 5). For completeness, I kept all questions intact, but made question 5 bold.

Electrical Engineering StackExchange Question Link: Using a MIDI connector both as MIDI In and Out (not simultaneously),

In my current (Arduino) project I have (currently) 3 MIDI INs and 2 MIDI OUTs. However, it would be nice if I can configure each of the MIDI connectors as IN or OUT by software (Arduino).

The circuits I am using are:




What I would like is to use the connector shown in the above picture as MIDI In OR MIDI out. I have not tried it yet, since I have too many doubts and I do not want to break a component. However, I thought about it (with my limited electronic knowledge), and I think:

  1. Pin 2 afaik for MIDI In, normally has +5V. In the MIDI Input circuit it is not connected, but in the MIDI Output circuit, it is connected to GND. I don't think this is good when being used as MIDI out, resulting in a short circuit (?). So I guess I have to make it +5V when used as MIDI Input and GND when used as MIDI Output (so putting HIGH or LOW 'digital' for Arduino).

  2. Pins 1 and 3 remain in both circuits untouched (so I assume I can do the same).

  3. Pin 4 and 5 is a bit tricky, since they are connected. I think I have to use one or two transistors to create two alternative paths for pins 4 and 5 for MIDI Input and Output. Or can I just set a specific voltage on pins 4 and 5 when used as MIDI In or Out? (and what would those values be?).

  4. For the MIDI input, an optocoupler is used, while for MIDI output, directly the Arduino is used. I wonder if I have to use for both an optocoupler (since people might connect it accidentally to a MIDI output from a MIDI device.

5. I'm not sure how to handle the RX and TX pins ... for 5 MIDI devices, I would need to use 5 RX and TX pins, but the Arduino (Mega) only has 4 of each. But this is probably a question for the Arduino board.

  • 1
    Could you check over your question for typos in the pin numbers, please? I got confused at the first part about lin 3 - do you mean pin 2?
    – Mark Smith
    May 18 '17 at 6:17
  • @MarkSmith Thanks ... I indeed made a mistake (and also in item 1, where I meant pin 2). In item 3 I only wanted to mention pins 4 and 5. May 18 '17 at 6:32
  • 1
    See the softwareserial tag.
    – CL.
    May 18 '17 at 7:33
  • @CL: I think I should really do some testing for MIDI with software serial. May 19 '17 at 19:07

I suggest you have a look at the 4051 or 4052 chip (datasheet). The 4051 is a 1:8 analog multiplexer while the 4052 contains two 4:1 multiplexers. So basically it allows you to switch one pin on your Arduino to any out of 4 or 8 MIDI circuits.

In the case of TX, it's really simple. You just need a pullup resistor on each output so that the pins don't float when not switched to.

In the case of RX, you have to make sure that you don't miss any transmissions. If your MIDI sources are not sending simultanously, that's easy as well... but probably they are. In that case you cannot use an existing RX UART. Since MIDI is very slow, you could still switch back and forth between the inputs and catch all the bits (you would be writing a software UART then). But if you're not short of pins, it would be much easier to just use SoftwareSerial for one input.

  • Thanks for the answer, for output this would work great. I tried before using a software serial, but didn't work out well. However, good to know that at least for output there is a good way. Jan 1 '18 at 18:03
  • I'm pretty sure this would also be a good way for RX. It's just more work to implement, but MIDI is really so slow that you could mux between several ports and still catch all the bits. I'm doing serial reading by bit banging at 160 KHz on an ATtiny running at 8MHz, so a 16MHz Arduino should be able to handle roughly 10 MIDI inputs through one multiplexed digital input pin.
    – travelboy
    Jan 2 '18 at 7:57
  • Hmm I tried long time ago (this is an old post) and I got regularly missed MIDI messages. Jan 2 '18 at 10:20
  • As MIDI is not clocked, it's crucial to get the timing right. You've got 32 microseconds per clock cycle which is not too bad, so once you detect the start bit, the data bits should be sampled at t+32us, t+64us etc. I'm getting curious what your application is :)
    – travelboy
    Jan 2 '18 at 19:58
  • My app is initially to build a device that can receive different MIDI channels and 'process'/change these and send it back to other MIDI channels (or different messages). The message processing should be very configurable (so software is more complicated, but I am a programmer). Jan 2 '18 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.