Isolating switch lines - Page 1
Forum section: electronics / beginners

author: Muffins
author_rank: Regular Contributor
timestamp: December 03, 2019, 07:07:30 am
content: Hi guys.I am trying to use switches to input high/low signals into a microcontroller. They are connected to wires a few meters long.I was looking for an easy way to isolate the microcontroller since I have previously experienced some noise problems when doing this. One way to do this seems to be to use an optocoupler between the switch and microcontroller as attached.My question is, how do I go about powering the switch circuit? Is this even a good way to approach this?

author: Muffins
author_rank: Regular Contributor
timestamp: December 03, 2019, 10:14:36 am
content: Here is the second iteration of the circuit.Am I being dumb thinking that separately powering the switches and isolating them is going to help prevent EMI from effecting my controller?

author: jhpadjustable
author_rank: Frequent Contributor
timestamp: December 03, 2019, 10:40:46 am
content: You were probably seeing the wire's self-inductance imposing transient voltages across your pull resistor, and injecting that into the internals of your micro and its power supply. The first version would be fine, because the long wire's transients stay out of the micro itself. In fact, I think you could get away with a simple, non-isolated transistor in place of the opto.

author: Muffins
author_rank: Regular Contributor
timestamp: December 03, 2019, 10:49:09 am
content: Would it be a good idea to use a twisted pair wire for the switch? If inductance is a contributing factor.

author: max_torque
author_rank: Super Contributor
timestamp: December 03, 2019, 11:25:36 am
content: Are you confusing debouncing or low pass filtering for isolation?Isolation, ie the input does not share the same ground potential as the controller is used primarily for safety or functional reasons.Debouncing,low pass filtering or similar techniques for noise reduction and rejection are used to prevent spurious triggering or a loss of funcitonality due to noise or transmission line effects.You haven't told us with the switches do, or how they are actuated, but taking a guess that they are pressed by a human, the good news is that this means, in microcontroller terms, the reponse of your system can be glacial! That means you can easily introduce a large amount of filtering without impacting on the performance of the system. For example, it takes a human around 100ms at best to press a switch. A delay of even 200ms is realistically mostly impercepatble, but that delay would still allow a 5hz input. Chances are, your "noise" is likely to be at an above mains frequency (50 or 60 Hz) so it's trivial to arange a simple low pass filter to stop the noise, but pass the signal.

author: Ian.M
author_rank: Super Contributor
timestamp: December 03, 2019, 01:09:44 pm
content: Optocouplers are the 'gold plated' way of keeping nasty and unwanted voltages away from your MCU pins, however they are often overkill, and frequently misapplied.I note that on your second diagram, you've labelled the swich 'Limit' which implies its on a machine of some sort. Therefore the need for proper isolation of the switch circuit (with no common ground) is much higher than we were anticipating and an optocoupler may actually be the correct solution.However your optocoupler should have an anti-parallel diode across its LED to protect it from reverse current (e.g, 1N4148, cathode to LED anode), and also a resistor in parallel so it doesn't start to turn on at small leakage currents. Typically the parallel resistor may be 10x the value of the one feeding the opto-LED. To prevent *MUCH* frustration debugging sensor inputs, each optocoupler should have a visible LED (+ series resistor) across its phototransistor output's pullup resistor, for visual indication that the sensor is active and the opto input circuit is working correctly.Also, I would recommend changing the order of the components so the switch goes to ground i.e. is on the low side of the opto LED. Assuming the opto-LED has a max current rating of 50mA, if you make the feed resistor 1K 1W, its going to be capable of withstanding an input shorted to over 30V without damage. Why do you need that? Well what's the supply voltage to the machine's motors, and do the motor and sensor cables go through the same cable chain, which would present a risk of drive to sensor shorts if a motor cable's insulation ever fails, which can easily happen if a motor cable fatigues enough to break most of its strands and develops a hot-spot. Also there is the risk of mis-connection due to two left handed, rocks in their heads technicians (or yourself on a bad day)!https://en.wikipedia.org/wiki/Murphy%27s_lawIf your product has to survive room temperature IQ 3rd party technicians mis-connecting it to mains supply voltages, or at least die in a way that it can detect and indicate, talk to us about your options! Designing inputs capable of surviving 250V AC without damage *is* possible, but expensive.For reliability, its generally recommended to design for a minimum of 10V across and 10mA through a switch that's providing a status or control input. Seehttps://en.wikipedia.org/wiki/Wetting_current10mA works well with most OPTOs, and a 12V sensor supply would meet the >10V OC voltage recommendation.Make the sensor supply available (with over-current protection) at the input connector to accommodate future replacement of mechanical or reed switches with powered active sensors if the former often become unreliable in service.Lastly,isolated DC-DC convertersare reasonably affordable, certainly much cheaper than a second mains to low voltage DC PSU. They are available in a range of input and output voltages and power ratings. A 5V to 12V 1W one drawing up to 200mA from the 5V rail would typically be good for around 75mA out so could run several switches. I'd protect its output with a reasonably beefy anti-parallel 15V Zener diode (e.g. 1N5929, cathode positive) directly across it, then a polyfuse in series, of a current rating no more than 2/3 the DC-DC converter's max output current. That provides reasonable protection against external voltages being mis-applied to the DC-DC converter output up to the polyfuse voltage rating, and against short-circuits. That would be good for up to four switch circuits at 10mA each. Go up to a 2W DC to DC converter and a 100mA polyfuse, and you'd be good for up to eight circuits. Above that, protecting the DC-DC converter against external overvoltage becomes more of a PITA so if you have a high number of switches discuss your options with us.N.B. try to avoid cheese-paring cost savings e.g only providing a single pair of ground and supply pins for a number of sensor input pins becomes a royal PITA for future maintenance when trying to diagnose and replace a faulty sensor as you cannot fully disconnect one without disturbing the others.

