Driving a relay AND LED from microcontroller pin - Page 1
Forum section: electronics / general

author: Bryan
author_rank: Frequent Contributor
timestamp: September 26, 2020, 08:15:53 am
content: All:Is there a best practise if one wishes to drive a relay from a microcontroller pin (Arduino) as well as a indicator LED. Would use a transistor to switch the relay and a diode across the relay coil to protect the transistor, but what if I wanted to incorporate a LED to indicate when the relay is switched on and off.?. I suppose I could run it in parallel across the load but is that a good idea, or perhaps a separate transistor from the same output pin to switch the LED?Cheers

author: sleemanj
author_rank: Super Contributor
timestamp: September 26, 2020, 08:19:46 am
content: I'd just put LED and series resistor in parallel with relay (and reverse diode). Make resistor like 1k+, you don't need much for just an indicator.

author: Bryan
author_rank: Frequent Contributor
timestamp: September 26, 2020, 08:24:03 am
content: Well, that was quick. <lol>. As for the resistor value I suppose I should probably take the relay coil resistance into consideration and use a resistor value to supply the required 15ma to the led using a parallel resistance calculation?

author: sleemanj
author_rank: Super Contributor
timestamp: September 26, 2020, 08:29:05 am
content: No, the coil is in parallel with the *combination* of the led and it's resistor, it does not affect current through the resistor+led.Code:IOPIN -> Resistor -> Led -> Gnd\-> Coil -> GndYou do of course need to consider the total current of resistor+led plus the coil curent is within the limits of your IO pin.

author: Bryan
author_rank: Frequent Contributor
timestamp: September 26, 2020, 08:40:13 am
content: Ok, you mean like this?. Relay is 12v

author: sleemanj
author_rank: Super Contributor
timestamp: September 26, 2020, 08:42:57 am
content: Your led is backwards, but yes that should work otherwise.

author: Bryan
author_rank: Frequent Contributor
timestamp: September 26, 2020, 08:48:24 am
content: Oops, right on that. Thanks for the help. The current for the led should then be (12 - .7)/1000 ~ 12ma. Maybe a 680ohm for a little more brightness 17ma

author: ledtester
author_rank: Super Contributor
timestamp: September 26, 2020, 09:07:35 am
content: You might want to check out this thread for sizing your base resistor:https://www.eevblog.com/forum/beginners/how-to-calculate-base-resistor-for-transistor/At 4.7K you are only allowing 1 mA of base current if you are driving it at 5V. This will generally allow around 10mA of collector current to flow. How much current does your relay require?Also, for your LED see if 5 mA (or even 2 mA) works well enough for you. Modern LEDs are very efficient and don't require a lot of current to light up.

author: Bryan
author_rank: Frequent Contributor
timestamp: September 26, 2020, 09:12:52 am
content: ThanksQuote from: ledtester on September 26, 2020, 09:07:35 amThanks will have a read, the relay is spec'd for about 70ma, so will adjust accordingly.

author: sleemanj
author_rank: Super Contributor
timestamp: September 26, 2020, 09:19:56 am
content: The current for the led is(12 - Vfwd - Vce) / RWhere Vfwd is the forward voltage of the led (eg, 3v-ish for blue, 2v-ish for red), Vce is the collector-emitter voltage at your on-state, and R is the led resistor. For an indicator led, it's really not needed to be high, a few mA with modern leds is heaps.

author: Lindley
author_rank: Regular Contributor
timestamp: September 26, 2020, 09:43:31 am
content: Hi,You do not mention what load the relay is switching, but relays can be prone to causing interfenece to the Arduino when the contacts open and close unless you use good suppression.Generally use a SSR modules instead for AC loads under 2A often cheaper than a relay and these little boards have an inbuild On led.For heavier AC current you can buy the single block devices.If switching DC current then a Logic Level Mosfet is the one to use.

author: Bryan
author_rank: Frequent Contributor
timestamp: September 26, 2020, 09:49:54 am
content: Quote from: sleemanj on September 26, 2020, 09:19:56 amUgh, yes I had the forward voltage wrong, had diode on my mind.

