Your First CLICK PLC Program
Write a motor start/stop circuit, test it, map it to CLICK hardware addresses, export ladder CSV, and put it into CLICK with ClickNick.
The program
This is a sealed motor circuit. Start latches the motor on. Stop resets it off. A speed input copies through only while the motor runs.
from pyrung import Bool, Real, PLC, Program, rung, copy, latch, reset, rise
from pyrung.click import ClickBlocks, TagMap
x, y, c, t, ct, sc, ds, dd, dh, df, xd, yd, xd0u, yd0u, td, ctd, sd, txt = ClickBlocks()
# Semantic tags — no hardware addresses yet
StartButton = Bool()
StopButton = Bool()
MotorRunning = Bool()
Speed = Real()
DisplaySpeed = Real()
with Program() as logic:
with rung(rise(StartButton)):
latch(MotorRunning)
with rung(rise(StopButton)):
reset(MotorRunning)
with rung(MotorRunning):
copy(Speed, DisplaySpeed)
rise() triggers on the rising edge. It gives a one-scan pulse when the button goes from off to on. Without it, holding Start would re-latch every scan (harmless here, but wrong for counting or toggling).
Test it
def test_motor_start_stop():
with PLC(logic, dt=0.1) as plc:
# Start the motor
StartButton.value = True
plc.step()
assert MotorRunning.value is True
# Release button — motor stays latched
StartButton.value = False
plc.run(cycles=5)
assert MotorRunning.value is True
# Stop the motor
StopButton.value = True
plc.step()
assert MotorRunning.value is False
# Speed only copies while running
StopButton.value = False
StartButton.value = True
Speed.value = 75.0
plc.step()
assert DisplaySpeed.value == 75.0
StartButton.value = False
StopButton.value = True
plc.step()
Speed.value = 99.0
plc.step()
assert DisplaySpeed.value == 75.0 # Didn't update — motor is off
Same logic, deterministic timing, real assertions. Run with pytest.
Map to CLICK hardware
Once the logic is correct, map semantic tags to CLICK memory addresses:
mapping = TagMap({
StartButton: x[1], # X001 — discrete input
StopButton: x[2], # X002
MotorRunning: y[1], # Y001 — discrete output
Speed: df[1], # DF1 — float register (analog input)
DisplaySpeed: df[11], # DF11
})
Validate that the program fits CLICK's constraints:
The validator checks type compatibility, instruction support, and addressing limits. Fix any findings, then tighten to mode="strict" when the program is clean.
Export ladder CSV
from pyrung.click import pyrung_to_ladder
bundle = pyrung_to_ladder(logic, mapping)
bundle.write("./output") # writes main.csv + subroutines/*.csv
This produces deterministic CLICK ladder CSV files ready for import.
Put the rungs into CLICK
To put the rungs in ./output into CLICK, use ClickNick's Review Changes. See ClickNick's help.
Next steps
- CLICK PLC Dialect — pre-built blocks, TagMap details, validation findings, nickname CSV I/O
- CLICK Python Codegen — imports CLICK ladder CSV back into pyrung
- Testing Guide — forces, time travel, forking, pytest patterns
- ClickNick — the companion tool for CLICK clipboard I/O and nickname management