author: Muffins
author_rank: Regular Contributor
timestamp: December 03, 2019, 03:11:47 pm
content: Ian is spot on. The switch is an end of motion limit for a machine axis.I'm using aTeknicservo motor as a drop-in replacement for a stepper motor for driving the axis. Not really using the motors fancy features yet so the end limits are important.I've had some issues with EMC. My previous circuit had some filtering on the inputs but wasn't shielded from radiated emissions. Someone was arc welding nearby when the motor was active and the 100kg slide shot into the end of its axis. I've also got some reports of the axis moving on its own, as if someone had pushed the stop/go buttons. The last thing I noticed was when I was replacing limit switches with the system turned on there was erratic behavior of the controller. I'm more involved on the software side of things so this has been very difficult to put something together. I've mostly forgotten why I designed that circuit the way I did so I'm starting fresh and making notes.Ian, I've been carefully through your post, it's late in the day now but please see attached image for my interpreted circuit diagram.You say that for reliability a minimum of 10v is recommended for control inputs. Why is that? What would happen if the voltage was lower?I'll be sure to avoid cheese-paring cost savingsedit:I have included a diagram of the previous input protection I used.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: December 03, 2019, 03:28:47 pm
content: One thing to note, a lower supply voltage means RFI gets rectified by those clamp diodes.An otherwise stable logic level can be pushed into the transition range this way.Expect excursions on the order of 10V in an industrial environment; that's a hand-waving good reason to guess 10V+ of supply. RS-232 for example has a nominal +/-15V level (single ended signaling), or RS-422 has a +12/-7V common mode range (differential signaling). And common industrial controls are 24V (AC or DC), or 120VAC, etc. (Or 4-20mA, but note they are usually half-assed with common ground circuits and the current loop isn't nearly as clean as it should be; you only get true current mode operation with a lot of care, often simply using an isolated transmitter/receiver. Which of course applies for anything else that needs a lot of noise margin, isolation that is.)So it can help to filter first, then ESD. Matter of fact, you may find it's better to exactly swap the order of components in that circuit -- put the 10R between the ESD diodes and the logic receiver (its purpose is simply to limit ESD current into the logic device, not to prevent it entirely), and put the 1k resistor and cap out in front (for filtering).Maybe a ferrite bead as well (helps cut down on RFI), but note that ferrite beads only have a peak impedance of, whatever they have; whereas resistors are, well, resistors for most frequencies, so putting a 1k resistor facing the outside world will do more than a 300R or even 1kR ferrite bead/chip can. And even moreso for 10k or whatever.Ferrite beads are preferred for logic outputs however, where the low DC resistance is valuable.Tim

author: Ian.M
author_rank: Super Contributor
timestamp: December 03, 2019, 04:01:38 pm
content: No. the 10V, 10mA rule of thumb is to ensure sufficient voltage and current to break through the oxide film that typically forms on switch contacts.For signal integrity in a high EMI environment, you may well need a higher voltage, and to increase the ON state threshold e.g. by adding a Zener in series with the Opto-LED.Referring to your diagram optocoupler3.PNG, the protection Zener for the isolated DC-DC goes across its OUTPUT, *NOT* bridging its isolation barrier.

author: Muffins
author_rank: Regular Contributor
timestamp: December 04, 2019, 01:47:25 pm
content: Thank you for the input guys, it has been really helpful. Let me just set aside some time to process it all and I'll follow up after that.Gonna hopefully move forward with a pcb by the end of the week.

author: Muffins
author_rank: Regular Contributor
timestamp: December 05, 2019, 11:02:22 am
content: okay so I've tried to adapt the circuit for outputs as well. Please see attached diagram. In this case the servo motor has built in optocouplers other hardware might be used in its place.The power supply and dc-dc converter is just based on what's available with a local supplier. 5W is overkill.I'll do some reading up on ferrite beads today, I've never used them before.

