Simple Examples and Patterns
Reusable Structured Text patterns and worked examples in rungs.dev — motor start/stop, latching, edge detection, scaling, and other common PLC programming tasks.
This section provides simple, reusable logic patterns for common PLC programming tasks in Structured Text (ST).
1. Basic Motor Start/Stop Logic
// Start motor if Start button is pressed and Stop is not
IF Start_Button AND NOT Stop_Button THEN
Motor_Run := 1;
ELSE
Motor_Run := 0;
END_IF;Or, simplified:
Motor_Run := Start_Button AND NOT Stop_Button;2. Latched Motor Control (Set/Reset Pattern)
// Latch motor ON when Start is pressed
IF Start_Button THEN
Motor_Latch := 1;
END_IF;
// Unlatch motor when Stop is pressed
IF Stop_Button THEN
Motor_Latch := 0;
END_IF;
// Run motor based on latch state
Motor_Run := Motor_Latch;3. Analog Threshold Control
// Turn on fan when temperature exceeds 60.5°C
IF Temperature > 60.5 THEN
Fan := 1;
ELSE
Fan := 0;
END_IF;4. Mode Selector with CASE
CASE Mode OF
1: Operation := 'Auto';
2: Operation := 'Manual';
3: Operation := 'Maintenance';
ELSE
Operation := 'Unknown';
END_CASE;Practice
Try a timing pattern in the Flasher exercise — blink an output one second on, one second off while enabled.
OSRI / OSFI — One Shot Rising and Falling with Input
OSRI sets OutputBit for one scan when InputBit turns on; OSFI sets it for one scan when InputBit turns off. Both use the FBD_ONESHOT structure in rungs.dev Structured Text.
The Scan Cycle
How a PLC runs your program — the read, evaluate, write loop called the scan — and why it applies the same way to Ladder Logic and Structured Text.