DEV Community

Cover image for I Built PLC Ladder Lab: Learn Ladder Logic One Scan at a Time ⚡
Sams Zubayer Mridha
Sams Zubayer Mridha

Posted on AI-assisted

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

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.

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
Enter fullscreen mode Exit fullscreen mode

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.

▶ 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.

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)
Enter fullscreen mode Exit fullscreen mode

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).

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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! 🏭

Top comments (0)