Relays and Optocoupler - When is it required? - Page 1
Forum section: electronics / beginners

author: anfilt
author_rank: Newbie
timestamp: May 23, 2021, 01:56:38 am
content: So I was looking at various schematics for driving a relay. One things I noticed often when a micro-controller is involved they often also include an optocoupler. However, when the relay coil voltage is the same, like a with 5V MCU and 5V coil I noticed a lot designs still include the optocoupler, is this mainly done to limit noise or similar when the coil is switched off? Like the coil already provides isolation from the load being driven so that does not seem to be the reason. What made me think about this a little more closely is because I also saw some designs without an optocoupler when the driving the switching transistors.--Summary--It make sense to have one when the voltage is much different because if one of the switching transistors failed it could also blow up your controller. For example 24V coil and 5V MCU. However, when they are the same why would some designs not include an optocoupler and some do?

author: pqass
author_rank: Super Contributor
timestamp: May 23, 2021, 04:42:52 am
content: "...with 5V MCU and 5V coil I noticed a lot designs still include the optocoupler." Really? I've not had that experience.As you said, there is isolation already so the extra optocoupler before the relay isn't really needed. Unless they don't share the same ground. Besides, you'll still need a NPN low-side transistor to switch the relay since MCUs or optocouplers won't be able to source/sink 80mA or so of current that a typical 5V coil demands."...switching transistors failed it could also blow up your controller."Not really likely for just 80mA of current unless someone forgot the flyback diode."For example 24V coil and 5V MCU."A low-side NPN transistor would have no problem with the 24V coil Vcc interoperating with a 5V MCU Vcc. Using a high-side PNP on its own would be a problem though."...why would some designs not include an optocoupler..."Madman Muntz got it!https://en.wikipedia.org/wiki/Muntzing

author: anfilt
author_rank: Newbie
timestamp: May 23, 2021, 05:42:25 am
content: ""...with 5V MCU and 5V coil I noticed a lot designs still include the optocoupler." Really? I've not had that experience." Well I have found a fair few of them that's what I mean by a lot. So I just trying to understand why.

author: MasterT
author_rank: Frequent Contributor
timestamp: May 23, 2021, 02:17:17 pm
content: Search pattern "resetting microcontroller when relay switched off"google's first findingshttps://electronics.stackexchange.com/questions/101815/pic16f628a-resetting-after-relay-turned-off

author: penfold
author_rank: Frequent Contributor
timestamp: May 23, 2021, 03:24:11 pm
content: I can't say I've ever seen it done in a commercial design. So long as the creepage and clearance between the coil and contacts are maintained on the PCB as it is within the device, a suitably rated drive transistor and flyback diode or snubber arrangement is used on the coil itself... then I see very little reason toneeddo it.In some extreme EMC cases, maybe 12pF or so coil to contact capacitance may be too much, or perhaps the relay could be physically in a separate I/O domain of the PCB which would warrant some isolation, but I would expect a separate power supply. Whether an opto is the right device to offer such isolation is a bit academic, but it is a case for isolation nonetheless.

author: fcb
author_rank: Super Contributor
timestamp: May 23, 2021, 04:00:13 pm
content: Quote from: anfilt on May 23, 2021, 01:56:38 amBefore I say something like 'what a load of tosh', any chance you can post links or pictures of the schematics?? There maybe a different reason for using an opto-coupler than you think.The only reason you would include an optocoupler in a relay driver is if you need isolation between voltage domains for some reason (different earthing, floating supplies, isolation for noise (see earthing) etc). You certainly wouldn't use it as method of isolating back-EMF spikes.If you are using opto-couplers as method of getting enhanced creepage/clearance, then perhaps - but relays tend to have greater creepage/clearance than most common optocouplers.You might use an opto-coupled triac (MOC3020 etc..) to drive a 24VAC or mains coil type relay. An opto-coupled solid-state switch as coil drive amplifier.

