Flyback Diodes and Relays

yeeeargh

New member
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:

relay2.png relay3.png

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:
relay1.png

Here a a few passages from the application guidelines regarding those diodes:
tq2-application1.png tq2-application2.png

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
 
Last edited:
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.
 
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!
 

Attachments

  • 3EAC6105-A22D-4EF3-98BB-6C752129B07B.jpeg
    3EAC6105-A22D-4EF3-98BB-6C752129B07B.jpeg
    436.2 KB · Views: 17
Last edited:
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.

ATTINY protection diodes.png

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.

ATTINY Abs Max Ratings.png

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:

ATTINY Vol & Voh.png

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.

ATTINY Vol vs Iol.png

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.

ATTINY Voh vs Ioh.png

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?"
robert-shaw-the-sting-poster-1.jpg
 
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.
 
Here's my interpretation of the datasheet re: relay drive and protection diodes...

I wish I'd found this absolutely delightful post earlier! In all my web searches for terms related to this topic, I never came across this post. Woe is me!

I did manage to start a lot of discussion about on other forums, though:
As a side note: at least some of the relays have 4.5v variants (instead of 5v), which makes their nominal voltage much closer to the 4.2 volts the ATtiny is delivering.
 
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.
...
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.

@Chuck D. Bones - would you be willing to elaborate on that? Won't there be some voltage at which the MCU is likely to be hurt, even if the current is under the limit? As datapoint, in the diyAudio thread from my previous post, someone claims to have measured 600v on the relay coil.
 
As a side note: at least some of the relays have 4.5v variants (instead of 5v), which makes their nominal voltage much closer to the 4.2 volts the ATtiny is delivering.
Bear in mind that a relay requires a certain amount of POWER delivered to the coil to activate the relay. That means that relays from the same family with a higher rated coil voltage will have a lower required coil current, and vice-versa. Many of the relays we use in pedals have 140mW coils.

In the example above, a 4.5V coil requires 10% less voltage than a 5V coil, but 10% more current. This can be a losing proposition if we're already at the limit of the microcontroller's output current capability.

Something else to keep in mind is that too many people build a circuit, see that it works one time and conclude that the design is sound. Rigorous engineering practice dictates that we should analyze and test the circuit under extreme conditions to determine how much design margin we have. The parts we buy are almost always better than the datasheet pass/fail limits. That is no guarantee that the next batch of parts will be as good as the last batch. We have to accommodate component tolerances and manufacturing variability. Unless you know how much margin you have, you're just rolling the dice.
 
@Chuck D. Bones - would you be willing to elaborate on that? Won't there be some voltage at which the MCU is likely to be hurt, even if the current is under the limit? As datapoint, in the diyAudio thread from my previous post, someone claims to have measured 600v on the relay coil.

If the microcontroller has internal protection diodes, then those diodes ensure that the applied voltage does not exceed the safe threshold. BUT, as I stated earlier, those diodes do not have an infinite capacity to absorb energy. At a high enough current, the diodes will be damaged and the rest of the chip along with them.

Now for a brief physics lesson...
Relay coils are inductors and inductors store energy in their magnetic field. As long as the inductor current is constant, the amount of stored energy is constant. If we try to reduce the inductor current, some (or all) of the stored energy will be released via the inductor's (relay coil's) leads. That energy has to go somewhere. If we have a diode in parallel with the coil, then the released energy will be dissipated in the diode. If we don't have a diode in parallel with the coil, then the energy goes somewhere else, such as the internal protection diodes in the microcontroller that's driving the relay. The collapsing magnetic field tries to maintain a constant coil current, at least briefly. A 178Ω coil driven by 4.2V will have a coil current of 23.6mA. When the microcontroller stops driving the relay coil, then the coil will (briefly) push 23.6mA into the microcontroller's protection diode(s) until the energy is dissipated. If the protection diodes are rated for more than 23.6mA, then we're good.

The relay's coil voltage will go as high as necessary to make that 23.6mA current continue to flow. I believe that "someone" could have measured 600V. It can easily go higher. At some point we either get an arc or the relay coil's insulation breaks down. Did "someone" describe the conditions under which the 600V measurement was made?

If you want to try a fun experiment,* with one finger touch both coil terminals of a relay and have someone apply power to the coil. You'll feel nothing. Now have them abruptly remove the power and THEN you will feel something. Been there, done that. The slang term is "inductive kickback" and it is the same principle that allows car ignition coils to make 20KV.

* This experiment has serious safety concerns. Do not attempt it if you have a pacemaker or a weak heart.
 
Except for cost and PCB space, are there any downsides to de-coupling the MCU from the relay via transistors and protection diodes?

Where I'm coming from: I'm trying to create something of a generic framework for microcontroller-based relay bypass. As you've shown, the ATtiny should be OK to drive the relays discussed here. But my thinking is, what if we want to use a different relay that uses more power? Or what if we want to use an MCU that can't sink/source enough current to drive a relay coil, or doesn't have protection on the IO pins?
 
I'd use a BS170 to drive the relay. You can drive the BS170 directly from the MCU. Or use a 2N3904 with a 1K series resistor between the base and the MCU. In either case, you will need a flyback diode on the relay coil. The drain (collector) can also be used to drive the SW terminal on a PedalPCB board.
 
If we're driving the relay with a transistor, then 1N4148 or 1N400x is fine. If we're driving the relay with an IC, such as 555 or an MCU, 1N4148 might be fine but schottky provides the ultimate protection because the flyback voltage is clamped by a lower Vf. This ensures that the internal protection diodes never turn on.
 
Back
Top