Beckhoff First Scan Bit Info

The First Scan Bit is a system-generated boolean flag that is TRUE for exactly one PLC cycle after:

After that single cycle, it automatically returns to FALSE and stays FALSE until the next restart.

In TwinCAT 2, it is typically FirstCycle or FirstScan.
In TwinCAT 3, it is available via Tc2_System function block or directly as FirstScan in some contexts.

Always use FirstScan in your main program – Never assume default values.
Set outputs to safe states first – Protect machinery and personnel.
Avoid heavy computation inside FirstScan – Keep it fast; one cycle only.
Test all restart scenarios – Power cycle, online change, cold start, warm start.
Log first scan events – Useful for debugging unexpected initializations. beckhoff first scan bit

Never do this:

IF TRUE THEN   // This will run every cycle, not just first
    Initialize();
END_IF
  • Consider versioning and CRC checks for retained data to determine if retained data should be used or reset.
  • A common pitfall:
    If you mark variables as RETAIN, they survive a warm start. But on a first scan (especially after download), you may want to override retain values.

    IF fbFirstScan.bFirstScan THEN
        (* Force reset retain values on fresh download *)
        rMotorPosition := 0.0;    // Even if retain, reset on first scan
        bRecipeLoaded := FALSE;
    END_IF
    

    For advanced TwinCAT 3 users working with Function Blocks, the most elegant and robust "first scan" is not a bit at all—it's the object constructor: FB_Init. The First Scan Bit is a system-generated boolean

    When you instantiate a function block, TwinCAT automatically calls its FB_Init method once.

    FUNCTION_BLOCK FB_DriveController
    VAR_INPUT
        bEnable : BOOL;
    END_VAR
    VAR_OUTPUT
        bReady : BOOL;
    END_VAR
    VAR
        fSpeed : REAL;
    END_VAR
    

    // This runs once when the FB is created (first scan of the FB) METHOD FB_Init : BOOL VAR_INPUT bInitRetains : BOOL; // TRUE if retain variables are restored bInCopyCode : BOOL; // TRUE if FB is copied END_VAR fSpeed := 0.0; // Initialize internal variable bReady := FALSE; END_METHOD

    Advantages:

    When to use this: In large OOP-based TwinCAT projects with many reusable function blocks.


    | 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. | After that single cycle, it automatically returns to

    During the first scan, you typically want to:

    Without a proper first scan routine, your machine might start with actuators in unpredictable positions or unfinished states from a previous run.