Computing stuff tied to the physical world

Direct relay switching – final

In Hardware on Mar 19, 2013 at 00:01

To conclude this little excursion into relay switching, here is the final circuit:


I’ve re-wired everything to use two DIO pins, and removed the extra 10 Ω resistor:


Bottom side:


The sketch can now switch both I/O pins using a single instruction, and avoid the brief shorting out of the I/O pins on the ATmega:

void setup() {
  digitalWrite(5, 0);
  digitalWrite(6, 0);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

static void setLatch (bool on) {
  // change both I/O pins in the same clock cycle
  if (on)
    PORTD |= bit(5) | bit(6);
    PORTD &= ~ (bit(5) | bit(6));

void loop() {

This also gets rid of the inductive kickback, or rather, it “diffuses” that effect into the slow capacitor charge and discharge ramps. All in all, I think this setup is a viable circuit for low-power switching of this tiny relay.

I’ve ordered a few more of these units, and intend to try them out with live 230 VAC mains switching. Note that this is pushing it, and that it will most definitely not meet all rules and regulations for mains separation and isolation – but for just controlling small lights, and other small devices such as power adapters and chargers, I think it’s still usable, and am willing to try it – at least for personal use around the house here at JeeLabs.


  1. Any reason you selected the 5v version of the relay and not the 3v or 4.5v versions?

    • It was a gift… one does not look gift horses in the mouth :)

      Actually, I think it is not a bad choice in the end. The lower voltage versions will have coils with a lower resistance, which might just be too much for the ATmega I/O pins. So far, my 2 units have been switching reliably down to 2.9V (without switching load).

  2. Any reason not to just use SSRs and avoid this drama of coils and multiple pins? Clare and IXYS amongst others make a slew of parts designed for DC control, AC loads, in all manner of current ratings.

    • Power consumption, for one: latching relays stay on and off without applying power.

  3. Could you use a varistor across the coil instead?

    • I’m not too familiar with varistors, I assume it’d work for surge suppression, but the cap also acts as differentiator: it only lets current through at the start of its charge/discharge cycle.

  4. That capacitor does not prevent the voltage generated from inductive kickback! Every time you switch that relay, a spike is generated back to the chip output. While it may be “diffused” by the cap, it is still there. A better alternative would be to either put a cap, diode or varistor across the coil (backwards). This would also speed up the relay switching which would allow a greater load across the contacts. Right now the relay cuts on when it is ready to, when the must close voltage is achieved, on a 5 volt relay that is around 4 volts, so it will not switch on right away with that cap in there. Even if that relay is designed to switch a load at 220 volts (some are, some are also UL rated), I would not expect it to switch any more than 50 to 60 watts on a resistive load, even less on an inductive load. An inductive load would also need snubbing across it using a diode or varistor. That should be in the data sheet for the relay.

  5. Duh, I should have read the whole series before I wrote the previous comment! I see you are using the cap as a source to switch the relay, I should have known better! The load part is still valid though.

  6. Indeed, switching 230 VAC mains may well be quite tricky. I’m hoping it’ll work for resistive loads such as small lamps up to 30 W or so, as well as chargers and other devices powered by wall warts.

  7. I did find a spec sheet for that relay and it is UL and CSA certified. Must operate voltage is 3.75v, and max switching voltage is 220 Vdc and 250 Vac. Max power is 30W resistive and 62.5 VA resistive.

  8. Why are two GPIO pins that move in the same direction (logic 1 or logic 0) used; to be able to source/sink more current? How did you know to use 2 pins and not just 1, or more than 2?

    By the way, on a related topic — some water valves (of the type used to water lawns and gardens) available here in the US use similar latching devices to open/close the water flow. I have been working on a circuit to operate these valves without the original controller from the manufacturer of the valves, so I have done some research. I only bring this up here because the original controller uses a big capacitor to generate the approximately 4 Amp pulse needed to active the solenoids in the valves. I documented what others have found regarding operation of these valves, plus my own findings, here:

Comments are closed.