I Built PLC Ladder Lab: Learn Ladder Logic One Scan at a Time ⚡
Ladder logic looks simple on paper: a few contacts, a coil, two vertical rails. Then you meet your first latch or on-delay timer, and the question becomes “but what is the PLC actually doing right now?” I kept running into that while working through PLC programming for my Computer-Aided Manufacturing course at Texas Tech. Static diagrams don’t show time, and they don’t show the scan cycle. So I built a tool that does. 👉 Try it live: plc-ladder-logic.vercel.app
梯形图逻辑在纸面上看起来很简单:几个触点、一个线圈、两条垂直母线。但当你遇到第一个锁存器或接通延时定时器时,问题就变成了“PLC 现在到底在做什么?”我在德克萨斯理工大学学习计算机辅助制造课程的 PLC 编程时,一直遇到这个问题。静态图表无法展示时间,也无法展示扫描周期。所以我构建了一个能做到这些的工具。👉 在线试用:plc-ladder-logic.vercel.app
What it simulates: A small bottle-filling station, the classic first automation project: A conveyor moves empty bottles. Belt motor → Q0.0. A photo sensor sees a bottle under the nozzle. Sensor → I0.0. A solenoid valve fills it for 3 seconds. Valve → Q0.1, timed by a TON. A memory bit remembers the bottle is full, so the belt can move it away. Full → M0.0. Addresses follow Siemens S7-1200 style, and the symbols are standard IEC 61131-3.
它模拟的内容:一个小型灌装站,这是经典的入门级自动化项目:传送带移动空瓶。传送带电机 → Q0.0。光电传感器检测喷嘴下方的瓶子。传感器 → I0.0。电磁阀进行 3 秒灌装。阀门 → Q0.1,由 TON 定时器控制。一个存储位记录瓶子已满,以便传送带将其移走。满瓶 → M0.0。地址遵循西门子 S7-1200 风格,符号符合 IEC 61131-3 标准。
What you can do with it: ▶ Watch the machine and the ladder together. The machine view (tank, valve, sensor beam, motor) runs right next to the live ladder diagram. Any wire with power on it glows green, so you can see exactly why the belt stops when a bottle breaks the beam. ▶ Step through every PLC scan. This is the part I wanted most. Switch to Scan by scan and time freezes. Each click advances one phase: Read inputs → Rung 1 → Rung 2 → Rung 3 → Rung 4 → Write outputs. An I/O panel shows the wire value next to the process image value. Block the sensor halfway through a scan and you’ll see the program ignore it until the next Read inputs. That idea is hard to explain with a diagram alone.
你可以用它做什么:▶ 同时观察机器和梯形图。机器视图(储罐、阀门、传感器光束、电机)就在实时梯形图旁边运行。任何通电的线路都会发出绿光,因此你可以清楚地看到当瓶子阻断光束时传送带停止的确切原因。▶ 逐步执行每个 PLC 扫描。这是我最想要的功能。切换到“逐次扫描”模式,时间就会冻结。每次点击推进一个阶段:读取输入 → 梯级 1 → 梯级 2 → 梯级 3 → 梯级 4 → 写入输出。I/O 面板在过程映像值旁边显示线路值。在扫描过程中途阻断传感器,你会看到程序直到下一次“读取输入”才会响应。这个概念仅靠图表很难解释。
▶ See the TON timer and the Set/Reset latch work. The timer gets its own dial with IN and Q lamps and a simple state flow: Idle → Timing → Done. A signal trend plots I0.0, Q0.0, Q0.1, ET, TON.Q and M0.0 over the last 15 seconds, so you can see TON.Q pulse for a single scan while M0.0 stays latched. ▶ Learn in small steps. A built-in lesson walks through each stage of the cycle, and every step ends with a “Think first” question, such as: Why not just put NC TON.Q on the valve rung instead of using a latch? (Short answer: the valve closes, the timer resets, Q drops, and the valve reopens. It chatters. You need memory.) Click any contact, coil or timer for a plain-English explanation based on its live value.
▶ 观察 TON 定时器和置位/复位锁存器的工作过程。定时器有自己的刻度盘,配有 IN 和 Q 指示灯,以及简单的状态流:空闲 → 计时中 → 完成。信号趋势图绘制了过去 15 秒内 I0.0、Q0.0、Q0.1、ET、TON.Q 和 M0.0 的变化,因此你可以看到 TON.Q 在单次扫描中脉冲,而 M0.0 保持锁存状态。▶ 分步学习。内置课程引导你了解周期的每个阶段,每一步都以“先思考”问题结束,例如:为什么不在阀门梯级上直接放一个常闭的 TON.Q,而是使用锁存器?(简短回答:阀门关闭,定时器重置,Q 下降,阀门重新打开。这会导致抖动。你需要存储器。)点击任何触点、线圈或定时器,即可根据其当前值获得通俗易懂的解释。
The program: The default Auto cycle program is four rungs: R1 Q0.0 = NOT I0.0 OR M0.0 (run belt, or bypass when full). R2 Q0.1 = I0.0 AND NOT Q0.0 AND NOT M0.0 (fill only if bottle present, belt stopped, not full). R3 TON(IN = Q0.1, PT = 3 s) → SET M0.0 (latch “Full” after 3 s). R4 NOT I0.0 → RESET M0.0 (clear “Full” once the bottle leaves). The app shows each rung as a Boolean equation with live values substituted in, for example Q0.0 = (NOT 1 OR 0) = 0. If you prefer equations to diagrams, that view makes the ladder click. Two simpler programs build up to it: Stop at sensor (rung 1 only) and Fill 3 s (valve + timer, but no way to restart, which is the point).
程序:默认的自动循环程序包含四个梯级:R1 Q0.0 = NOT I0.0 OR M0.0(运行传送带,或在满瓶时旁路)。R2 Q0.1 = I0.0 AND NOT Q0.0 AND NOT M0.0(仅在有瓶子、传送带停止且未满时灌装)。R3 TON(IN = Q0.1, PT = 3 s) → SET M0.0(3 秒后锁存“满瓶”)。R4 NOT I0.0 → RESET M0.0(瓶子离开后清除“满瓶”)。该应用将每个梯级显示为代入实时值的布尔方程,例如 Q0.0 = (NOT 1 OR 0) = 0。如果你更喜欢方程而不是图表,这种视图能让你更容易理解梯形图。在此基础上还有两个更简单的程序:在传感器处停止(仅梯级 1)和 3 秒灌装(阀门 + 定时器,但无法重启,这正是重点所在)。
How it works under the hood: It’s a single HTML file with no frameworks, no build step and no dependencies. Plain JavaScript, inline SVG, and requestAnimationFrame. The key design choice: the same data structure draws the ladder and runs the logic. Each rung is a list of elements, so the diagram and the behavior can never disagree. A simplified version of the rung solver: function evalRung(rung, dt) { let power = 1; // left rail is always energized for (const el of rung.series) { if (el.kind === ‘TON’) { timer(power, dt); // IN = power reaching the box power = state.tq; // Q carries power onward } else { const bit = state.img[el.tag]; // read from the process image const closed = el.kind === ‘NC’ ? !bit : bit; power = power && closed; } } const out = rung.out; if (out.kind === ‘Q’) state.img[out.tag] = power; if (out.kind === ‘S’ && power) state.img[out.tag] = 1; if (out.kind === ‘R’ && power) state.img[out.tag] = 0; }
底层原理:这是一个单一的 HTML 文件,没有框架、构建步骤或依赖项。使用纯 JavaScript、内联 SVG 和 requestAnimationFrame。关键的设计选择是:相同的数据结构既用于绘制梯形图,也用于运行逻辑。每个梯级都是一个元素列表,因此图表和行为永远不会产生冲突。梯级求解器的简化版本如下:function evalRung(rung, dt) { let power = 1; // 左侧母线始终通电 for (const el of rung.series) { if (el.kind === ‘TON’) { timer(power, dt); // IN = 到达盒子的功率 power = state.tq; // Q 将功率向后传递 } else { const bit = state.img[el.tag]; // 从过程映像读取 const closed = el.kind === ‘NC’ ? !bit : bit; power = power && closed; } } const out = rung.out; if (out.kind === ‘Q’) state.img[out.tag] = power; if (out.kind === ‘S’ && power) state.img[out.tag] = 1; if (out.kind === ‘R’ && power) state.img[out.tag] = 0; }
And one scan is exactly what a real PLC does: function scan(dt) { state.img.I0 = sensorWire(); // 1. read inputs into the image program.forEach(r => evalRung(r, dt)); // 2. solve rungs top to bottom motor = state.img.Q0; // 3. write outputs to the field valve = state.img.Q1; } Because rungs are solved in order, the simulator reproduces real behavior, like the valve staying open for one extra scan after M0.0 latches: rung 2 was solved before rung 3 set the bit. In Scan by scan mode you can watch that happen. Try it 🔗 plc-ladder-logic.vercel.app It runs in any modern browser and fits on one screen. Keyboard shortcuts: Space play/pause, N next scan step, ← → lesson steps. If you teach or are learning PLCs, I’d love your feedback. What should come next: counters (CTU/CTD), a reject station with a second sensor, or exporting the ladder to structured text? Drop a comment or open an issue on GitHub. Thanks for reading!
单次扫描的过程与真实 PLC 完全一致:function scan(dt) { state.img.I0 = sensorWire(); // 1. 读取输入到映像 program.forEach(r => evalRung(r, dt)); // 2. 从上到下求解梯级 motor = state.img.Q0; // 3. 将输出写入现场 valve = state.img.Q1; } 由于梯级是按顺序求解的,模拟器重现了真实行为,例如在 M0.0 锁存后阀门多保持开启一次扫描:梯级 2 在梯级 3 置位之前就已经求解了。在“逐次扫描”模式下,你可以观察到这一过程。试用链接 🔗 plc-ladder-logic.vercel.app。它可以在任何现代浏览器中运行,并适配单屏显示。键盘快捷键:空格键播放/暂停,N 键下一步扫描,← → 键切换课程步骤。如果你正在教授或学习 PLC,我很期待你的反馈。接下来应该做什么:计数器 (CTU/CTD)、带第二个传感器的剔除站,还是将梯形图导出为结构化文本?请在 GitHub 上发表评论或提交 Issue。感谢阅读!