Arduino and optocouplers : basic question - Page 1
Forum section: electronics / projects

author: mushroom
author_rank: Regular Contributor
timestamp: November 11, 2017, 03:15:36 pm
content: Hello,I want to drive relays from an Arduino (Pro Mini). I try to reduce the number of components. I tested two working designs. The first one is very usual. Not a big deal.The second one is the subject of the topic. In order to activate the relay, and not to draw too much current from the Arduino, I found that R1=680R~820R is a good compromise (~7 mA in the IR diode). But, doing so, the phototransistor is *not * fully saturated : VCE measured at about 0.3V insteed of 0.1V.Optocouplers : PC817 (http://www.farnell.com/datasheets/73758.pdf)Relays : 24V, 7mAI don't know if driving a relay directly from a not fully saturated phototransistor is a great idea. I feel like it is'nt, and there is probably no room for component dispersions. But I can select best working optocouplers (50 in the drawer). Any advice is welcome.

author: TJ232
author_rank: Frequent Contributor
timestamp: November 11, 2017, 03:46:46 pm
content: I think you have at least 2 solutions:1. Add extra tranzistor after the optocoupler as it is in a lot of designs around.2. Use a darlington optocoupler like 4N32, 4N33 for example.I would probably prefer (1) if space is not a problem.

author: Ian.M
author_rank: Super Contributor
timestamp: November 11, 2017, 04:08:49 pm
content: Its not a great idea to drive low-current relays direct from an optocoupler, but it is potentially workable. LED output falls with age (actually operating hours) so you risk the phototransistor coming totally out of saturation, which could drop the voltage across the relay coil to below its pull-in voltage. You are already dropping a couple of volts with that LED in series. Its prudent to allow for up to 50% drop in CTRR over the lifetime of the optocoupler. If you use high gain optocouplers with a CTRR of 220% or higher, it will work reliably, *IF* your 7mA relay current is correct. Select on test is possible if you set up a jig to measure CTRR, but don't count on finding high gan optos in your existing stock as that part is usually binned and sold by CTRR.However unless the grounds of the relay drive supply and the Arduino are isolated, using optocouplers is pointless and you'd be better off with aULN28038 channel (or ULN2003 7 channel) Darlington driver, which can handle 8 (or 7) relay coils with no other external components and will draw under 1.5mA from the Arduino output pin. It even includes the antiparallel diodes for the relay coil back EMF suppression.

author: jaycee
author_rank: Regular Contributor
timestamp: November 11, 2017, 06:14:34 pm
content: Driving a relay directly from an optocoupler doesnt seem like a good idea to me. Most relays normally take more than 7mA. Better to use the external transistor.On the other hand, why do you need an optocoupler ? Safety ?

author: mushroom
author_rank: Regular Contributor
timestamp: November 11, 2017, 06:53:24 pm
content: Thanks for your quick answers.Yes, usually, relays take more than 7 mA. But these relays are not usual relays. They are industrial grade command relays. And they do take 7 mA. Measurements and datasheet : Weidmuller RSS113024. They stick at 16.8V and can handle 6A (I will give them a few hundreds of mA).I'm happy to read that what I think to be a bad idea *is* a bad idea !So, I will stick to the usual schematics, and unchain some more angry pixies to the opto coupler with pessimistic specs:If = 7.5 mA -> R1 = 560R (spec : 600 max @ 2mA)CTRR = 100 insteed of 600, BC547B hFE = 100 insteed of 200 -> R2 = 220k or 330k.(tested good)I just wanted to reduce the number of components : less components = less holes (DIY etching).The project is a small PLC, driving a variable frequency drive and using Modbus/RS485. Usually, PLCs and VFDs have opto-isolated inputs/outputs, even if grounds are tied together at some point. See Open PLC, and the 0R resistor between grounds. VFDs also often have opto isolated I/Os. Floating potentials are not a good thing in machine tools environment (would'nt like to see my 8HP lathe trip for any reason).Before I opened this thread, everything was working fine on breadboards with the usual circuit (hard and soft). I wanted to know if it was possible to cut corners because it has to fit a small DIN rail enclosure, and because I hate drilling PCBs. I'm lazy but I will drill some more holes...

author: MasterT
author_rank: Frequent Contributor
timestamp: November 11, 2017, 07:10:50 pm
content: The specification for PC817 says Collector dark current = 100 nA, that may cause problem with false triggering. You may need to add a resistor to base-emitter of the BC547

author: rsfoto
author_rank: Contributor
timestamp: November 11, 2017, 10:41:11 pm
content: Quote from: jaycee on November 11, 2017, 06:14:34 pmI guess he uses an optocoupler in order to avoid a ground loop problem when he is using 2 different power sources.