author: langwadt
author_rank: Super Contributor
timestamp: May 23, 2021, 05:54:52 pm
content: seems like the hardware version ofhttps://en.wikipedia.org/wiki/Cargo_cult_programming

author: james_s
author_rank: Super Contributor
timestamp: May 23, 2021, 06:31:29 pm
content: Quote from: penfold on May 23, 2021, 03:24:11 pmI did a commercial design that had a 5V microcontroller switching a relay with a 12V coil using a low side NPN transistor and a flyback diode across the coil, the relay was switching 240VAC at up to a few amps. We have not had any failures either on the bench or in the field.I suppose optocouplers could be handy for something like a relay board meant for beginners, it does provide an extra degree of protection against accidental shorts, mis-wiring, and stuff like that but I see no reason to use an optocoupler in most cases.

author: penfold
author_rank: Frequent Contributor
timestamp: May 23, 2021, 07:09:42 pm
content: Quote from: james_s on May 23, 2021, 06:31:29 pmHence the "So long as ..."... though on second thoughts I retract that, it was a slightly foolish comment since I doubt there's any safety standard that would allow the opto to be the limiting factor rather than the relay's isolation itself.

author: Zero999
author_rank: Super Contributor
timestamp: May 23, 2021, 08:31:18 pm
content: I did a quick search and using an MCU to drive an opto-coupler, driving a relay seems to be quite common. Schematics I've found on hobbyist sites and photographs of commercially available modules show opto-couplers driving relays. I suspect it's a case of copy paste. There's no need for an opto-coupler, as the 0V for the relay coil, is normally shared with the MCU's power supply. If it isn't, then I can see the need for an opto-coupler, but would prefer to drive the relay from the same power supply, as the MCU, if at all possible and not use an opto-isolator.https://www.instructables.com/How-Does-It-Work-DIY-Relay-Modules/https://www.amazon.co.uk/SUNFOUNDER-2-Channel-Optocoupler-Expansion-Raspberry/dp/B00E0NTPP4https://electronics.stackexchange.com/questions/464343/driving-relay-directly-from-optocoupler-what-is-besthttps://circuits-diy.com/optocoupler-relay-driver-with-pc817-2n3904/

author: james_s
author_rank: Super Contributor
timestamp: May 23, 2021, 08:57:09 pm
content: One application where I could potentially see a need for the optocoupler is in a medical device, where there tend to be greater requirements for isolation and a little redundancy is a welcome addition for the small cost it adds to relatively expensive low volume equipment. Generally speaking though I agree that it's probably largely a case of somebody saw a relay driver that used it somewhere so they used the same approach.

author: fcb
author_rank: Super Contributor
timestamp: May 23, 2021, 09:01:45 pm
content: Thanks Zero999 for those links. Some of these 'engineers' need taking outside and re-educating with the hot-end of soldering iron, cargo-cult indeed as someone has said.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: May 24, 2021, 02:56:39 am
content: My father had a greenhouse (about 6000 square meters of glass) and the first climate computer was an 8080 based beast the size of a desk with a real terminal on top and it looked quite impressive when I was 8 years old.Later I found out there were rows upon rows of reed relays in the thing that switched some 200+ wires from a separate 24V source and at the end of all those wires (about 40meter long) there were 12V signal relays which again switched big contractors for motors up to 5.5kW.That thing worked quite reliably for about 20+ years.Part of the reliability was probably from using several layers of isolation. The reed relays are also only capable of switching small currents. Not enough to switch the big 3-phase contactors. Yet another reason for the extra layers is that during maintenance a mistake is made (for example replacing a faulty 5kW contactor), it would only fire back so far and not reach the climate control computer itself.

