Driving latching relay with attiny85 - Page 1
Forum section: electronics / general

author: dadler
author_rank: Unknown
timestamp: February 01, 2015, 11:04:14 pm
content: Hi all-I am working on a circuit similar to this:However, I am concerned about inductive transients when the (single coil latching) relay current pulse abruptly stops.I guess an H-bridge or equivalent, or something like a ULN2003 would certainly work, but seems like overkill for this application. Trying to keep the parts count/cost minimal. I also considered back to back zeners, but I don't think this will actually protect the inputs.I contacted the designer of the above circuit, and he claims transient protection is not necessary (didn't give a reason). I thought maybe he was doing something clever in software, just a guess.Here is the datasheet for the relay I am using: EA2-5SNJ (single coil latching)http://www.mouser.com/ds/2/283/ea2-1419.pdfAny input would help--I am a beginner.

author: JoeN
author_rank: Frequent Contributor
timestamp: February 02, 2015, 04:06:57 am
content: So you are using pins 6 & 7 basically as an H-bridge, setting one high and the other low and reversing the polarity to drive this relay? And it works right now? - as in there is enough current and you are not exceeding the ICs current sourcing/sinking specs? Well, if so that's great. The one thing is that I have never heard of anyone recommending driving any inductive load off an Atmel microcontroller or any microcontroller that isn't also a motor driver IC (Freescale does some of these) without protection. I found this and it seems it is the industry standard protection circuit for an H-bridge. It requires 4 diodes rather than one, but otherwise works in the same fashion.This is showing a 74LS244 doing the driving but you just substitute your pin 6 for the junction of pin 12 and 14 and pin 7 for the junction of 16 and 18 on that GIF. This all assuming that your microcontroller's pins are not being overdriven right now.

author: bktemp
author_rank: Super Contributor
timestamp: February 02, 2015, 10:26:12 am
content: It is hard to say if driving an inductive load is within specs or not, but it works. I have done simillar things multiple times without problems. But I would not use it on a product where a failling microcontroller can have expensive results.Since an AVR is CMOS, it has internal protection diodes on all pins. But the datasheet does not give a maximum current rating for those.One solution might be using only low and high instead of tristate. To remove power from the coil instead of switching the outputs to tristate, simply drive both pins low. This shorts both coil pins together and gives the stored energy a path to discharge without generating any spikes.

author: linux-works
author_rank: Super Contributor
timestamp: February 04, 2015, 06:33:58 am
content: one of the products I designed uses latching relays (for an audio attenuator).http://www.amb.org/audio/delta1/schematic and even code is available (arduino) - maybe that will give you some hints.

author: JoeN
author_rank: Frequent Contributor
timestamp: February 04, 2015, 05:21:26 pm
content: Quote from: linux-works on February 04, 2015, 06:33:58 amThat circuit uses ULN2003s, which I think is the way to go personally, but the original poster specifically said:Quote from: dadler on February 01, 2015, 11:04:14 pmI think he doesn't want to use a 7 channel IC just for one channel for aesthetic reasons. Those ICs are certainly cheap enough these days.

author: linux-works
author_rank: Super Contributor
timestamp: February 04, 2015, 05:48:47 pm
content: my point was to show the 'biasing trick' of keeping both relay lines up close to Vcc and then touching one end to ground (briefly! lots of current flows so it should be a millisecond or so, not very long at all) will latch it.the ULN was used for easy integration and even though it has more 'lines' than you'd need, its still a small enough chip and cheap enough. it has the built-in diodes protections and so its really easy to use.anyway, it was just an example to show how it can be done. we've been shipping boards and the circuit has been built by hundreds of people (I get email each time the source is downloaded) and so its a proven reliable circuit design.

author: cosmicray
author_rank: Frequent Contributor
timestamp: February 04, 2015, 06:01:27 pm
content: Quote from: dadler on February 01, 2015, 11:04:14 pmSingle coil latching usually requires a positive voltage to latch, and a negative voltage to unlatch. Another way of thinking is that you reverse the leads to the single coil, as all you really are aiming for is to reverse the voltage across the coil. The datasheet I'm looking at shows both + and - on each connection for a single coil latching example.

author: linux-works
author_rank: Super Contributor
timestamp: February 04, 2015, 06:26:36 pm
content: the trick I used was to keep both wires tied to plus and then touch an opposite side to ground (or the other one to toggle). this creates a polarity swap, pretty much, and that's the trick.you could also use an h-bridge but no matter what you do, you have to be sure to pulse the relay and be sure its not left with constant current going thru it.