author: Ian.M
author_rank: Super Contributor
timestamp: November 11, 2017, 11:10:19 pm
content: What's the Arduino powed from? If its via a regulator from the 24V rail, with a commoin ground, its pointless to use optos, just use a ULN2803 to drive up to eight relays.If however its got a fully isolated supply, with no common ground, for an eight channel through hole design with the absolute minimum of holes, stick an opto in front of the ULN2803 for each channel. The phototransistor collectors should go to 9V derived from your 24V rail - use a 78L09 regulator (see datasheet for caps required). The emitters go via indicator LEDs to the ULN2803 inputs. That gives you about 2mA LED current, plenty for high efficiency LEDs. On the input side of the optos, use a 9 pin SIL resistor array (8x1K) between the opto LEDs and the common (either Arduino +5V or GND depending on whether you want them active low or high). It will take around 3.5mA to drive it from each Arduino output pin, so plenty of safety margin with 100% CTRR optos. If you want to reduce the load on the Arduino outputs, get higher CTRR optos and increase the SIL resistor value.Of course you *could* use discrete resistors but the SIL array saves you seven holes. The ULN2803 saves you a *LOT* of holes because it contains Darlington transistors with base resistors and back-EMF protection diodes all in one package. For further hole reduction go SMD!

author: Zero999
author_rank: Super Contributor
timestamp: November 11, 2017, 11:45:25 pm
content: This isn't the first time this question has been asked here. I agree with the above: get rid of the opto-couplers and use a Darlington driver IC such as the ULN2803.Opto-couplers are useful when the load needs to be isolated from the controller, but the relays already provide the isolation here so it's completely unnecessary.What are the relays going to be switching? If it's mains, make sure the relays are appropriately rated and there's enough creepage between the contacts and the coil, on the PCB. If it's DC, then bear in mind that the DC rating of a relay is usually lower, than the AC rating, because it's easier to break the arc, when the current goes through zero many times per second.

author: brainwash
author_rank: Frequent Contributor
timestamp: November 11, 2017, 11:56:13 pm
content: Operating in "undocumented" CTR mode is certainly possible and I've done it in non-commercial products. Basically, when not saturated, the optocoupler acts as a voltage-to-current converter. I haven't seen any manufacturer that recommends this mode and barely any data on CTR variance.To sum it up, if you use the same relays and same optocouplers, you could drive the outputs with just 1.5mA on the optocoupler input (=600% CTR = 9mA).However, a Darlington array is much easier to use, as others above have posted.

author: Zero999
author_rank: Super Contributor
timestamp: November 12, 2017, 12:25:26 am
content: Quote from: brainwash on November 11, 2017, 11:56:13 pmAre you sure? I thought it would be more like an isolated current to current converter, since the phototransistor's current is proportional to the photodiode's forward current, rather than the voltage.

author: brainwash
author_rank: Frequent Contributor
timestamp: November 12, 2017, 12:40:16 am
content: My mistake, it's current-to-current but I've always used a resistor, hence the expression. With a fixed input resistor, the output current is proportional to the input voltage.

author: mushroom
author_rank: Regular Contributor
timestamp: November 12, 2017, 03:05:14 am
content: The schematics are similar (but not identical) to these (OpenPLC) :https://drive.google.com/file/d/0BwyThwktWLAlWV9mRzBiNXphYjQ/editYes, GND_24V and GND_5V are equipotential. See "power source" in the bottom right corner. I will *never* create floating potentials in such an equipment (machine tools controls). Pointless or not, VFDs and PLCs are generally optocoupled. And all grounds and 0V have to be earthed (and equipotential).Don't be scared ! The relays are obviously used as relays, not contactors. They drive AC3 contactors. This way : PLC -> relays -> contactors -> async motors. It would stupid to drive motors with relays. These relays are made for this job : I took them from DIN rail relays I can't use because of their Wago connectors ; I use only crimp terminals and screw terminals. Have a look at the attached pic, and at its schematics : the LED is also in series with the coil. I just copy the original design, and I reuse the relays and LEDs on a PCB... Only 7 mA !2 mA is fine for these optos. I need only 4 inputs and 5 outputs. I already have all the discrete components I need. Everything works fine on the bench, I will start PCBs routing and maybe etch tomorrow.

author: Siwastaja
author_rank: Super Contributor
timestamp: November 12, 2017, 07:12:51 am
content: Quote from: brainwash on November 11, 2017, 11:56:13 pmThere are optocouplers designed to do exactly that, used, for example, in isolated feedback loops in power supplies.The "switching ones" indeed do not have the necessary data and consistency to do that.

