Beckhoff First Scan Bit May 2026
Physical outputs (via PA_ or AT%Q*) often keep their previous value through a restart unless explicitly cleared.
IF FirstScan THEN // Force all digital outputs OFF myDigitalOutput := FALSE;// Set analog output to 0V/0mA myAnalogOutput := 0; // Disable drives driveEnable := FALSE;
END_IF
The First Scan Bit is a Boolean flag that is TRUE only for a single PLC cycle immediately after the runtime system starts executing the application. On the second cycle, it becomes FALSE and remains FALSE until the next controller reboot or program download. beckhoff first scan bit
Software-driven flag (common)
Edge-detected approach
Supervisor/Task-based initialization
Add the TwinCAT_SystemInfoVarList to your project, then:
IF TwinCAT_SystemInfoVarList._FirstScan THEN
// One-time actions
END_IF
The "First Scan" bit in Beckhoff TwinCAT PLC systems is a boolean status flag indicating the PLC program's initial execution cycle after startup, download, or reset. It enables safe, deterministic initialization of variables, hardware states, and communication interfaces before normal cyclic operation proceeds.
| Mistake | Consequence | Fix |
|---------|-------------|-----|
| Using FirstScan inside a function block | The FB’s FirstScan is local to that instance – may trigger multiple times. | Use a global g_FirstScanDone flag in the main PLC cycle. |
| Assuming FirstScan runs before I/O update | I/O is typically updated before the first PLC cycle, so outputs may glitch. | Explicitly write safe values in FirstScan even if I/O was read. |
| Forgetting FirstScan in interrupt tasks | Fast tasks or alarms may execute before MAIN’s FirstScan clears. | Add FirstScan logic to every task, or use a global semaphore. |
| Relying on FirstScan after online change | Online change (warm restart) also triggers FirstScan. | If you need cold start only, check bInit_Cold in SysLibCallback. | Consider versioning and CRC checks for retained data
VAR RETAIN bInitialized : BOOL; END_VAR VAR bFirstScanSys : BOOL; END_VARbFirstScanSys := TwinCAT_SystemInfoVarList._FirstScan;
IF bFirstScanSys AND NOT bInitialized THEN // Run once in device lifetime bInitialized := TRUE; END_IF