Try Embedded

A latching relay that remembers its position

Set and reset a bistable relay with 100 ms pulses from two buttons, and see it hold a 24 V lamp on with no power in its coils.

Industrial · Intermediate

In a real product: Smart light switch (Smart home): A latching relay holds the light's state with no power in its coil.

An ordinary relay needs current through its coil for as long as the contact must stay closed. A latching (bistable) relay has a permanent magnet that holds the armature wherever it was last pulled to, so it needs only a short pulse to change and uses no power while holding. It also keeps its position through a power cut.

What you'll learn

  • The two coils of a latching relay: A1 sets it, A3 resets it, and they share A2
  • Switching a 24 V coil from a 5 V pin through a relay module
  • Why a coil gets a short pulse, and why the two coils must never be on together
  • Using a changeover contact (11, 12, 14) to choose between two lamps

How it works

Button 1 on pin 2 means SET and button 2 on pin 3 means RESET. Each press makes the sketch close one channel of the relay module for 100 ms. Channel 1 puts 24 V across the set coil (A1-A2), channel 2 across the reset coil (A3-A2).

The relay's common contact 11 is fed 24 V. While it is set, 11 is joined to 14 and the green lamp is lit; while it is reset, 11 is joined to 12 and the red lamp is lit. Between pulses neither coil has any voltage, and the relay still holds its position.

Try this

  • Press SET twice: nothing changes, the relay is already there.
  • Make the pulse 5 ms long and see whether it still switches (the simulator needs the coil at 70 % of its voltage).
  • Add a third button that toggles: read the state with the changeover contact through an optocoupler.

sketch.ino

// Two buttons set and reset a latching relay with 100 ms pulses through a 2-channel relay module.
const int SET_BUTTON = 2;    // to GND, internal pull-up
const int RESET_BUTTON = 3;
const int SET_COIL = 8;      // relay module IN1: 24 V across the set coil while HIGH
const int RESET_COIL = 9;    // relay module IN2
const int PULSE_MS = 100;

void pulse(int pin) {
  digitalWrite(pin, HIGH);
  delay(PULSE_MS);
  digitalWrite(pin, LOW);  // never leave a latching relay's coil on
}

void setup() {
  Serial.begin(9600);
  pinMode(SET_BUTTON, INPUT_PULLUP);
  pinMode(RESET_BUTTON, INPUT_PULLUP);
  pinMode(SET_COIL, OUTPUT);
  pinMode(RESET_COIL, OUTPUT);
  Serial.println("SET or RESET the relay");
}

void loop() {
  if (digitalRead(SET_BUTTON) == LOW) {
    pulse(SET_COIL);
    Serial.println("Set pulse sent");
    while (digitalRead(SET_BUTTON) == LOW) {}  // one pulse per press
  }
  if (digitalRead(RESET_BUTTON) == LOW) {
    pulse(RESET_COIL);
    Serial.println("Reset pulse sent");
    while (digitalRead(RESET_BUTTON) == LOW) {}
  }
}

Lessons behind it