# Flyback Diodes and Relays



## yeeeargh (Jul 12, 2020)

Hi everyone,

I’m experimenting with relay switching right now and have a question regarding flyback diodes. I had a look at different circuits and tested a few on a breadboard. I even traced my EQD Acapulco Gold to have a look how they’ve done the switching.

Here are a few examples:







Top right is the way it’s done in the PedalPCB intelligent relay bypass module: https://docs.pedalpcb.com/project/PedalPCB-IntelligentRelayBypass.pdf

While looking at the schematics of a few circuits to power a relay I stumbled over different designs. The first batch used a non-latching relay and pretty much all had a diode between the power pins of the relay.

I had noise problems with that design and stumbled upon circuits with single coil latching relays like the TQ2-L-5V or EC2-5SNU. The TQ2 is used in the PedalPCB relay bypass module and the EC2 is used in my EQD Acapulco Gold. With those relays you change the polarity of the two power pins to switch the relay on or off. This is the cause you can’t just use one diode across both pins to protect the ATTINY against the current coming from the relay when you change the state of the relay.

Pretty much all info I could find said that you should use a flyback diode to protect the micro controller but the vast majority talked about non latching relays. The few resources which talked about latching relays said you could use back to back zener diodes (is this called H-Bridge protection?). The datasheet and application guides show a few examples but seem to lack explicit instructions for single coil latching relays (Well, at least for me as l laymen). EQD use a resistor to ground on both pins and I not quite sure if they serve a similar purpose.

I’m struggling to decide on how for to go with this. Are the diodes necessary in this case or can I leave them off without damaging the ATTINY over time. If you have a few keywords for me for further research that would be great too.

Here’s a picture of circuit block with the switch and LED as I’m planning it right now:




Here a a few passages from the application guidelines regarding those diodes:






Datasheet: https://www3.panasonic.biz/ac/e_download/control/relay/signal/catalog/mech_eng_tq.pdf
General Applications Guidelines: https://www3.panasonic.biz/ac/e_dow.../catalog/mech_eng_cau_general.pdf?f_cd=401620
Applications Guidlines: https://www3.panasonic.biz/ac/e_dow...rm/catalog/mech_eng_cau_appli.pdf?f_cd=401623


----------



## Chuck D. Bones (Jul 12, 2020)

If you have even been holding onto a relay's terminals when someone de-engizes it, then you've received a lesson in inductive kick-back.  Happened to me in Electric Shop in Junior High and it left a lasting impression.  ?

All inductors store energy in their magnetic fields.  It's physics *.  As long as you don't change the current flow (magnitude or direction), then the energy stays put.  But if the current drops or reverses, then that stored energy comes out with a vengeance.  This is how car ignition coils work.  What this means for us is that if you drive a relay with a semiconductor, that semiconductor can get overstressed unless some other parts are put into the circuit to absorb the stored magnetic energy when it's released.

Check the ATTINY85 datasheet and see if it doesn't already contain protection.

I would hesitate to use an EQD pedal as an example of how circuit design should done.  EQD is not alone in this, but since you mentioned them by name above, I thought I should issue a warning.

There are a few ways to implement protection against inductive kick-back.  I'll look into this further and get back to you.  



---------------- If physics or math give you a headache, stop reading now. ------------

* The definition of an (ideal) inductor is a device that exhibits this voltage-current relationship:

V = L * di/dt

This means that the voltage across the inductor is equal to the inductance times the rate of change of the current.  The faster the current changes, the higher the voltage.  That's how we can make 20KV from a 12V battery in order to fire a spark plug.


----------



## phi1 (Jul 13, 2020)

I’m pretty interested in this... I’ve read through the Coda Effects blogpost about it a few times and was set to use a PIC like he did, but it seems that all the 5v relays require more power than the PIC should give (PIC pins rated for 25mA, TQ2-L-5 requires 28.1mA (140mW))**.  Other relays I checked out had similar requirements. I know transistor drive is an option, but I was trying to avoid that if not needed. Coda’s post even talks about the current specs, but then seems ignore the result... Therefore I was headed for the ATtiny since the outputs are rated for 40mA (let me know if all that sounds right). 

**edit: looks like I read the wrong chart, the 1 coil latching type is 100mW = 20mA. Still, attiny with has a larger margin, which seems preferable. 

The attiny85 datasheet says it has protection diodes (page 53) but I don’t see them spec’d out anywhere on that page or on the absolute max ratings page. I searched online and the consensus I saw was that they aren’t spec’d because you shouldn’t rely on them, so best to add external diodes if you may need them (these posts weren’t pedal or relay specific, just generally).  So I don’t see why it wouldn’t be a good idea to add them. 