author: max_torque
author_rank: Super Contributor
timestamp: December 05, 2019, 12:20:31 pm
content: Wow, that's a lot of effort, cost and complexity for a switch input....(and sitill hasn't got any low pass filtering or debouncing in it)The reason an optocoupler often "fixes" a noise problem is the fact that is has a relatively low input impedance. Unless you need to isolate ground potentials, then you gain nothing by using one, nothing that couldn't be gained cheaper and simpler anyway.A typical microcontroller GPIO port has an input impedance to ground of substantially larger than 100,000 ohms (and it can be WAY more). That means that any current, even a tiny amount, that is pushed down into that port generates a large voltage (V = I x R ) Say you need 2v to trip into logic level high, at 100kohm, that's just 20 micro amps! So, if you don't put any circuitry in front of your input port, and the wiring to the switch picks up differential noise, then that noise only has to generate a tiny amount of current to falsly flag a high level at that port. And assuming we haven't got a wiring fault that actually allows a DC voltage offset onto the wiring harness, then the noise that is coupled in will be AC, and so without any low pass filtering, you will get a number of "high" triggers on the port.Assuming you are using micoswitches, then your current system also does nt in anyway protect against contact bounce. Now sure, you can do that in software only if you want, but given that the circuit to help block bounce is a low pass filter, and you also want that to block AC noise, why not put in place some hardware to achieve both those aims!What you need to consider is what frequency you want to be able to pass? For a limit switch, the actual "trigger" position will depend on lots of factors, the mounting of the switch, the precise tollerance of the switch and it's levers and arms themselves, and of course, how fast the carriage is moving itself. ie how far does it travel before the "switch tripped" signal registers. Now luckily, mostly for limit switches you don't actually care precisely when/where they trip, as they are just used to prevent the carriage running into the hard mechanical end stops. If you are however using those switches to set up or "zero" the system, then you need to ensure that the position at which the switch trips is repeatable. You probably still don't care exactly where it is, just that it happens in the same place each time. You will see many machines (3d printers, CNC mills etc) doing a rapid carriage traverse till they hit the end stops, and then moving back a short distance, and re-hitting that stop switch at a much lower speed in order to try to precisely and repeatibly estimate that end stop positionWith this in mind, i'd personally use a 24v (or 12v) voltage to my switch, i'd then pass that voltage through a relatively low impedance voltage divider to ground to get a signal into the voltage range of the input port of my micro, and i'd include a suitable capacitor to form a low pass with the voltage division resistors, and a zenner or diode clamp to the rails, and then another higher value series resistor to protect the micro's own internal diodes. If i were really paranoid, you could include a zener in the switch input as well to push the voltage threshold upwards.At 24v to avoid excessive power loss in the voltage divider, you'd want that resistance to be greater than about 2.4 kOhm, so assuming a 5v micro, i'd suggest a 2k input resistor, with a 470 ohm resistor to ground. Thats a nice low impedance for the switch to drive (and gives 10 mA through the switch contacts) and 24v input results in 4.7v at the output of the divider. For a typical TTL GPIO port, that means "high" occurs at inout voltages above about 13v, and "low" below about 4v.If you put a 1uF capacitor after that divider, that's a low pass with a cut off (-3dB) at around 80Hz. You may need to experiment with this capacitor value to find the sweet spot in terms of blocking AC noise, but allowing a quick enough response for the real signal.I'd also put a 4.7v zenner to gnd or use two diodes to clamp that point in the circuit to Vcc and GndBetween there and the micro pin i'd finish by adding a 10k series resistor to protect the micros port protection diodes.So, that's 3 resistors, a capacitor, and a zenner (or two diodes) for a total cost of about, well, nothing really. No need for isolated supplied, optos or any of that stuff!

author: Jeroen3
author_rank: Super Contributor
timestamp: December 05, 2019, 12:28:08 pm
content: People often use the shotgun method and go opto isolated while the majority of the problems are caused byhigh-impendant inputs! Those are easily fooled by the smallest amout of inductive or capacitive coupling due to interference from un-snubbed relays turning off or VFD's operating nearby.Follow the standards. Get that current flowing before detecting a high!Also reference material, although this does add input group isolation. SilabsAN970

author: Ian.M
author_rank: Super Contributor
timestamp: December 05, 2019, 12:34:53 pm
content: If you still need optoisolation on the inputs (and it does provide useful protection against damage from idiots shorting input ground to a high current AC or DC supply, which without isolation can easily blow the sh!t out of your controller as it burns the ground tracks off your PCB!), I suggest:Put a polyfuse on the DC-DC converter output, after the shunt Zener where it can do some good if sensor Vout+ gets shorted to the motor supply. You probably don't need one on its input if the SMPSU feeding it is short circuit protected at a low enough limit not to burn any tracks off your board.N.B. the shunt Zener on the DC-DC converter output should *NEVER* normally conduct, so check datasheets carefully and make sure that there is an adequate margin between its minimum breakdown voltage and the converter's abs. max. output voltage, allowing for tolerances of both. Its got to be beefy enough to trip the polyfuse without blowing, and once you are past a few tens of mA, you may need to use a unidirectional TVS diode instead of a Zener to get a high enough surge rating.No comments yet on your output side optocoupling to the motors. Please link any applicable motor or driver datasheets!