Controlling a latching relay, like HFD2/012-S-L2 (datasheet [PDF]) with two MCU pins is straightforward:

enter image description here

And here is the corresponding code:

void switchRelayTx() {

void switchRelayRx() {

But when you need >= 10 relays in the project you end up out of MCU pins very soon. Thus I wonder if it's possible to control such a relay with only one MCU pin, maybe by utilizing a High-Z pin state, and without too many additional external components? Off the top of my head, I couldn't come up with the schematic.

Simply putting an "I2C pin number extender" like PCF8574 is possible of course, but I'm curious if there is a little less brute-force solution. Using a regular relay is not an option in the project because of the extra current such relays consume. Using a single-coil latching relay could be an option though.

  • \$\begingroup\$ "Using a single-coil latching relay could be an option though." That was the first thing that came to my mind too when reading this. So why don't you? \$\endgroup\$
    – Lundin
    Jun 17, 2021 at 7:46
  • \$\begingroup\$ Sure it is, but how much complexity in the form of circuit design and components you are willing to add in order to enable driving of dual or single coil relay with single GPIO pin? Which STM32 model is that, the PC15 is usually a weak output pin and it looks like your design exceeds ratings for a weak output pin. \$\endgroup\$
    – Justme
    Jun 17, 2021 at 8:01
  • \$\begingroup\$ @Lundin I must admit I never used such relays in practice. My understanding is that they are controlled by reversing the polarity on the coil. Can it be done with a single MCU pin? \$\endgroup\$ Jun 17, 2021 at 8:38
  • \$\begingroup\$ I suppose you could use the relay itself to toggle which side of the coil you drive. But then you have to sacrifice contacts or use a relay with more of them. \$\endgroup\$
    – Lundin
    Jun 17, 2021 at 8:50
  • \$\begingroup\$ What about keeping the 1 GPIO in the high impedance (Input) mode while Idle, and then drive it either Low or High, depending on the direction you want to switch the relay? That would require you to change transistors/resistors wiring, so they would conduct in Low/High, but not in Hi-Z state. \$\endgroup\$
    – akwky
    Jun 17, 2021 at 14:03

3 Answers 3


Using a 3-state pin is a workable approach. Use a pair of comparators to detect when the pin is below 1/3 VCC for one coil and above 2/3 VCC for the other coil. Then your code defines 3 states: drive low, drive high or no drive. The I/O would be tied to a midpoint voltage.

The LM339 has an open-collector output with enough current for the coil, so it would replace the transistor.

  • \$\begingroup\$ Maybe use this solution in combination with 3-state output shift registers. Then you can daisy chain them and only need an SPI interface. \$\endgroup\$
    – Lundin
    Jun 17, 2021 at 8:17
  • \$\begingroup\$ Exactly what I was looking for, many thanks! \$\endgroup\$ Jun 17, 2021 at 14:34

Using discretes you can use N+1 pins since you can multiplex if you want the set coils engaged or the reset coils engaged.

Common negative all set coils into an P-channel and all reset coils into an N-channel. If you drive the set/reset high, n-channel will conduct. if you pull it low the p-channel will conduct.

The you enable the common high side switch for the relay you want to change. This could be done with a CD4010 for example.

Or if you can afford to change the relays to 5V and can afford a +5V and -5V power supply sufficient to switch the relay you can do the above in a tri-state buffer chip or shift register. As hacktastical and lundin already hinted.

Using other chips you can use h-bridges or some MAX4820.


It possible, but inconvenient. You could consider using chips such as 74HC138 to control one relay at a time. For example 2 chips would allow you to control 8 relays with 5 control pins- 3 address plus S/R strobes. Or 7 pins to control 16 relays (that could be reduced).

You would use PNP rather than NPN transistors since that chip has inverted outputs. Ask if this is not clear how to implement.

  • \$\begingroup\$ Why not simply expand the outputs of the MCU with a chain of HC595? With any one 595 (8 outputs each) you can control 4 relays, but you only need 4 MCU pins (3 if you don't need to 3-state the outputs) to control the whole chain (which can be "arbitrarily long", since relays are slow anyway). \$\endgroup\$ Jun 17, 2021 at 13:39
  • \$\begingroup\$ @LorenzoDonati--Codidact.com Yes, that's one standard approach and could certainly work. The decoders have the (perhaps slight) advantage that they act as a hardware interlock to prevent more than one relay coil from being energized at once. \$\endgroup\$ Jun 17, 2021 at 13:45
  • 1
    \$\begingroup\$ Interesting consideration. I didn't think of that. Even if the relays don't need to be interlocked because of interacting loads, I guess it could be useful to avoid too large current spikes on the rail powering the relay coils. It can be done with SW delays but sometimes SW is buggy... :-) \$\endgroup\$ Jun 17, 2021 at 14:14

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.