BUT, pedalpcb and all the other diy kits I’ve seen around use attiny and don’t bother with fly back diodes.  Except for madbean, Which does PIC, transistor, and protection diode. 

Also, ditto chuck, EQD does some *interesting* stuff that shouldn’t be followed blindly.

@PedalPCB any insight is welcomed!


----------



## Chuck D. Bones (Jul 13, 2020)

Good stuff, Ph1l!  

Here's my interpretation of the datasheet re: relay drive and protection diodes...

Zooming in on page 53 (same as shown in Phi1's post), we can see the protection diodes to Vcc (+5V) and GND.





These diodes are intended to provide ESD protection, but can be used as flyback diodes provided we don't exceed the Absolute Maximum Ratings on page 161.  We can ignore Rpu for this discussion because Rpu is only in play when an I/O pin is configured as an input.





The line we're most interested in is *DC Current per I/O Pin*.  Also take note of the "weasel words" on the right.

That 40mA limit is how much current those protection diodes can tolerate without damage.  As long as we stay under the 40mA limit, we don't have to worry about the *Voltage on any Pin with respect to Ground* because too much heat and/or current are what will hurt those diodes.

When we interrupt the current on a relay coil, or any other inductor, the relay coil tries to keep the same current flowing while the stored magnetic energy is dissipated.  This means that if the ATTINY is putting 20mA into the coil and then stops driving the coil, the peak current that relay sends back into the protection diodes will be 20mA.  That current will taper off to zero in a small fraction of a second as the magnetic energy is dissipated in the diodes and coil resistance.

So how much current does the ATTINY put into the relay coil?  

The MOSFETs that drive the I/O pins act like switches with series resistors at low to moderate currents.  At some point, they run out of juice and limit the current.  Lower down on page 161, the datasheet gives us a hint about I/O pin drive capability:





This part of the table says that if you draw 10mA to/from an I/O pin, it's guaranteed to get within 0.6V of ground or 0.7V of Vcc.  Doesn't tell us anything about what happens at other currents.  Figures 22-20 and 22-22 on pages 183 and 184 give us the info we need.  The relay coil is driven at each end by an I/O pin.  Most of the time, both I/O pins are off and the coil current is zero.  When the ATTINY wants to latch or unlatch the relay, it drives one I/O pin high and the other I/O pin low.





Fig 22-20 is the I/O pin drive capability when it's pulling the output toward Ground.  We'll focus on the 25C line since we'll be operating our pedals close to that temp.  It's pretty much a straight line.  The resistance is the slope of the line, about 22 Ohms.  



 

Fig 22-22 is the  I/O pin drive capability when it's pulling the output toward Vcc. Here, the resistance is about 25 Ohms.  

The relay coil current is Vcc divided by both I/O pin resistances + the relay's coil resistance. 

Panasonic's TQ2 datasheet says that the coil resistance of the TQ2-5V latching relay is 250 Ohms.  Hang on, we're almost there...

The nominal coil current is: 5 / (22 + 25 + 250) = 16.8mA
The nominal coil voltage (taking into account the voltage drops in the I/O pins) is 16.8mA * 250 Ohms = 4.2V
I say "nominal" because all of these numbers have tolerances.  

The relay is guaranteed to operate correctly as low as 75% of the rated coil voltage, which is 3.75V for a 5V relay.  We have positive margin on the relay drive because 4.2V > 3.75V

We have positive margin on the I/O pin stress rating because 16.8mA < 40mA

So now we know that the ATTINY can drive the relay reliably and it can absorb the inductive kick-back without damage. 

*"Ya follow?"


*


----------



## zgrav (Jul 13, 2020)

Thanks Chuck.  I appreciate the step-by-step discussion there.


----------



## Chuck D. Bones (Jul 13, 2020)

yeeeargh said:


> Here are a few examples:
> 
> View attachment 5427View attachment 5428
> 
> Top right is the way it’s done in the PedalPCB intelligent relay bypass module: https://docs.pedalpcb.com/project/PedalPCB-IntelligentRelayBypass.pdf



Top left, the diode is drawn backwards.
Top right, we now know that's ok.
Bottom left, WTF do those resistors do?  Nada, Zilch, _Bupkess_.
Bottom right, the zeners may or may not conduct before the internal protections diodes do.  The zeners don't help, but they don't hurt either.


----------

