MIDI connection sense circuit - Page 1
Forum section: electronics / projects

author: sirhaggis
author_rank: Regular Contributor
timestamp: November 28, 2016, 01:52:40 pm
content: I'm currently working on a MIDI in+out circuit attached to an MCU, and had the idea to design a connection sensing circuit in so the MCU is aware when the user connects a port to a MIDI device. A MIDI bus is a pretty standard circuit as such:https://www.tigoe.com/pcomp/img/midi-schem.jpgIt seems like this should be possible as the protocol defines an idle high state, however it is not entirely clear to me how to best go about it. The protocol is defined in terms of current (5mA=0,0mA=1) with the input isolated by an optoisolator. In theory detecting a connection on an input should be a matter of measuring current input through the opto-isolator. Detecting a connection on an output would be a matter of measuring a current draw of about 5mA between pins 4 and 5. The circuitry in question would obviously need to ensure it does not draw any significant amount of current from the port or drop the voltage when performing the detection. I've never done any sort of high impedance current detection circuitry. Can anybody suggest possible approach(es)?Cheers,Luke

author: helius
author_rank: Super Contributor
timestamp: November 28, 2016, 04:21:10 pm
content: The 1984 MIDI protocol already has provision for link sensing, called "Active Sense": a periodic 1 byte status message. Nothing further is needed.Perhaps I should clarify: it is not generally possible to detect when an outgoing MIDI connection exists because the device on the other end of the cable can be turned off: optoisolation means that this is not visible to the transmitter. Detecting when an incoming connection exists is the function of Active Sense.

author: Cervisia
author_rank: Regular Contributor
timestamp: November 28, 2016, 06:34:15 pm
content: To detect whether an input port is connected to your output, just try to send Active Sensing messages and measure the current. With the 220 ohm resistors in the output circuit, you do not need a separate shunt. (Once you have sent an Active Sensing message, you have to continue sending it regularly.)Detecting whether an output port is connected to your input is not so easy. Most devices do not send Active Sensing messages, and might not send any other messages for a long time. Pin 5 is often an open collector output. If the device uses the circuit from the specification, you could try to measure the voltage between pin 4 (connected to Vcc through a resistor) and pin 2 (ground). But not everybody conforms to the specification; a few devices switch the current loop on pin 4, and some devices switch off the port power if they do not need it. And beware of ESD/EMI countermeasures that could interfere with your measurements (the specification mentions ferrite beads, but common mode chokes or huge RC filters are also used in practice).(And that circuit on tigoe.com is a bad way to use a 6N138; you should use a 1k pullup and a 10k base bypass (pins 5-7).)

author: sirhaggis
author_rank: Regular Contributor
timestamp: November 29, 2016, 03:22:47 am
content: QuoteBut if I understand the specification correctly even if a MIDI device swaps pins 4 and 5 there should still be a detectable voltage between either of them and pin 2 (GND) whilst idle, since it specifies idle to be high state. Measuring this and combining with some logic on the MCU should be able to give a reliable detector... If the device switches off the power port when not in use we can presumably still detect presence since the signal pin must still be in an idle high state (or would it completely disable its port... if so this is not a condition I intend to detect anyway).Even assuming pins 4/5 may be swapped, I could presumably put a voltage detector on both pins and OR the output to the MCU, then use some code to determine presence if a voltage has been seen in a specified time period?Assuming this would work, my main question would be how I would implement a high impedance voltage detector that maintains ground isolation? Some sort of opamp configuration perhaps? Any ideas?

author: Cervisia
author_rank: Regular Contributor
timestamp: November 29, 2016, 07:46:05 am
content: Quote from: sirhaggisThe specification specifies (p. 1):QuoteIt says nothing about the idlevoltage. The idle state just has to prevent the current flow; this can be done either by driving pins 4 and 5 to the same voltage (push/pull output), or by making at least one pin open (open-collector output). (The original specifcation does not say what kind the buffer should be; the 2014 update recommends an open-collector buffer because this works better when the output is short circuited.)When pin 4 is switched off, then all you see in the idle state is pin 2 as ground, and pin 5 connected to ground with a resistor.Anyway, there are very few devices that switch pin 4, so I'd just ignore this case.QuoteUse a second optocoupler with very high CTR (e.g., 6N139), and disconnect it when you've actually detected a connection. Or invent some clever trickery to use the same optocoupler for both detection and the actual receiver.

author: sirhaggis
author_rank: Regular Contributor
timestamp: November 30, 2016, 02:15:55 am
content: Great ideas, thanks for your help