Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Timers, Counters and Sequences

PLC Sequences and State Machines

Learn to program machine sequences in a PLC with state machines: step variables, transitions, actions, SFC concepts, and a runnable C model of a two-cylinder cycle.

Most machines do things in order: clamp the part, drill, unclamp, eject. Each step starts when the previous one is finished and some condition holds, such as a sensor seeing the cylinder at its end position. Programming this as a pile of independent rungs gets messy fast, because every rung must know what step it belongs to. The cleaner approach is a sequence, implemented as a state machine: a variable holds the current step, and the program does exactly the work for that step.

PLC code in this lesson is read-only; the runnable examples are C stand-ins that model the same logic on a simulated machine.

The state machine idea

A state machine has three parts:

  1. States (steps). What the machine is doing now. Represented by an integer or an enumeration.
  2. Transitions. Conditions that move the machine from one state to the next.
  3. Actions. Outputs switched on while a state is active.
stateDiagram-v2
    [*] --> Idle
    Idle --> Clamp: start pressed
    Clamp --> Drill: clamp sensor on
    Drill --> Retract: drill depth reached
    Retract --> Release: drill home
    Release --> Idle: clamp released

A drilling cycle as a state diagram. Each arrow carries the condition that triggers it.

Because only one state is active at a time, outputs can never conflict, and you can read the program like the machine's story.

The CASE pattern in Structured Text

In Structured Text the usual structure is one CASE statement on a step variable (read-only example):

TEXT
CASE step OF
    0: (* Idle *)
        IF startButton THEN step := 10; END_IF;
    10: (* Clamp *)
        clampValve := TRUE;
        IF clampSensor THEN step := 20; END_IF;
    20: (* Drill *)
        drillMotor := TRUE;
        IF depthSensor THEN step := 30; END_IF;
    30: (* Retract *)
        drillMotor := FALSE;
        IF drillHome THEN step := 40; END_IF;
    40: (* Release *)
        clampValve := FALSE;
        IF NOT clampSensor THEN step := 0; END_IF;
END_CASE;

Numbers in steps of 10 leave room to insert a new step later without renumbering, the same trick this site uses for lesson order. In ladder logic the equivalent is a set of latched step bits or a compare-and-move on an integer; in IEC Sequential Function Chart (SFC), steps and transitions are drawn graphically, and the runtime handles the structure.

A runnable model

The C program simulates the cycle above, with a pretend machine whose sensors react a few scans after an output turns on. It is a stand-in: the loop is our scan cycle and the arrays play the role of I/O.

C C stand-in: a clamp-drill sequence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The control logic is only the switch. Everything above it is the simulated machine. In a real PLC, the machine side is your sensors and valves.

Adding a timer to a step

Steps often wait: "hold the clamp for 2 s before drilling". Combine the state machine with a TON timer: run the timer only while the step is active, and use its Q as the transition.

C C stand-in: a timed step and a step counter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Good habits for sequences

  • One state variable, one writer. Only the sequence code changes step. Other code reads it.
  • Outputs from states. Switch outputs inside the state (or with a step = X comparison), not in several places; otherwise two writers fight.
  • Always have an abort path. Add a transition from every state to a safe "Stopped" or "Fault" state on an emergency condition. See fault handling.
  • Add timeouts. If the cylinder never reaches its sensor, the step waits forever. Start a timer in each step and fault if it expires.
  • Show the step on the HMI. Displaying the step number makes troubleshooting far easier; see HMI basics.
  • Reset cleanly. Define what happens on power-up: usually go to Idle or a "return to home" routine, never jump into the middle of a cycle.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main advantage of a step variable over many independent rungs?

    A single current-state variable makes the active step explicit and prevents contradictory outputs.
  2. 2. Why number steps 0, 10, 20, 30 instead of 0, 1, 2, 3?

    Gaps in the numbering let you add an intermediate step (for example 15) without editing the others.
  3. 3. A step waits for a cylinder sensor that never turns on. What protects the machine?

    A watchdog timer per step turns an endless wait into a diagnosed fault.
  4. 4. In SFC, what does a transition contain?

    Steps hold actions; transitions hold the condition that must be true to leave a step.

Summary

  • A sequence is a state machine: a step variable, transition conditions and per-step actions.
  • Use a CASE statement, latched step bits, or SFC; keep one writer for the step variable.
  • Combine steps with timers and counters, add timeouts and an abort path, and define the power-up state.
  • The C programs model the logic with a simulated machine; they are stand-ins for real PLC code.

Next: HMI Basics and Screen Design