author: Bryan
author_rank: Frequent Contributor
timestamp: September 26, 2020, 09:51:38 am
content: Quote from: Lindley on September 26, 2020, 09:43:31 amActually it is not a relay but a pneumatic valve. I suspect they use some type of coil winding like a relay, but good point, will have to monitor to see what interference it creates.

author: Siwastaja
author_rank: Super Contributor
timestamp: September 26, 2020, 10:17:54 am
content: The key to avoid interference problems is that the diode is not placed at the relay, it's placed in a physically tight loop right next to the transistor. You also place a power bypass capacitor (between Vcc and GND) right at that spot. Imagine the loop current flowing from capacitor +, through diode, through transistor, to capacitor -, and minimize that area. For the capacitor, a combination of say 100nF to 1uF small ceramic and, say, 47uF electrolytic is a good all-arounder.Note that the diode slows down the relay/valve turn-off/opening time. Often this isn't a problem, but if it is, you would need to use a zener with higher drop than 0.7V of the diode to clamp the voltage peak to a level transistor is OK with.

author: Kalvin
author_rank: Super Contributor
timestamp: September 26, 2020, 11:10:11 am
content: Quote from: Siwastaja on September 26, 2020, 10:17:54 amI have always placed the freewheeling diode right next to the relay (where the problem exists).After some googling, there seems to be two opinions to this: Some people suggest to put the freewheeling diode to the near end (ie. close to the transistor), and the others suggest to put the diode at the far end (ie. right next to the relay).It was suggested that placing the diode at the far end (ie. right next to the relay) would reduce any EMI problems as the coupling to the other nearby cables will be reduced and the radiating loop will be smaller as the current loop due to relay coil is smaller.However, placing the freewheeling diode right next to the transistor may provide some protection from the possible inductive spike due to the inductance of the long wiring, but this may introduce nasty EMI problems in the system although it will protect the transistor: The inductive coupling between the other wiring in the system may cause intermittent system problems when the relays are turned off.So, I would place the freewheeling diode right next to to the relay pins, and use a twisted pair or some other decent cable for driving the relay.Just make sure that the diode is a fast recovery one, and it can dissipate the energy stored in the relay coil (so, you would not probably want to use 1N4148 for this application because it will burn due to the excessive energy stored in the relay coil, and the 1N400x-series diodes may be too slow to react to the current spike unless you are limiting the transistor's turn-off time).

author: Bryan
author_rank: Frequent Contributor
timestamp: September 26, 2020, 11:20:57 am
content: Quote from: Kalvin on September 26, 2020, 11:10:11 amThanks for information, as for the 1N400x, the circuit switching OFF times would vary from .1s to 10s

author: Kalvin
author_rank: Super Contributor
timestamp: September 26, 2020, 11:42:13 am
content: Quote from: Bryan on September 26, 2020, 11:20:57 amI meant that how fast the transistor switches from the on-state to the off-state, ie. how fast the signal driving the transistor base / mosfet gate is changing from "1" to "0". Faster the transition, the faster diode you will need. If you slow the driving signal transition from "1" to "0", the transistor will dissipate somewhat more energy during that transition time. Typically that should not be a problem it the transition times are kept reasonable slow like tens or hundred of microseconds or so.

author: Siwastaja
author_rank: Super Contributor
timestamp: September 26, 2020, 12:01:58 pm
content: Relay coil isn't some magical voltage spike generator causing a "problem" that is then filtered. The voltage spike is simply caused by interrupting current going through inductance (which is storing energy) without having a path for said current. Voltage rises until there is a path, either by arcing over, avalanching somewhere, or just dissipating into equivalent capacitances of the surroundings. So the problem doesn't exists "at the relay", it exists because of how the current isswitched. Relay wiring inductancec is in series with the relay inductance. So having diode at the relay, you arestillswitching an inductive load (the wiring). And doing thatwithoutfreewheeling that inductive loop!Proper driver makes sure there always is a path for the current. This driver consists of two switches, and is also called "half bridge". One of the switches can be a diode.If you place the diode at the relay, you have created two inductive loops: the relay coil loop, and the bridge loop (formed by capacitor, diode and transistor), and the latter is relatively large (compared to what it could be with proper layout). So you have created anadditionalproblem.If you place the diode at the bridge where it obviously belongs to, and add bypass capacitance, then you have minimized that loop. The total coil inductance is now very slightly larger (sum of relay coil inductance and the wiring inductance), but this change is negligible because the relay coil already has a lot of inductance (because it has hundreds of turns of wire,and a core).Look at how switch mode converters and motor controllers are built, look at appnotes on buck converter layout, and there is absolutely no question about it. The situation is exactly the same, just it might be less demanding if you bring slew rate down more, which you can do here; also because currents are low.The switch node is noisy, yes, but moving the diode further away won't make it any less noisy. Quite the opposite; your bridge with tens of cm of loop inductance is a nice oscillator at a few MHz. If you are worried about the relay wiring radiating noise due to large dV/dt, I think the best idea is getting rid of that wiring. Usually, the relay and its driving logic reside on the same PCB. I tend to place the FET, gate pulldown, and diode/zener and the bypass cap right next to the relay on a PCB, then use a gate series resistor right at the MCU IO pin, and bring that (source-terminated) IO line for whatever distance it takes on the PCB.Limiting slew rate is almost always a very good idea given that relays are extremely slow anyway, and usually allows you to have all kinds of incorrect and weird practices with no ill effects.Mysterious microcontroller resets and other kinds of more subtle and hard-to-debug malfunctions are surprisingly common in circuits that drive relays. I have had my share too when I was a beginner. On the other hand, people are often more successful in large DC/DC converters and motor controllers, because they do their due diligence. Apply that knowledge to the most basic relay circuit, because the same principles apply, and you are good to go. This is, minimizing switching loops, using DC link bypass capacitors of both low-ESR, low-ESL types paralleled with higher ESR damping, and finally, slow down the switching to avoid unnecessarily sharp edges. With an FET, this would be achieved with a series resistor at gate.

author: rvalente
author_rank: Frequent Contributor
timestamp: September 26, 2020, 02:35:30 pm
content: So, instead of using just a diode, why not a RC snubber where you can control the dv/dt? Or even, a diode in series of a resistor?I understand there must be a path for the energy stored in the coil but, it does not have to be dissipated as fast as possible, right?E.g. Siemens sirius contactors (big industrial chunky relays) there are 3 types of suppressors: for AC: RC and varistor and also for DC diodes and they are always placed on the coil (maybe due to convenience but never in the controller, other than when some crazy noises appears on the cabinet hehe)

author: voltsandjolts
author_rank: Unknown
timestamp: September 26, 2020, 03:34:33 pm
content: The diode belongs across the coil to suppress the back emf, which it does by dissipating the energy, for the most part, in the coil resistance.Placing the diode further from the relay means that back emf current spike will travel through longer traces/wiring causing more issues.

author: Siwastaja
author_rank: Super Contributor
timestamp: September 26, 2020, 03:49:27 pm
content: Quote from: voltsandjolts on September 26, 2020, 03:34:33 pmMaybe show where I went wrong instead of just lazily re-instantiating the claim I was refuting. This isn't a popularity contest, you know? It's a complex matter where I might be overlooking something.Nothing prevents you from using an extra snubber at the relay, though, if you need long wiring.

author: Kalvin
author_rank: Super Contributor
timestamp: September 26, 2020, 04:49:13 pm
content: I tried to simulate this relay switch-off and switch-on transitions with the LT Spice and QucsStudio simulators using a lossless transmission line for a 10 meter cable/twisted pair, so that the freewheeling diode was either at the transistor end (near end) or at the relay end (far end). Unfortunately I was not able to get LT Spice converge properly with short cable lengths, and the QucsStudio gave too "nice" results for the simulation, so at this point I cannot present any good simulation data.But one could think the two cases as follows:1. The freewheeling diode is placed right next to the transistor and the relay is connected with a 10 meter cable to transistor's collector, and the current is switched off quickly from transistor. The voltage at the relay's terminals will rise quickly causing a fast transient current in the 10 meter cable until the diode at the transistor's collector starts conducting and the diode is able to dissipate the coil's energy. As the twisted pair nor any other wiring is perfect, there will be some EMI which can couple to the nearby wiring causing intermittent faults at the time the relay is switched off.2. The relay is connected to the transistor's collector with a 10 meter cable and the freewheeling diode is placed right next to relay, and the the current is switched off quickly from transistor.The voltage at the relay's terminals tries to rise quickly, but the freewheeling diode will start conducting and dissipating the relay's energy forming only a very small current loop with the relay, compared to the case 1. As the inductance of the wiring is very small compared to the relay's coil, the wiring will cause very little, if any, EMI problems to the system.Even if I was not able to simulate the circuit very well, I was still able to observe that placing a simple snubber close to the transistor improved the situation even more in both cases (diode placed at the transistor end or at the relay end. I simulated with 10 ohm resistor in series with 100nF capacitor between the transistor's collector and ground.Summary: I would still place the freewheeling diode as close to the relay as possible, and adding a small snubber between the transistor's collector and ground won't hurt at all. Also, I would try to slow the transistor turn-off time as much as possible/feasible, which would reduce the relay's back-EMF in the first place.

author: ejeffrey
author_rank: Super Contributor
timestamp: September 26, 2020, 08:31:24 pm
content: Quote from: Kalvin on September 26, 2020, 11:10:11 amThe problem is caused by the switch not the coil. Well obviously it's both but the switch is what causes the high dI/dt. The proper placement is at the switch. Basically you want the current right before the switch opens and right after to look as similar as possible. That will limit dI/dt almost everywhere and avoid large voltage spikes and EMI.

author: Bryan
author_rank: Frequent Contributor
timestamp: September 26, 2020, 08:39:04 pm
content: FWIW this is what I am switching, i just called it a relay as I suppose it has a coil, and it would inherit the same characteristics as a relay coil, but likely to a lesser extent. Does it have issues with creating noise I do not know. Can reach out to the mfg and see what they say. I do recall contacting them years ago and they had mentioned that no diode is required?https://www.clippard.com/products/electronic-valve-ev

author: ejeffrey
author_rank: Super Contributor
timestamp: September 26, 2020, 09:07:06 pm
content: Really I shouldn't say "the proper placement is at the switch". That is the _ideal_ placement but a diode at the relay coil will be good enough in a great many cases. There is nothing wrong with good enough if there is a practical reason to do it that way

author: Lindley
author_rank: Regular Contributor
timestamp: September 26, 2020, 09:15:59 pm
content: Quote from: Bryan on September 26, 2020, 11:20:57 amWith such a small off time down to 100ms you will probably have problems with an accurate timing as the physical speed of the relay contacts will not be that precise.As the device is DC , using a Logic Level mosfet would seem a natural choice, simply connect its Gate to the Arduino pin, 5v or 3v3 , along with a pull down resistor and thats it, though think the other guys will advise on the best use of the flyback diodes.FDP7030BL is one type I use, or the IRL range, but choose one with a very low On resistance RDSon, so it runs cooler and may not need a heatsink.example circiut -Ediit, very few logic level modules on the usual sale sites, but like many folk, had one of these relay boards in the junk box, quiet simple to remove the relays and fit LL mosets instead, and just a little rewiring. All complete with in and output connectors and led indicators for each channel.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: October 04, 2020, 04:20:52 am
content: Quote from: Bryan on September 26, 2020, 08:39:04 pmThat isNominal Power: 0.67 wattsVoltage:12 VDC or 24 VDCso you need a driver stage, either NPN transistor or MOSFET with >> 100mA rating, and a catch/flyback diode, or a Zener (eg 48V) can be used if you need faster operation times.If you do not need independent LED and Solenoid operation, you have choice of drive of the LED from the MCU side, or the 12/24V side.

author: Bryan
author_rank: Frequent Contributor
timestamp: October 04, 2020, 07:56:39 am
content: Quote from: Lindley on September 26, 2020, 09:15:59 pmProbably should have been clearer, the device is probably more like a solenoid. Their specs state the coil has a response time of 5-10ms. It just arrived the other day so going to hook it up and see how it works.[url=https://www.clippard.com/products/electronic-valve-ev]https://www.clippard.com/products/electronic-valve-evAs for the Mosfet, I thought the gate should be in series with a 80 -120 ohm resistor to avoid any oscillations?

author: Siwastaja
author_rank: Super Contributor
timestamp: October 04, 2020, 08:32:06 am
content: Yes, a series resistor for gate is highly recommendable. Especially if the MOSFET is a larger one, the gate is quite a large capacitive load taking surge current only limited by the resistances of the systems, mostly the IO pin resistance. This means unnecessarily large current spikes in the microcontroller supply. Around 47 ohms is OK if you don't want to slow down switching, but you may actually want to limit switching speed (especially if you have followed the advice of increasing the switching loop area by placing the diode further away), which could be achieved by going for higher gate series resistor value.

author: AFremont
author_rank: Newbie
timestamp: October 06, 2020, 02:56:10 am
content: I know of no better way to analyze this problem and solve the fomenting argument, than to get one of these valves, build a test circuit with 12V and a pushbutton switch, and examine what happens on a oscilloscope. I see that the link to the part indicates .67W (~200 ohms?) of power, which seems to be significantly lower than an automotive relay (~1.5W). An undamped automotive relay can create a voltage spike of a couple of hundred volts, but many have a built in resistor (as opposed to a diode) in parallel with the coil, just because of this issue.I do agree that using a logic level mosfet is the way to go. Output pin impedance will generally limit the charge and discharge currents with the gate, but put in a little resistance if it makes you feel better.What intrigues me most about the "datasheet" for the valve is that they don't specify any internal resistance, but a power dissipation level of .67W. it's intriguing to me because they specify 12 or 24 volts is okay, but the dissipation is constant. How does that work?

author: Bryan
author_rank: Frequent Contributor
timestamp: October 06, 2020, 07:45:43 am
content: Quote from: AFremont on October 06, 2020, 02:56:10 amIf I have time this weekend I will breadboard it up and share the scope pics. As for 12v vs 24v, they offer two voltage models so I think the description is just shared. See attached pic of the actual specs for various models.

author: AFremont
author_rank: Newbie
timestamp: October 07, 2020, 12:09:42 am
content: That clears things up a lot, there are two different devices with the appropriate internal resistance to dissipate the claimed amount of power. The actual specs often bring a lot of clarity to marketing information.I will be curious to see the voltage spikes from the valve. Unless it is internally damped, I suspect we'll see spikes of well over 100V, assuming the scope captures at a high enough rate. These spikes are fairly short lived, but they eventually accomplish their goal after a few hundred jabs.I was honestly surprised, a couple of weeks ago, when I happened upon a case study of an automotive ECM destroyed by someone choosing an arbitrary replacement relay for controlling the fuel pump. The relays with an internal resistor dampen the spike significantly, the ones without a resistor generate up to 300V spikes. I've been tinkering with electronics and microcontrollers for quite a few years, built a bunch of things using logic level mosfets controlling relays, never had a failure, but I generally use a diode. I knew about spikes, just assumed that they were on the order of 20 or 30V, not 300V. For whatever reason, automotive fuel injectors generally create a 60V spike, even though they are much higher current than a typical relay, about 1A. I'm thinking maybe they have some kind of built in dampening, but I've never seen or heard of it anywhere. Automotive primary coils generate several hundred volt spikes, but at 10A, with another coil field coupling back onto it, I'm not that surprised.

author: Bryan
author_rank: Frequent Contributor
timestamp: October 07, 2020, 12:53:24 am
content: I have access to a 2465B scope, any tips on how to set up the triggers etc to monitor the spikes.

author: Lindley
author_rank: Regular Contributor
timestamp: October 07, 2020, 08:59:39 am
content: Be interesting to see the switch On and Off spikes from your relay set up.However you really need to put different loads onto the relay such as leds etc to establish some reference as to how good or bad your actuator actually is.A scope comparision between a Relay and Mosfet spikes would be more interesting.An informative article about relay loads -https://www.hw-group.com/cs/podpora/how-not-to-destroy-a-relay