author: Ian.M
author_rank: Super Contributor
timestamp: November 12, 2017, 07:22:17 am
content: My concern about ground isolation is the risk of damage to the Arduino and PC USB ports due to a high current ground loop. However its preferable to deal with that at the ATmega serial interface if you are basing your design on compatibility with a classic ATmega based Arduino, or use a USB isolator if you are using an actual Arduino or an USB ATmega.The only thing an opto with its input and output grounds connected together can protect your MCU against is over-voltage coming back from its input due to a high energy output blowup. If that's a concern, you might as well use ULN2001 (or ULN2801) which doesn't have internal series base resistors (but does have base pulldowns), with external series base resistors, and if your peak load voltage divided by your base resistor value exceeds the MCU's abs. max. clamping current, add low voltage unidirectional TVS diodes to clamp its inputs as well. However high energy output blowups are usually caused by pure user idiocy*(excluding ligntning, EMP and acts of God like a HV transmission line dropping across a lower voltage line), so once you get down to a design for a PCB specific project, you should be able to design them out.However in your design, for five relay outputs, I'd simply use a ULN2003. You cant get simpler and it has proven reliability in millions of industrial, HVAC and white goods control boards. Using optos for the four inputs is *PROBABLY* worth it if its going to be used in a high EMI environment, but do it right - antiparallel diode and a 12K resistor across the opto LED, fusible through hole series resistor, and a separate pair of in+ and in- terminals for each channel. The 12K resistor ensures the LED doesn't turn on below 100uA. The fully isolated input terminals give you great flexibility and no ground loops - just use twisted pair back to your signal source and decide there which two points to wire the input between to get the desired action.* which probably explains the popularity of crappy ineffectively optoisolated output interfaces in ArduinoLand

author: mushroom
author_rank: Regular Contributor
timestamp: November 12, 2017, 03:51:31 pm
content: I just took apart a low end PLC.It is based on an ARM, and there is no optoisolation (nowhere). Digital inputs are juste buffered with 14050BG ICs, and analog inputs use 10358 ADCs. Output relays are driven with a ULN2003A, as you suggested. All inputs are refered to 24V ground. The 5V is generated by a 7805 in series with a 2 watts resistor (on my side I use a step down converter).I also retrieved pics from a chinese crappy VFD I took apart a few years ago : all digital inputs isolated, and it *seems* to me that analog input is a R2R ladder, and is also optoisolated. Pics are not great, so I can't say. And I can no more take it apart, it is in service.I can use belt and suspenders at no cost (all the components are in the drawers...). It is easy to make a mistake while building electrical cabinets, or while connectting them to the motors and buttons. Until now, I never fried anything, but I generally use bullet proof high end industrial devices, not DIY ones !I am not confident with just a transistor or buffer between a µC and a 24V coil relay. I prefer not be mixing 24V and 5V on the positive side. And OpenPLC designers seem not to be confident themselves, as they isolated output relays. I could use 5VDC relays, but I have a large stock of 24VDC and only a few 5VDC.Inputs I made are slighly different from OpenPLC: the LED is in series with the opto LED, and I use no Schmitt inverting buffer but software debouncing. The best library I found is DebounceInput, and it works perfectly for my use. Yes indeed, inputs will not be grounded/earthed at PLC level, but externally. (they are on the breadboard, but it is only for coding and testing).There will be no link with USB, except for software updates and obviously with no motor running. USB cannot be used on a ProMini, as RX/TX are shared with the MAX485. Jumper is needed to allow communication OR software update.All inputs and outputs will be fused, but not on the PCB (the empty DIN module is only 150mm wide). I will use 6mm DIN rail fuse holders I already have.I don't buy anything if I can do the same with my component stock. I buyed 10th of kg of components, electrical and eletronics, in the good old times , when eBay sellers did not offer "buy it now" as only option. I made amazing bargains. Since then, I use these components. And therefore, I probably will not be playing with SMD and double layer PCBs until many years !I will post some more schematics later.

author: Zero999
author_rank: Super Contributor
timestamp: November 12, 2017, 04:19:17 pm
content: Quote from: mushroom on November 12, 2017, 03:51:31 pmThat's pretty common and perfectly safe. Many control systems are designed like that, with no problems. The 5V power supply is derived from the 24V supply on the same board and the relay contacts are already isolated. Adding an opto-coupler between the relays and the MCU would add nothing but cost and complexity to the design, with absolutely no benefit.The only place where isolation would give added protection/flexibility to the design, is on the input side, which would be trivial for the digital inputs, but a little more tricky for the analogue inputs.QuoteYes, that's fairly standard practise. A common method of isolating an analogue input is after the ADC, as it's much easier to isolate a digital, than it is an analogue signal.QuoteSo you think using opto-isolators on the relay coils would improve reliability? It won't. All it will do is add more parts which can fail. Generally minimising the number of parts, improves the reliability of a system.If you lack confidence, then how about building a prototype first? Then you can test it, to give you more confidence in the design.QuoteYes isolating the inputs is a good idea. The main reason is, the inputs can be connected to another circuit else where, which may float at a different voltage to your circuit.Isolating the outputs is also a good idea, which is why relays are used. Isolating the relay coils, from the MCU is not necessary, because they're on the same board and connected to the same power supply, as the microcontroller.

