I need to control several 5V single coil latching relays. The specific part has a coil current of ~20mA. The ATMega's datasheet claims a maximum pin current of 40mA. Is there a reason I cannot simply connect 2 pins from the AVR to the 2 sides of the coil and fire whichever one I need (on or off) to control it? I understand I'll need to play with the on/off timing a bit, but is this likely to damage the AVR in any way? Do I need a protection diode here? Thanks.

  • \$\begingroup\$ Check the output voltage of a GPIO pin with maximum load. If you are lucky you might pull it off by connecting four or eight pins of a port in parallel. You don't want to use pins from different ports as you have to switch them all at exactly the same moment. \$\endgroup\$
    – jippie
    Apr 13 '15 at 18:51

My thought is that your AVR controller should drive the relay directly just fine - provided that you do NOT switch the pins to input while the relay coil is energized.

Set both pins LO. Relay doesn't change state. Set one pin HI, wait a while, set that pin LO again. Relay changes to one state (or doesn't change, depending where it was originally).

When it's time to change the relay over, set the other pin HI, wait a while, set it LO again. Relay changes to the other state.

The only time there will be a transient is during the time when the pin is switching from one state to the other.

I am assuming that the protection diodes inside the chip will handle will handle the peak current that the pins are rated for - those diodes are a parasitic part of the output MOSFETs on the chip. They can't handle a significant amount of energy for any length of time but they should be able to handle the transients that you are going to get from turning the relay coil OFF until the other MOSFET has turned ON.

That is: the transient lasts only as long as it takes for the pin to change state from HI to LO or vice-versa.

I'm also assuming that you are driving one relay per pair of pins and that you aren't firing multiple relays at the same time.

If this is a low-energy design - that is: the entire circuit consumes only a few mA current, you may need to add a clamp to the + rail to stop it from rising.


Personally, I'd be putting a DSO on the supply rail and observing what happens as you fire the relay from one state to the other.


Note that the discussion above talks about whether it is safe to drive your relay directly from the controller pins. Now you need to find out if your controller will actually drive your relay. That is: will it supply enough voltage to properly switch your relay.

You say that the relay requires 20 mA @ 5V. That is a 250 Ohm load that is being driven by two output pins that are effectively in series. That is: the Rds_on of the pin that is held LO is in series with the Rds_on of the pin that is going HI. You need to find those Rds_on values for your controller under worst-case conditions and ensure that they allow sufficient voltage to the relay coil to switch properly.


You will need some form of protection from the back-EMF from the collapsing magnetic field of the coil, otherwise you risk damaging the MCU's IO pins.

You cannot use a diode in this case, since the diode would short out the coil when driven in one direction.

Ideally you would isolate the relay from the MCU using a H-bridge. If you really can't do that then a simple resistor across the coil may be enough to protect the IO pins, but getting the right value could be tricky. You need to:

  • Have it low enough to absorb the current
  • Have it high enough that it doesn't consume too much current from the MCU

Note that the 40mA quoted is the absolute maximum current, and Atmel don't guarantee usage over 25mA.

So really you want it to only consume a couple of mA, which means a value in the order of 1-2KΩ.

  • 1
    \$\begingroup\$ You can use 4 diodes - one from each GPIO to +5 and one from each GPIO to ground (in the appropriate direction of course). That will keep the GPIO lines form going (much) outside the supply rails. kevin \$\endgroup\$ Apr 13 '15 at 19:47
  • \$\begingroup\$ @Kevin: Those diodes may already be present in the microcontroller. If so, one needs to check whether they are able to withstand the current. If using external diodes, it is advisable to use schottky diodes, because they have lower forward voltage and faster switching, both of which protect the GPIO pin better. \$\endgroup\$
    – sh-
    Apr 20 '19 at 12:04

If you want to control "several" relays I'd be concerned about maximum current taken by the supply pins. You might get one or two relays working but the total current of several relays might be beyond the limit of the power pins.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.