author: David Hess
author_rank: Super Contributor
timestamp: May 24, 2021, 08:45:12 am
content: An optocoupler may be used to drive a relay so that the return current from the relay is not shared with the digital logic's return currents and the relay's return current can be more easily directed to the best grounding point. Essentially it makes it easier to maintain a single point ground, although it applies just as well to the positive supply if that is shared with CMOS digital logic where the threshold voltage relies on both the ground and supply potential.The optocoupler also provides more electrostatic isolation (less capacitance) between the logic and fast edges of the inductive solenoid coil than a transistor will.You might also see an optocoupler used to provide protection against ESD, although this applies even more so to human interface circuits.

author: Zero999
author_rank: Super Contributor
timestamp: May 24, 2021, 07:12:12 pm
content: I still don't see the point in using both a relay and opto-coupler.It won't provide any greater degree of isolation from the relay contacts, unless the coil is driven off a separate power supply, with a greater level of separation, than the MCU's power supply.I don't see how the return current from the relay coil is an issue. It's normally only a few tens of mA and even if the layout isn't that great, the emitter of the driver transistor will just bounce up a little, which is no big deal, unless the trace is stupidly thin. If there are concerns about return currents causing interference, I'd be more inclined to shift the relay to the negative rail and drive it with a PNP transistor, with an NPN driving the base, rather than mess around with an opto-coupler. Note the load in the circuit is the relay coil.

author: David Hess
author_rank: Super Contributor
timestamp: May 24, 2021, 08:05:29 pm
content: Moving the return current to the positive supply does not help with CMOS logic.And if the return current is not a problem, which is likely with a good layout, coupling from the high dV/dt though the transistors is still there. And good layouts are not always possible; the base current has to return through the emitter somehow, and using an optocoupler allows its point of return to be explicitly determined by breaking any possible ground loop.

author: Zero999
author_rank: Super Contributor
timestamp: May 24, 2021, 09:15:18 pm
content: Quote from: David Hess on May 24, 2021, 08:05:29 pmWell if two transistors are used, as I've shown, the base current will be much smaller, than with the one, or indeed an opto-coupler, which has a poor CTR. It's a better solution than an opto-coupler an a much cheaper.

author: james_s
author_rank: Super Contributor
timestamp: May 25, 2021, 07:38:26 pm
content: Digikey has a bunch of different optocouplers in stock for 23 cents each in qty 10, I'm not sure two transistors are going to be "much cheaper" than that. Even if it isn't necessary, I could see it being worthwhile if it makes routing the PCB a little easier.

author: Zero999
author_rank: Super Contributor
timestamp: May 25, 2021, 09:28:23 pm
content: Quote from: james_s on May 25, 2021, 07:38:26 pmHave you seen how cheap BJTs are?Did you look at the links I posted? Most of them have both a BJT and an opto-coupler. People here are giving them far too much credit.https://images-na.ssl-images-amazon.com/images/I/71oYyAoyO3L._AC_SL1500_.jpghttps://circuits-diy.com/wp-content/uploads/2020/07/Optocoupler-Relay-Driver-with-PC817-2N3904.pngAn opto-coupler has a much poorer CTR, than the hFEof a BJT, so goodness knows why anyone would use one, when unnecessary.

author: james_s
author_rank: Super Contributor
timestamp: May 25, 2021, 10:03:33 pm
content: Like I said, maybe it makes the PCB layout easier? Maybe it makes them sleep better at night? Maybe some very expensive or safety critical equipment is controlling the relay and you want to avoid any chance of damage due to transistor failing shorted, or a faulty connection resulting in a spike from the relay coil traveling back into the control circuitry? I've never used one to drive a relay but I can believe there might be a valid reason for doing so. There are also probably a lot of people who saw it done somewhere so they do it too, without really understanding why/if it's necessary. When it's a 23 cent part and we aren't talking large volume production who cares?

author: Zero999
author_rank: Super Contributor
timestamp: May 26, 2021, 10:56:33 am
content: Quote from: james_s on May 25, 2021, 10:03:33 pmThe part about being safety critical made me laugh, considering the cheap Chinese modules often lack other important safety features, such as sufficient creepage between the contact and coil. I suspect the part of your reply I've changed to bold, is the most likely reason. There are plenty of designs on the Internet with pointless/inappropriate parts, due to copy paster.