author: mushroom
author_rank: Regular Contributor
timestamp: November 13, 2017, 04:11:15 pm
content: Ok, I'm definitely convinced. I will not use optocouplers on the output side... There is maybe a reason why OpenPLC designers do use optos between MCU and relays : they are not on the same board. Could it be the reason ?I was hoping routing yesterday, but I have a very hard time with the mechanical design of the enclosure. The enclosure documentation I have is incomplete and many dimensions are missing or false, so I had to play with calipers.I now return to Eagle, and can still apply suggestions (except the ULN2003A)

author: Ian.M
author_rank: Super Contributor
timestamp: November 13, 2017, 04:53:39 pm
content: Quote from: mushroom on November 13, 2017, 04:11:15 pmYes. If the relays are off-board it vastly increases the risk of some dumb muppet (and the user end of the Arduino community has plenty of them) connecting a power rail directly to the output transistor, and if that power rail is well above 5V it will probably take out the MCU when the transistor blows the first time its set On..Why cant you use a ULN2003 (or ULN2803 if that's all you've got)? A single 16 pin DIP will be a lot simpler to route than 4x TO-92 transistors, diodes and base resistors. It's about the same for compactness (assuming through-hole parts), but you'll save board area round it for the interconnects.If your contactors have mains operated coils, be careful of creepage and clearance distances between your relay contact terminal tracks and any part of the driver circuit, including their coil terminals - you may need to slot the PCB to get enough creepage distance to meet commonly accepted safety standards. Calculator athttp://www.creepage.com/. Explanation of the requirements and terminology athttp://www.minntronix.com/download_file/view/159/401N.B use the *MAXIMUM* permissible mains voltage for the calculator, which I belive is 255V RMS, 360V Peak

author: mushroom
author_rank: Regular Contributor
timestamp: November 13, 2017, 05:24:59 pm
content: I don't want to use ULN2003A because I have plenty of BC547B and IN4008, and because it's easier to space TO92 pins than DIL pins ! The main PCB has to be 153x85mm, and I want to avoid small traces when possible : laser transfer.My contactors are all 24VDC, most of them with integrated TVSs.

author: Zero999
author_rank: Super Contributor
timestamp: November 13, 2017, 07:02:12 pm
content: Quote from: mushroom on November 13, 2017, 05:24:59 pmI've done PCBs with much smaller traces than DIL, using laser printer, toner transfer (down to SOIC8) but yes, I can understand you want to make this as big as possible, as your PCB needs to be a certain size, for mechanical reasons.Another alternative to BJTs is MOSFETs such as 2N7000.You can also get BJTs with built-in base resistors, such as the FJN3303.http://docs-europe.electrocomponents.com/webdocs/12cf/0900766b812cf8a9.pdf

author: mushroom
author_rank: Regular Contributor
timestamp: November 14, 2017, 01:30:28 am
content: Please, don't laugh at me. I just ordered 5x ULN2003A. I was pulling my hair on routing (it would have been easier with BEC transistors).Only crazy people never change their mind.

author: Ian.M
author_rank: Super Contributor
timestamp: November 14, 2017, 01:54:14 am
content: I'm sure you'll find the spares useful as well.One thing you can do with the remaining ULN2003 channels is configure them to drive a pizeo disk speaker. Use one with a pullup to +5V as an inverter to drive another with a pullup to +24V. Put that output on one side of the pizeo. Use the remaining channel with a pullup to +24V for the other size of the pizeo. Drive its input and the input of the inverter channel in parallel (e.g. using Arduino tone() ) for horrifyingly loud warning beeps etc.

author: mushroom
author_rank: Regular Contributor
timestamp: November 19, 2017, 06:39:52 pm
content: Great idea. But unfortnuately, I don't really figure the schematics. There is steel (still ? steal ? sorry...) some room on my pcb design, why not ? But I doubt that a buzzer in a DIN casing, in an IP66 400x600x200mm metal enclosure, with a loudy 24V fan + the loudy VFD fan could be audible. never the less I'd like to give it a try. I had a look at the buzzers I have, and most of them are 3 wire. I planed to use a 22mm industrial buzzer (Schneider) I have : they are barely audible while turning or milling...Didn't etch at this time, because I have some problems with the screw terminals. I have *a lot* of these things, but they don't fit. I'll probably open a thread about this.Still some room for improvements.