Beckhoff First Scan Bit -

Some hardware modules (e.g., high-speed counters, PWM generators) need a setup block executed exactly once.

IF FirstScan THEN
    // Configure encoder input
    Encoder_SetMode(ENC_MODE_QUADRATURE);
    Encoder_SetResolution(4096);
END_IF

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

In TwinCAT, the PROGRAM or FUNCTION_BLOCK structure has a specific order of execution. The VAR section declares variables, but it doesn't execute logic. To run logic on the first scan, you declare a boolean flag and check its state.

The Code:

PROGRAM MAIN
VAR
    bFirstScan : BOOL := TRUE; (* Initialize as TRUE *)
    nCounter   : INT;
END_VAR

(* Logic Section ) IF bFirstScan THEN ( --- This runs ONLY on the first scan --- ) nCounter := 0; ( Reset variables ) ( Perform other startup routines *)

bFirstScan := FALSE;    (* Set to false so this never runs again *)

END_IF

(* Your regular cyclic code runs here *) nCounter := nCounter + 1;

Add the TwinCAT_SystemInfoVarList to your project, then: beckhoff first scan bit

IF TwinCAT_SystemInfoVarList._FirstScan THEN
    // One-time actions
END_IF

| 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. |

The Beckhoff “first scan” bit is a small but important concept in TwinCAT PLC programming that helps ensure deterministic, safe, and predictable behavior during controller startup. While its implementation is straightforward, understanding its purpose and correct use is essential for robust automation systems.

What the first scan bit is

Why it matters

Typical uses and patterns

Implementation examples (conceptual)

  • end_if
  • One-shot initialization with persistent flag:
  • end_if
  • Best practices

    Version and platform considerations

    Conclusion The Beckhoff first scan bit is a simple but essential tool for controlled startup in TwinCAT PLC programs. Properly used, it ensures variables and outputs are initialized predictably, prevents unsafe transient actions, and supports reliable commissioning and diagnostics. Adhering to concise, documented first-scan patterns and combining them with broader safety practices produces safer, more maintainable control software.

    In Beckhoff TwinCAT (2 and 3), there is no single "magic" global bit like the S:FS in Allen-Bradley . Instead, you can access the "First Scan" status through built-in system variables or by creating a custom initialization flag. 1. Using Built-in System Info (FirstCycle) Some hardware modules (e

    The most reliable way to detect the first scan in TwinCAT 3 is to use the PlcTaskSystemInfo structure . Every task has an associated system structure that includes a FirstCycle boolean . Example Implementation (Structured Text):

    VAR fbGetCurTaskIdx : GETCURTASKINDEX; // Function block to get current task index bFirstScan : BOOL; END_VAR fbGetCurTaskIdx(); // Call the FB to refresh current task info bFirstScan := _TaskInfo[fbGetCurTaskIdx.index].FirstCycle; IF bFirstScan THEN // Logic here only runs on the very first PLC scan // e.g., Initializing setpoints or resetting state machines END_IF Use code with caution. Copied to clipboard 2. Manual Global Variable Flag

    A common "best practice" for portability across different PLC brands is to create your own flag in a Global Variable List (GVL) . Declare it with an initial value:

    VAR_GLOBAL bIsFirstScan : BOOL := TRUE; // Starts TRUE when the PLC runtime begins END_VAR Use code with caution. Copied to clipboard

    Reset it at the end of your main program:Place this line at the very bottom of your MAIN program or the last task to execute :

    // Main logic uses bIsFirstScan... // Final line of code: bIsFirstScan := FALSE; Use code with caution. Copied to clipboard 3. SFC Initialization Flag

    If you are using Sequential Function Chart (SFC) programming, TwinCAT provides implicit variables called SFC Flags .

    SFCInit: When set to TRUE, the sequence is reset to the initial step .

    SFCReset: Resets the sequence and continues processing from the initial step . Key Usage Considerations

    Cold vs. Warm Restart: The FirstCycle bit typically triggers when the Runtime starts . Simple PLC "Stop" and "Start" commands in the IDE might not always reset this bit unless the entire TwinCAT system is restarted or a new configuration is activated . Physical outputs (via PA_ or AT%Q* ) often

    Multiple Tasks: If your project has multiple tasks (e.g., a fast 1ms task and a slow 100ms task), each task has its own FirstCycle flag. Ensure you are checking the flag for the specific task where your initialization logic resides . RSLogix 5000 First Scan Bit (S:FS) Programming Guide

    PROGRAM SafeStartup
    VAR
        fbFirstScan : FB_FirstScan;
        bStartupComplete : BOOL;
        bResetDrives : BOOL;
        tStartupTimer : TON;
    END_VAR
    

    fbFirstScan();

    IF fbFirstScan.bFirstScan THEN // First cycle only bResetDrives := TRUE; bStartupComplete := FALSE; tStartupTimer(IN:=FALSE); END_IF

    // Drive reset pulse IF bResetDrives THEN // Pulse drive reset signal bResetDrives := FALSE; END_IF

    // Startup timer after first scan IF NOT fbFirstScan.bFirstScan AND NOT bStartupComplete THEN tStartupTimer(IN:=TRUE, PT:=T#2S); IF tStartupTimer.Q THEN bStartupComplete := TRUE; // Enable normal operation END_IF END_IF


    The Beckhoff First Scan bit is a small, easily overlooked tool that separates professional, robust PLC code from fragile, “works-most-of-the-time” logic. By taking explicit control of the first cycle, you eliminate startup surprises, protect hardware, and ensure your TwinCAT application starts every time in a predictable, safe state.

    Next time you write a new PLC program, ask yourself: “If this were a cold start right now, would my system behave safely?” If the answer isn’t an immediate “yes,” you need FirstScan.


    Have a tricky Beckhoff initialization issue? Share your experience in the comments or contact your local Beckhoff automation partner.