26 min read · Automation

The machine stops mid-cycle and six alarm lights come on at once. Nobody knows which one came first. Or you press Start and the machine stays silent: it does not move, and nothing says which condition is blocking it. Or someone opens a door and the motor stops, then the door is closed and the motor starts turning again with no command. These three symptoms usually share one root cause. The machine's conditions were written together into one long run of logic, without separating which conditions mean permission to start, which mean a command to stop, and which belong to the order of the work.
This article separates these three ideas, both in the explanation and in the code. It uses a PLC-style scan-cycle simulator of an imaginary mixing tank, written in TypeScript and run on Node 24 with nothing to install. It includes real output to look at and a test suite to run. A manager can read the explanation and the tables and follow the picture. An engineer can run the code alongside.
Important before you start: machine safety functions, such as an E-stop and guard door switches, must be built with devices designed for safety or with a safety PLC, and assessed against standards such as ISO 13849-1 or IEC 62061. In this example the E-stop and the door are only simulated inputs, used to teach how to write the logic. Do not use them in place of a real safety circuit.
The real problem: machine conditions mixed together until they cannot be read
In a program that has grown over several years, machine conditions are usually patched one spot at a time, after each incident. The result is that the word interlock gets used for everything, from "the Start button must not work while the door is open" to "if the level is low, stop the motor". It gets worse when people on site understand the same word in different ways. This article uses three practical definitions. They are a team agreement, not wording from any particular standard, and another plant may define them differently, so agree on them at the start of the project.
- Permissive: a condition that must be true before the system accepts a start command. If it fails, the operator must be shown a list of which items are blocking the start.
- Interlock: a condition that stops or blocks a device while it is running. If it is a trip, it must latch and stay latched until someone deliberately resets it.
- Sequence: the order of the work, for example wait, fill, mix, drain, done. Each step has an entry condition, a time limit (timeout) and a safe action for when something goes wrong.
Consider three hypothetical situations. They are not from any BT San customer project.
- Chemical mixing tank: the operator presses Start and the machine does not respond, because a motor overload trip is still set from the previous cycle. The screen says nothing, so a technician has to be called to trace the wires one by one.
- Small packing line: a guard door is opened to clear a jam. The motor stops as it should, but when the door is closed the motor starts again at once, because the interlock did not latch. The person whose hands are still near the machine did not expect it.
- Mixer with several simultaneous trips: overload, low level and lost feedback appear in the same second. The team loses half a day looking for the cause, even though the first trip could have been identified immediately if first-out had been recorded.
What the documents say, and what must not be assumed
| Topic | What the document says | Effect on this article |
|---|---|---|
| IEC 61131-3 | The IEC webstore page says it is part 3 of the standard for programmable controllers, covering programming languages. It defines the syntax and semantics of a unified set of languages, including Ladder Diagram and Structured Text. | The example is written in TypeScript so that it can run, but the scan structure (read inputs, process, write outputs) follows the same pattern used in ST on a real PLC. |
| Sequential Function Chart | Wikipedia explains that a step can be active or inactive, an action runs only in an active step, and a transition has a condition attached. SFC is one of the languages defined by IEC 61131-3. | The steps idle, fill, mix, drain and done are the steps, and the conditions for changing step are the transitions. The example is written directly as a state machine and does not use real SFC. |
| Interlock | Wikipedia defines it as a mechanism that makes two mechanisms depend on each other, used to stop a machine from operating until a barrier is in position. | It is the source of the meaning of interlock used here, but that page does not mention the word permissive. The meaning of permissive in this article is therefore a practical agreement. |
| Machine safety | The IEC webstore page says IEC 62061 sets requirements for designing, integrating and validating safety-related control systems for machinery. Wikipedia summarizes ISO 13849 as applying to the parts of a control system that perform safety functions, with performance levels PL a to e based on the rate of dangerous failure per hour. | E-stops and guard doors must be designed and verified to these standards. Ordinary logic is not enough. |
| Recording the sequence of events | Wikipedia explains that a sequence of events recorder logs the time and order of input changes, so that the cause can be analyzed from the order in which things actually happened. | This is the same principle as first-out in the example. However, I did not find a public document that directly confirms a definition of the term first-out, so no standard is cited for it. |
| Alarm management | Wikipedia says there are guidelines such as ISA 18.2 and EEMUA 191, and describes the alarm flood phenomenon: a large number of alarms during a plant upset. | This is the reason to separate the first trip from the ones that follow. The details of the standards must be read from the actual editions. |
The condition ids (P_DOOR, IL_DOOR and so on), the timeout values and the time units in the example are assumed values so that it can run. When you use this for real, they must be set from the process and from the risk assessment of the actual machine.
Overview of the example
The example simulates one mixing tank. It has a fill valve, a drain valve, an agitator motor, high and low level switches, a motor overload input, run feedback, a guard door and an E-stop (simulated). Every file is small enough to read in one sitting.
| File | Role |
|---|---|
| types.ts | Data types, timeout values and machine states |
| plant.ts | Model of the tank, valves and motor, plus faults that can be injected, such as no water coming in or lost feedback |
| logic.ts | The permissive, the interlock and the gate that forces outputs off immediately |
| machine.ts | The step sequence, timeout, latch, first-out, reset and safe actions |
| sim.ts | A time stepper that advances simulated time in 100 ms steps, so results can be repeated |
| scenarios.json | Five scenarios, including cases that are deliberately bad |
| demo.ts | Runs every scenario and prints the event log |
| machine.test.ts | Tests 7 cases with node:test |
| output.txt | The real output of node demo.ts |
Run it with node demo.ts and node --test machine.test.ts in the example folder. It needs Node 22.6 or later (tested on 24). According to the Node documentation, enum types and namespaces that contain runtime code cannot be used, and imports must end in .ts, so the example is written that way.
Step 1: The machine model and the time stepper
A PLC works in cycles. It reads all inputs, runs the program once, then writes all outputs. The example uses a 100 ms cycle and simulated time, not a real clock, so the result is the same every time and can be tested. plant.ts says how each input results from the outputs. For example, run feedback comes on 200 ms after the motor is commanded, and does not come on if the overload trips.
import type { Inputs, Outputs } from "./types.ts";
// แบบจำลองถังกับมอเตอร์อย่างง่าย: ระดับ 0-100 เปลี่ยนตามวาล์วทุกรอบสแกน
export type Plant = {
level: number;
doorClosed: boolean;
estopOk: boolean;
overloadOk: boolean;
supplyOk: boolean; // false = น้ำไม่เข้าแม้วาล์วเปิด
fbDead: boolean; // true = run feedback ไม่ขึ้นแม้มอเตอร์ถูกสั่ง
agMs: number;
};
export const newPlant = (): Plant => ({
level: 0, doorClosed: true, estopOk: true, overloadOk: true,
supplyOk: true, fbDead: false, agMs: 0,
});
// simplified: อัตราไหลคงที่และไม่มีสัญญาณรบกวน; ของจริงต้องกรอง debounce สวิตช์ระดับ
export function plantStep(p: Plant, out: Outputs, dtMs: number): Inputs {
if (out.fill && p.supplyOk) p.level = Math.min(100, p.level + 2);
if (out.drain) p.level = Math.max(0, p.level - 5);
p.agMs = out.agitator ? p.agMs + dtMs : 0;
return {
estopOk: p.estopOk, doorClosed: p.doorClosed, overloadOk: p.overloadOk,
levelLow: p.level >= 10, levelHigh: p.level >= 90,
runFb: out.agitator && p.agMs >= 200 && !p.fbDead && p.overloadOk,
};
}import type { Machine } from "./types.ts";
import { SCAN_MS, newMachine } from "./types.ts";
import { newPlant, plantStep } from "./plant.ts";
import type { Plant } from "./plant.ts";
import { scan } from "./machine.ts";
export type SimEvent = {
t: number;
set?: Partial<Pick<Plant, "doorClosed" | "estopOk" | "overloadOk" | "supplyOk" | "fbDead">>;
press?: "start" | "reset";
};
export type Scenario = { name: string; note: string; endMs: number; events: SimEvent[] };
// เวลาในการจำลองเดินทีละ 100 ms ไม่ผูกกับนาฬิกาจริง ผลลัพธ์จึงซ้ำได้ทุกครั้ง
export function simulate(sc: Scenario, print?: (line: string) => void): { m: Machine; p: Plant } {
const m = newMachine();
const p = newPlant();
for (let t = 0; t < sc.endMs; t += SCAN_MS) {
const op = { start: false, reset: false };
for (const e of sc.events.filter((x) => x.t === t)) {
if (e.set) { Object.assign(p, e.set); print?.(`t=${String(t).padStart(5, "0")} EVENT ${JSON.stringify(e.set)}`); }
if (e.press) { op[e.press] = true; print?.(`t=${String(t).padStart(5, "0")} EVENT กดปุ่ม ${e.press}`); }
}
scan(m, plantStep(p, m.out, SCAN_MS), op);
for (const line of m.log.splice(0)) print?.(line);
}
return { m, p };
}Step 2: Permissive: say why it cannot start
The permissive is a function that returns a list of conditions, each marked pass or fail, not a single true/false value. The operator needs to know which item is blocking the start. A machine that stays silent when Start is pressed is the cause of many unnecessary service calls. What matters is that the permissive is checked only when a start command is received. It does not stop a machine that is already running. Stopping is the job of the interlock.
// ---- Permissive: เงื่อนไขที่ต้องจริง "ก่อน" รับคำสั่งเริ่ม ----
export type Check = { id: string; text: string; ok: boolean };
export function permissives(i: Inputs, m: Machine): Check[] {
return [
{ id: "P_ESTOP", text: "วงจร E-stop ปกติ", ok: i.estopOk },
{ id: "P_DOOR", text: "ประตูกั้นปิด", ok: i.doorClosed },
{ id: "P_OVERLOAD", text: "โอเวอร์โหลดมอเตอร์ไม่ทริป", ok: i.overloadOk },
{ id: "P_EMPTY", text: "ถังว่าง (สวิตช์ระดับต่ำแห้ง)", ok: !i.levelLow },
{ id: "P_NOLATCH", text: "ไม่มี trip ค้างรอ reset", ok: m.latched.length === 0 },
{ id: "P_IDLE", text: "ลำดับอยู่ขั้นรอ (idle)", ok: m.step === "idle" },
];
}
export const blockedReasons = (c: Check[]): string[] =>
c.filter((x) => !x.ok).map((x) => `${x.id} ${x.text}`);Step 3: Interlock: stop immediately and stay stopped until reset
This part has two layers. In the first layer, activeCauses checks which causes are true right now: the E-stop (simulated), the door open while the machine is running, overload, low level during mixing, no run feedback, and a step that has run past its time limit. In the second layer, gate forces the outputs off in the same scan cycle, without waiting for the sequence to notice first, because stopping a motor should not depend on which step is running.
// ---- Interlock: สาเหตุที่ต้องหยุด/ห้ามเดินขณะทำงาน ----
export type Cause = { id: string; text: string };
export function activeCauses(i: Inputs, m: Machine): Cause[] {
const live = m.step === "fill" || m.step === "mix" || m.step === "drain" || m.step === "fault";
const c: Cause[] = [];
if (!i.estopOk) c.push({ id: "IL_ESTOP", text: "E-stop ถูกกด" });
if (live && !i.doorClosed) c.push({ id: "IL_DOOR", text: "ประตูกั้นเปิดขณะเครื่องทำงาน" });
if (live && !i.overloadOk) c.push({ id: "IL_OVERLOAD", text: "โอเวอร์โหลดมอเตอร์ทริป" });
if (m.step === "mix" && !i.levelLow) c.push({ id: "IL_DRYRUN", text: "ระดับต่ำกว่าสวิตช์ต่ำขณะผสม" });
if (m.fbMs >= LIMITS.feedbackMs) c.push({ id: "FB_AGITATOR", text: "สั่งมอเตอร์แล้วไม่มี run feedback" });
const limit = { fill: LIMITS.fillTimeoutMs, mix: LIMITS.mixTimeoutMs, drain: LIMITS.drainTimeoutMs }[m.step as "fill"];
if (limit !== undefined && m.stepMs >= limit) {
c.push({ id: `TO_${m.step.toUpperCase()}`, text: `ขั้น ${m.step} เกินเวลา ${limit} ms` });
}
return c;
}// ---- ผลของ interlock ต่อเอาต์พุต: ทำงานทันทีในรอบสแกนเดียวกัน ----
export function gate(cmd: Outputs, i: Inputs, m: Machine): Outputs {
const ok = i.estopOk && m.latched.length === 0;
return {
fill: cmd.fill && ok && !i.levelHigh, // ระดับสูงปิดวาล์วเติม
agitator: cmd.agitator && ok && i.doorClosed && i.overloadOk && i.levelLow,
drain: cmd.drain && ok,
};
}The point that is often missed is that gate also uses the condition latched.length === 0, not just the current state of the door. If it looked only at the door, the motor would start turning again as soon as the door was closed. That is the second situation above.
Step 4: Sequence: steps, time limits and safe actions
machine.ts has two tables, held as data: the commands for each step, and the safe action for each step when a fault occurs. Writing them as tables lets you review them with the process engineer without reading the whole file. In this example every step turns all outputs off, but some real machines must keep certain devices running, such as a cooling pump, which is why they are separate tables from the start.
const OFF: Outputs = { fill: false, agitator: false, drain: false };
// การกระทำที่ปลอดภัยเมื่อเกิดความผิดปกติในแต่ละขั้น (กำหนดชัดเจนเป็นข้อมูล)
// simplified: ถังนี้ปิดทุกเอาต์พุต เครื่องจริงบางตัวต้องคงปั๊มระบายความร้อนไว้
export const SAFE_ACTION: Record<Step, Outputs> = {
idle: OFF, fill: OFF, mix: OFF, drain: OFF, done: OFF, fault: OFF,
};
const COMMAND: Record<Step, Outputs> = {
idle: OFF, done: OFF, fault: OFF,
fill: { fill: true, agitator: false, drain: false },
mix: { fill: false, agitator: true, drain: false },
drain: { fill: false, agitator: false, drain: true },
};The scan function is one scan cycle. The order within the cycle matters. First it counts time and checks feedback. Then it finds the causes and latches them. Then it looks at the reset button, then runs the step sequence, and finally passes through the gate before writing the outputs. Each step has an entry condition, for example mix can be entered only when the high level has been reached, and a timeout that activeCauses checks. If the time is exceeded it becomes a trip that takes the same path as an interlock.
export function scan(m: Machine, i: Inputs, op: Ops): Outputs {
m.nowMs += SCAN_MS; m.stepMs += SCAN_MS;
m.fbMs = m.out.agitator && !i.runFb ? m.fbMs + SCAN_MS : 0;
const causes = activeCauses(i, m);
for (const c of causes) if (!m.latched.some((t) => t.id === c.id)) latch(m, c);
if (op.reset && !m.prevReset) tryReset(m, causes);
if ((m.step === "idle" || m.step === "fault") && op.start && !m.prevStart) {
const why = blockedReasons(permissives(i, m));
if (why.length === 0) goto(m, "fill");
else say(m, `START ถูกปฏิเสธ เพราะ: ${why.join("; ")}`);
} else if (m.step === "fill" && i.levelHigh) {
goto(m, "mix"); // เงื่อนไขเข้า mix: ระดับสูงถึงแล้ว
} else if (m.step === "mix") {
if (i.runFb) m.mixRunMs += SCAN_MS;
if (m.mixRunMs >= LIMITS.mixRunMs) goto(m, "drain");
} else if (m.step === "drain" && !i.levelLow) {
goto(m, "done");
} else if (m.step === "done" && m.stepMs >= 500) {
goto(m, "idle");
}
m.prevStart = op.start; m.prevReset = op.reset;
const cmd = m.step === "fault" ? SAFE_ACTION[m.faultFrom ?? "fault"] : COMMAND[m.step];
m.out = gate(cmd, i, m);
return m.out;
}Step 5: Latch, first-out and a reset that must be deliberate
The latch keeps trips in a list in the order they occurred. The first one to arrive, when there is no first-out yet, is recorded as the first-out. Later ones are marked as having followed the first. The machine then enters the fault state and uses the safe action of the step that went wrong. The reset is accepted only on a new press (a rising edge, not a held button), and is refused if a cause is still present. A button that clears everything while the cause is still there is the recipe for a machine that trips again and again.
function latch(m: Machine, c: Cause): void {
const trip = { ...c, atMs: m.nowMs, step: m.step };
m.latched.push(trip);
if (m.firstOut === null) { m.firstOut = trip; say(m, `TRIP ${c.id} (first-out): ${c.text}`); }
else say(m, `TRIP ${c.id} (ตามหลัง ${m.firstOut.id}): ${c.text}`);
if (m.step !== "fault") { m.faultFrom = m.step; goto(m, "fault"); }
}
function tryReset(m: Machine, causes: Cause[]): void {
const still = causes.filter((c) => m.latched.some((t) => t.id === c.id));
if (still.length > 0) { say(m, `RESET ถูกปฏิเสธ สาเหตุยังอยู่: ${still.map((c) => c.id).join(", ")}`); return; }
say(m, `RESET รับแล้ว ล้าง ${m.latched.map((t) => t.id).join(", ")}`);
m.latched = []; m.firstOut = null; m.faultFrom = null;
goto(m, "idle");
}Limit of the example: first-out here is ordered by the 100 ms scan cycle. If two causes occur in the same cycle, the order follows the order of the checks in the code, not the real physical order. A real system that needs finer ordering must have time stamping at the input, or a high-resolution event recording module.
Real run: five scenarios
scenarios.json is plain data. It says what changes at what time (door opens, no water comes in, feedback is lost), or which button the operator presses. Bad cases are included on purpose, such as pressing reset while the cause is still present and pressing Start while the tank is not empty.
=== A. สั่งเริ่มตอนยังไม่พร้อม แล้วแก้ทีละข้อจนผ่านครบหนึ่งรอบ
(ประตูเปิดและโอเวอร์โหลดทริปตั้งแต่ต้น ต้องเห็นรายการเหตุผลที่เริ่มไม่ได้)
t=00000 EVENT {"doorClosed":false,"overloadOk":false}
t=00300 EVENT กดปุ่ม start
t=00400 [idle] START ถูกปฏิเสธ เพราะ: P_DOOR ประตูกั้นปิด; P_OVERLOAD โอเวอร์โหลดมอเตอร์ไม่ทริป
t=00800 EVENT {"doorClosed":true}
t=01000 EVENT กดปุ่ม start
t=01100 [idle] START ถูกปฏิเสธ เพราะ: P_OVERLOAD โอเวอร์โหลดมอเตอร์ไม่ทริป
t=01200 EVENT {"overloadOk":true}
t=01400 EVENT กดปุ่ม start
t=01500 [idle] -> fill
t=06000 [fill] -> mix
t=08100 [mix] -> drain
t=09800 [drain] -> done
t=10300 [done] -> idle
สรุป: ขั้น=idle ระดับ=5 first-out=- ตามมา=-
=== B. เปิดประตูระหว่างผสม แล้วทดสอบ reset ที่ต้องใช้จงใจ
(reset ขณะประตูยังเปิดต้องถูกปฏิเสธ ปิดประตูแล้ว reset ผ่าน แต่เริ่มใหม่ไม่ได้เพราะถังยังไม่ว่าง)
t=00100 EVENT กดปุ่ม start
t=00200 [idle] -> fill
t=04700 [fill] -> mix
t=05200 EVENT {"doorClosed":false}
t=05300 [mix] TRIP IL_DOOR (first-out): ประตูกั้นเปิดขณะเครื่องทำงาน
t=05300 [mix] -> fault
t=05600 EVENT กดปุ่ม reset
t=05700 [fault] RESET ถูกปฏิเสธ สาเหตุยังอยู่: IL_DOOR
t=06000 EVENT {"doorClosed":true}
t=06300 EVENT กดปุ่ม start
t=06400 [fault] START ถูกปฏิเสธ เพราะ: P_EMPTY ถังว่าง (สวิตช์ระดับต่ำแห้ง); P_NOLATCH ไม่มี trip ค้างรอ reset; P_IDLE ลำดับอยู่ขั้นรอ (idle)
t=06600 EVENT กดปุ่ม reset
t=06700 [fault] RESET รับแล้ว ล้าง IL_DOOR
t=06700 [fault] -> idle
t=07500 EVENT กดปุ่ม start
t=07600 [idle] START ถูกปฏิเสธ เพราะ: P_EMPTY ถังว่าง (สวิตช์ระดับต่ำแห้ง)
สรุป: ขั้น=idle ระดับ=90 first-out=- ตามมา=-
=== C. หลาย trip ซ้อนกัน ต้องจำตัวแรก (first-out)
(โอเวอร์โหลดทริปก่อน แล้ว E-stop ถูกกดตามมา ตัวแรกคือสาเหตุ ตัวหลังคือผลตามมา)
t=00100 EVENT กดปุ่ม start
t=00200 [idle] -> fill
t=04700 [fill] -> mix
t=05300 EVENT {"overloadOk":false}
t=05400 [mix] TRIP IL_OVERLOAD (first-out): โอเวอร์โหลดมอเตอร์ทริป
t=05400 [mix] -> fault
t=05500 EVENT {"estopOk":false}
t=05600 [fault] TRIP IL_ESTOP (ตามหลัง IL_OVERLOAD): E-stop ถูกกด
สรุป: ขั้น=fault ระดับ=90 first-out=IL_OVERLOAD ที่ t=5400 ตามมา=IL_ESTOP
=== D. น้ำไม่เข้า ขั้นเติมเกินเวลา
(วาล์วเติมเปิดแต่ไม่มีน้ำ ต้องมี alarm ขั้นเติม และปิดทุกเอาต์พุต)
t=00000 EVENT {"supplyOk":false}
t=00100 EVENT กดปุ่ม start
t=00200 [idle] -> fill
t=06200 [fill] TRIP TO_FILL (first-out): ขั้น fill เกินเวลา 6000 ms
t=06200 [fill] -> fault
สรุป: ขั้น=fault ระดับ=0 first-out=TO_FILL ที่ t=6200 ตามมา=-
=== E. สั่งมอเตอร์แล้วไม่มี run feedback
(คอนแทกเตอร์ไม่ดึงหรือสายสัญญาณขาด ต้องจับได้ภายใน 500 ms)
t=00000 EVENT {"fbDead":true}
t=00100 EVENT กดปุ่ม start
t=00200 [idle] -> fill
t=04700 [fill] -> mix
t=05200 [mix] TRIP FB_AGITATOR (first-out): สั่งมอเตอร์แล้วไม่มี run feedback
t=05200 [mix] -> fault
สรุป: ขั้น=fault ระดับ=90 first-out=FB_AGITATOR ที่ t=5200 ตามมา=-Reading the result scenario by scenario
- Scenario A: Start is pressed with the door open and the overload tripped. The system refuses and gives two reasons. After the door is fixed and Start is pressed again, one reason remains. When everything is fixed, it enters fill and runs through fill, mix, drain, done and idle with no trip.
- Scenario B: the door is opened during mix, which gives the trip IL_DOOR and the fault state. Pressing reset while the door is still open is refused. After the door is closed the machine still does not run by itself. Pressing Start while in the fault state is refused with reasons that include P_NOLATCH. A deliberate reset is needed to return to idle, and a new cycle cannot start immediately because the tank is not empty (P_EMPTY).
- Scenario C: the overload trips first, then the simulated E-stop follows 200 ms later. The system records IL_OVERLOAD as the first-out and IL_ESTOP as the one that followed. The repairer therefore starts at the motor, not at the E-stop button.
- Scenario D: no water comes in. The fill valve has been open for the full 6000 ms, which gives TO_FILL, and all outputs are switched off.
- Scenario E: the motor is commanded but there is no run feedback. This gives FB_AGITATOR at about 500 ms after the command, with no need to wait for the step's timeout.
The test suite
machine.test.ts has 7 cases, and node --test machine.test.ts gives 7 passes. They cover what the business cares about: the permissive refuses a start and lists every reason, the interlock stops a running motor and stays latched after the door is closed, first-out keeps the first trip, the timeout of the fill step raises an alarm, lost feedback is caught, and reset is refused while a cause is still present. If someone edits the code and breaks any of these mechanisms, the test suite fails.
test("reset: ต้องกดจงใจ และถูกปฏิเสธถ้าสาเหตุยังอยู่", () => {
const { m } = run([
{ t: 100, press: "start" }, { t: 5200, set: { doorClosed: false } },
{ t: 5600, press: "reset" }, // ประตูยังเปิด
{ t: 6000, set: { doorClosed: true } },
{ t: 6600, press: "reset" },
], 7000);
assert.equal(m.step, "idle");
assert.equal(m.latched.length, 0);
const early = run([{ t: 100, press: "start" }, { t: 5200, set: { doorClosed: false } }, { t: 5600, press: "reset" }], 6000);
assert.equal(early.m.step, "fault");
});
test("first-out: เก็บ trip แรก ตัวถัดมาเป็นผลตามมา", () => {
const { m } = run([
{ t: 100, press: "start" }, { t: 5300, set: { overloadOk: false } }, { t: 5500, set: { estopOk: false } },
], 6500);
assert.equal(m.firstOut?.id, "IL_OVERLOAD");
assert.deepEqual(m.latched.map((t) => t.id), ["IL_OVERLOAD", "IL_ESTOP"]);
});If you will use this for real
- Write it in a real PLC with Ladder or Structured Text, in whichever form your maintenance team can read. The structure is the same: the permissive is a check function before start, the interlock is a latching block, and the sequence is a state machine or SFC.
- Safety functions (E-stop, guard doors, light curtains) must use a safety relay or a safety PLC and be assessed to ISO 13849-1 or IEC 62061 by a competent person. Ordinary PLC logic can read the status, but should not be what cuts the power.
- Make a cause-and-effect table showing which cause commands which effect. The same document is used to review with the process engineer, to check FAT/SAT, and as the reference when someone asks for a bypass.
- Run FAT/SAT tests row by row through that table, covering both normal and abnormal cases, including pressing reset while the cause is still present, and testing with a broken signal wire.
- Send the first-out status and the list of reasons that block a start to the HMI and SCADA screens, so operators can see them without opening the program.
- Every bypass must have an approver, an expiry date and a record. It must not be a forced value in the PLC that gets forgotten.
Applying it to your business
- Mixers or agitated tanks in food, chemical or cosmetics plants: use the same structure, swap in real valves and sensors, and add temperature or pH conditions as permissives and interlocks.
- Packing lines or multi-section conveyors: the downstream section must be ready before the upstream section starts (permissive), and the upstream section stops when the downstream one goes down (interlock). Write the start and stop sequence as a single table.
- Dosing and weighing systems: fill is allowed when the scale is stable and the receiving tank is ready. If the scale value does not change within a set time, raise a timeout. This is also related to the article on weighing and dosing systems with a PLC.
Common mistakes
- Using the word interlock for every condition, so people cannot tell whether a start condition or a stop condition is meant.
- An interlock that does not latch, so the machine runs on by itself once the door is closed.
- A reset button that clears everything without checking whether the cause is still present, or an automatic reset.
- No timeout in each step, so the machine hangs silently when a sensor does not change.
- Not checking feedback from the devices that were commanded, and assuming that a command means it is running.
- Not recording the first trip, so the repairer chases symptoms instead of the cause.
- Believing that an interlock in an ordinary PLC is a safety function.
- Fixing a problem during production by skipping a condition, then not recording it or not restoring it.
Checklist
- Agree the definitions of permissive, interlock and sequence in the team, and write them in the project documents.
- Make a cause-and-effect table: cause, effect, latching, and how to reset.
- Set the safety functions apart, for a competent person to assess to ISO 13849-1 or IEC 62061.
- Every step has an entry condition, a timeout and a safe action.
- The permissive must show the list of blocking reasons on the screen.
- A tripped interlock must latch, and reset must be a deliberate action that is refused while the cause is still present.
- Check feedback from every important device that is commanded.
- Record the first-out and the time of every trip.
- Test every row of the table at FAT and SAT.
- Set bypass rules with an approver and an expiry date.
Caution: the example is a simulator for learning. All the data is assumed. The times and conditions do not come from a real machine. It carries no safety certification of any kind and is not safety advice for any specific machine. A real design must go through a risk assessment by a competent person.
References
- IEC webstore — IEC 61131-3:2013 Programmable controllers, Part 3: Programming languages (accessed 2026-11-21)
- Wikipedia — Sequential function chart (accessed 2026-11-21)
- Wikipedia — Interlock (engineering) (accessed 2026-11-21)
- IEC webstore — IEC 62061:2021 Safety of machinery, functional safety of safety-related control systems (accessed 2026-11-21)
- Wikipedia — ISO 13849 (scope, performance levels a-e) (accessed 2026-11-21)
- Wikipedia — Sequence of events recorder (accessed 2026-11-21)
- Wikipedia — Alarm management (ISA 18.2, EEMUA 191, alarm flood) (accessed 2026-11-21)
- Node.js — Running TypeScript natively (type stripping) (accessed 2026-11-21)