author: Jwillis
author_rank: Super Contributor
timestamp: May 26, 2021, 12:05:59 pm
content: A discharging coil can have voltages many times higher than the energizing voltage . But the diode should be enough to resolve that .Galvanic isolation also addresses any ground potential differences or ground looping which can cause errors to occur in an MCU .Transients can also create line noise which could also have an effect .It all very much depends on what components are being used and circuit design.

author: Nominal Animal
author_rank: Super Contributor
timestamp: May 26, 2021, 12:37:22 pm
content: If the relay is very far from the microcontroller, then using an optocoupler close to the microcontroller, and a separate power circuit for the relays (completely isolated from the microcontroller), might make sense in an electrically noisy environment. That way, any currents induced in the long wires would have to be pretty big to affect the operation, since they'd only affect the relays.For example, consider a DIY CNC or plasma table, with the relay controlling a spindle motor or coolant pump.I personally really dislike designs where the logic and the motors share a ground. It is extremely common, but causes all sorts of issues; not the least of which are ground loops if you connect a computer in a separate mains circuit to the logic. The optimum location for the isolation is between the microcontroller and the stepper drivers, and costs very little (see e.g. SI86xx and ISO76xx digital isolators), even including Trinamic drivers with UART/SPI comms and not just the typical STEP and DIR pulse lines.

author: james_s
author_rank: Super Contributor
timestamp: May 26, 2021, 09:57:04 pm
content: Quote from: Zero999 on May 26, 2021, 10:56:33 amI'm not talking about cheap Chinese relay modules specifically, I'm speaking in general when I say there may be legitimate reasons to include an opto isolator driving a relay, not that the cheap Chinese modules may be legitimate reasons. Those cheap modules are a good candidate for the designer having seen it used somewhere and gone with that approach.

author: james_s
author_rank: Super Contributor
timestamp: May 26, 2021, 09:58:54 pm
content: Quote from: Jwillis on May 26, 2021, 12:05:59 pmShould, yes. But what if the diode fails? Without isolation a failed diode could result in damage or unpredictable behavior to other parts of the system, with isolation that situation is very unlikely.

author: fcb
author_rank: Super Contributor
timestamp: May 26, 2021, 10:38:19 pm
content: Quote from: james_s on May 26, 2021, 09:58:54 pmShow just one circuit (with a common ground) whose fault condition is protected by an optocoupler. And with an equivalent number of components (or less) I'm pretty sure we (as in the forum) can come-up with a non-optocoupler circuit that it is smaller, cheaper and offers the same protection from your fault or faults.

author: james_s
author_rank: Super Contributor
timestamp: May 27, 2021, 02:23:11 am
content: Quote from: fcb on May 26, 2021, 10:38:19 pmMissing flyback diode on the relay, NPN transistor switching the low side, could you not get a spike back through the transistor out through the base and into whatever delicate CMOS IC is driving it? Cracked solder joint on the emitter that is causing repeated spikes that are not shunted by the B-E diode? If there's an optocoupler then short of a spike of sufficient magnitude to flash across the IC this cannot happen. Also who says there has to be a common ground? In the case of a relay board for hobby/prototyping you never really know what somebody will connect it to. If it has optical isolation you don't need a common ground, you could have an entirely separate power supply over with the relays and the control circuit is only driving LEDs. The LEDs could even be multiplexed and drive a load of relays from a handful of IO pins. I know I'm grasping at straws here but there have got to be applications where it makes sense. Maybe not in some specific board where we've seen it done, but the basic concept of optical isolation being used in a relay coil driver.

author: JeanF
author_rank: Regular Contributor
timestamp: May 27, 2021, 06:39:20 am
content: Thank you all for your insights and thanks to anfilt for asking. I actually had the same question when I encountered cheap Chinese relay boards and I also suspected a case of copy-pasting, but it's interesting to read the other design/safety considerations here