author: Psi
author_rank: Super Contributor
timestamp: February 04, 2015, 06:46:20 pm
content: Two 1w 4.5v zeners should work wired back to back.( you get one zeners breakdown plus the others forward drop. So around 5v and above plus -5v and below the current will go into the zeners.The avr io can handle 5.5v so there is some room.Its far from ideal, a uln2003 is best, but the zeners is much better than nothing.

author: macboy
author_rank: Super Contributor
timestamp: February 05, 2015, 03:08:46 pm
content: Quote from: Psi on February 04, 2015, 06:46:20 pmSure it can handle +5.5 V w.r.t. ground, but the inductive kick will be -5.1 V (4.5 + 0.6) w.r.t. ground.I'd suggest that avoiding the fast turn-off is more appropriate. Adding a capacitor across the coil will accomplish this. The coil's sizable resistance (178 ohm) should dampen the LC circuit to prevent oscillation. The size of the cap depends on the coil. Too big and the voltage will drop too slowly; too small and it will still shoot negative for some time (and might ring as well). When it is just right, the voltage will quickly drop to zero without going negative.Another alternative is... nothing. The relay's coil has 178 Ohm of resistance, so the current at 5 V is just 28 mA. When you release the drive voltage, this current will keep flowing, and it will be shunted to Vcc or ground (depending on how exactly you drive the coil) via the micro's internal protection diodes. Those diodes can't handle much current, but with only brief 28 mA pulses, they might be just fine.

author: dadler
author_rank: Unknown
timestamp: February 25, 2015, 10:23:12 pm
content: So I got around to throwing this circuit on the scope. It's the exact circuit in the OP.The uC is issuing a 5ms pulse, driving one output pin high and the other low.There indeed is inductive spiking going on, but it seems to actually cause the attiny to sink/source the inductive current down to ground/vcc.This results in ~-2V potential at the uC output pins, which seems like a bad idea.This current appears to (dis)charge the linear regulator's output cap, resulting in ringing in the LC tank circuit seemingly formed by the coil's inductance and the 78L05 output cap. At least that's my beginner-level analysis, I am probably wrong.Here is a shot of one of the events in question:Either way, the circuit seems to work but I still feel uncomfortable about the inductive behavior.

author: ehud42
author_rank: Newbie
timestamp: February 05, 2023, 01:09:56 am
content: Poking at this thread as I'm working on an 'art' project where I'm taking over control of the simple stepper/coil in a clock. It needs to be driven +ve then -ve. I left the coil attached to the original clock circuit board (with the 32k crystal, etc - but battery removed).The coil needs less than 10mA to work. So well within what the ATTiny data sheet says it can do.Since the ATTiny can only source around 10mA, do I need a current limiting resistor? (Presumably I'm driving w/ 2x the voltage, so inducing twice the current?)And I'm concerned about back EMF - so I tried 2x 3.3V Zeners in opposing series+--mmmm--+| |P0-+-z<-->z-+-+|P2------------+My limited scope (old 100MHz Tektronix) does not seem to show any spike on the trailing edge - with or without the zeners. Might there be protection in the original clock circuit?(P1 is the builtin LED on the DigiSpark clone I'm using, and I'm using it as a heartbeat - since I'm toggling the clock coil only once every 8,760 seconds)

author: ledtester
author_rank: Super Contributor
timestamp: February 05, 2023, 01:53:31 am
content: Quote from: ehud42 on February 05, 2023, 01:09:56 amI think the GPIO pins are capable of more than 10mA.The Electrical Characteristics are presented starting on page 161 of the datasheet:https://ww1.microchip.com/downloads/en/devicedoc/atmel-2586-avr-8-bit-microcontroller-attiny25-attiny45-attiny85_datasheet.pdfThe footnotes at the end of Table 21-1 state:QuoteSo current above 10mA is not guaranteed but may happen.

author: Psi
author_rank: Super Contributor
timestamp: February 05, 2023, 02:12:44 am
content: If possible it's best to buy latching relays that have a separate coil for each position.The kind that require reverse polarity are annoying to drive unless you have an AC supply.

author: brucehoult
author_rank: Super Contributor
timestamp: February 05, 2023, 02:17:10 am
content: Quote from: ledtester on February 05, 2023, 01:53:31 amEspecially if you're only sourcing or sinking significant current from one pin (of each) at a time.

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 05, 2023, 07:35:49 am
content: Quote from: brucehoult on February 05, 2023, 02:17:10 amLooking at the primitive schematic ehud42 provided, he is using two IO pins to drive the coil in both directions, which means 1 pin is sourcing and the other sinking the same current. At the same time an already on the board present LED is lit on another IO pin, but if it is an SMD LED current will be low.In this scenario it is more the question what the steady state current of the coil is, when in case of some software issue, one IO pin remains high and the other low. If that current is to high it might fry things.About the zener diodes I would connect them to ground. This way each pin is limited to +3.3V and negative -0.7V.

author: brucehoult
author_rank: Super Contributor
timestamp: February 05, 2023, 08:04:59 am
content: Quote from: pcprogrammer on February 05, 2023, 07:35:49 amThat would be why I very carefully specified one source pin and one sink pin at the same time. And you might get as much as 60 mA in those conditions.QuoteCould well be 1 mA or less, yes.QuoteThe OP's relay data sheet states that it switches in 1-2 ms, and recommends holding the signal for 10 ms to ensure latching. So the steady state current is zero. It requires 100 mW to switch -- it is available in various coil voltages -- so as the 5V version is being used that is 20 mA.So, strictly speaking, it is above the per-pin rating of the ATTiny, but it is well below the per-port rating, so it should be fine if it is the only significant load being driven in that 10 ms.Well, that's the OPs data.The new thread tickler says their stepper motor needs less than 10 mA, so even better.

author: westfw
author_rank: Super Contributor
timestamp: February 05, 2023, 09:47:29 am
content: QuoteWhich per-pin rating are you looking at? The footnote mentioned is for the output voltages at a particular output current.Elsewhere the "absolute max" rating is listed as 40mA, and the 22-6 "Pin Driver Strength" tables show pretty reasonable voltages even at 20ma source/drain.Also, the AVRs are well known to source/sink current substantially above 10mA, especially if you're less worried about maintaining near-rail logic levels.PROBABLY, the ~10mA reverse current spikes you'd expect from a 10mA relay will be adequately dissipated by the "protection diodes" on the IO pins. And apparently it's a tested implementation. But of course it would be safer to double the parts cost of the project by adding some higher current H-bridge drivers and a diode bridge. :-)(and a ULN2003 is a sink-only driver, and isn't useful for driving one of these "reverse polarity to change state" latching relays.)

author: westfw
author_rank: Super Contributor
timestamp: February 05, 2023, 09:51:02 am
content: QuoteWell, OK.It looks like you can get an L9110-like H-bridge driver (with internal diodes as well) from Aliexpress for about $0.10...

author: PCB.Wiz
author_rank: Super Contributor
timestamp: February 06, 2023, 07:06:32 am
content: Quote from: ehud42 on February 05, 2023, 01:09:56 amWhatvoltageis the stepper coil rated at ?Current alone is not enough - FWIR, simple stepper clocks run from single 1.5V Alkaline cells ?If the clock is rated for an end-battery + losses of say 1Volt, you have a 100 ohm coil and you should design the AVR to mimic the design drive conditions.10mA drive is also a bigger ask at 3.3V supply, than it is at 5V supply.As an example, if the AVR drops 650mV at each driver at 3.3V Vcc and 10mA, you have 2V to play with, to get the required drive, so an additional 100 ohm series resistor could meet that.

author: westfw
author_rank: Super Contributor
timestamp: February 06, 2023, 08:53:31 am
content: QuoteThe 5V relay mentioned in the OP has a "must operate" voltage of 3.5V.Obviously if you want to operate on a 3.3V supply, you wouldn't use the 5V relay (a 4.5 and a 3V version are available.)There are also double-coil versions of the same relay. I don't know how things like cost would compare, but if you're actually buying new relays (as opposed to picking something up off the surplus market), it seems to me like the double-coil version would use the same number of pins as the single-coil version, with less "design complexity"

author: ledtester
author_rank: Super Contributor
timestamp: February 06, 2023, 09:47:23 am
content: Throwing this out here...This TE app note describes how to drive a a latching relay with a series capacitor. This allows you to use an ordinary relay driver/push-pull output if it can supply the required impulse current:https://www.te.com/commerce/DocumentDelivery/DDEController?Action=srchrtrv&DocNm=Axicom_AppNote_5&DocType=CS&DocLang=ENI found it in this Kerry Wong blog post:http://www.kerrywong.com/2014/12/12/how-to-drive-a-latching-relay/and he has additional discussion in this video:How to Drive a Latching Relay -- Kerry Wonghttps://youtu.be/cCYUpTyl3BA

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 06, 2023, 09:58:29 am
content: For the new question fromQuote from: ehud42 on February 05, 2023, 01:09:56 amthe capacitor would simplify things. Just a single pin of the MCU and less risk with one pin stuck high and the other low.It is funny though to see how this resurrection of old threads with new questions leads to answers to the original question.