Try Embedded

Guard door interlock with a limit switch

Read a guard door's limit switch and a start button, run a machine only while the door is closed, and stop it with a red light and buzzer the moment it opens.

Industrial · Intermediate

In a real product: Industrial machine guard (Industrial): The machine runs only while its guard door is closed.

Machines have guards, and guards have interlocks: a switch that tells the control system the door is closed. Open the door and the machine must stop. This project builds the logic with a roller-lever limit switch on the door.

What you'll learn

  • A limit switch's two contacts: 13-14 closes when the lever is pushed, 21-22 opens
  • Why "door closed" is the active state: a broken wire reads as "door open", which is safe
  • A small state machine: IDLE, RUNNING and GUARD OPEN
  • Switching 24 V tiers of a stack light, and its buzzer, with solid-state relays

How it works

With the door closed the lever is pushed, so 13-14 passes 24 V to optocoupler IN1: the sketch reads it as LOW on pin 2. The start button's 13-14 contact feeds IN2 (pin 3).

The sketch starts the machine only when start is pressed and the door is closed. It switches the green tier on with one SSR. If the door opens while it is running, it switches green off and the red tier on, and sounds the buzzer until the door is closed again and start is pressed once more: it does not restart by itself, which is what a safety standard requires.

Try this

  • Click the limit switch to open and close the door while the machine runs.
  • Close the door without pressing start: the machine stays stopped.
  • Wire the NC contact (21-22) too and compare the two readings in a sketch.

sketch.ino

// A guard door interlock: run only while the door's limit switch reports it closed, and never restart by itself.
const int DOOR_CLOSED = 2;  // optocoupler OUT1: LOW while the limit switch's 13-14 is closed (door closed)
const int START = 3;        // optocoupler OUT2: LOW while the start button is pressed
const int RUN_LIGHT = 8;    // SSR for the green tier
const int ALARM = 9;        // SSR for the red tier

enum State { IDLE, RUNNING, GUARD_OPEN };
State state = IDLE;

void setup() {
  Serial.begin(9600);
  pinMode(DOOR_CLOSED, INPUT);
  pinMode(START, INPUT);
  pinMode(RUN_LIGHT, OUTPUT);
  pinMode(ALARM, OUTPUT);
  Serial.println("Idle: close the door and press start");
}

void loop() {
  bool closed = digitalRead(DOOR_CLOSED) == LOW;
  bool start = digitalRead(START) == LOW;

  switch (state) {
    case IDLE:
      if (start && closed) { state = RUNNING; Serial.println("Running"); }
      break;
    case RUNNING:
      if (!closed) { state = GUARD_OPEN; Serial.println("GUARD OPEN: stopped"); }
      break;
    case GUARD_OPEN:
      if (closed && !start) { state = IDLE; Serial.println("Door closed: press start to run"); }
      break;
  }

  digitalWrite(RUN_LIGHT, state == RUNNING);
  digitalWrite(ALARM, state == GUARD_OPEN);
}

Lessons behind it