Beckhoff First Scan Bit
In the world of industrial automation, a clean start is everything. When a Beckhoff PLC (Programmable Logic Controller) boots up—whether from a power cycle, a download of a new TwinCAT configuration, or a manual restart—the system enters a critical, fleeting state. Variables are uninitialized, previous runtime values linger in memory, and physical outputs may hold their last state. How do you reliably distinguish this “first scan” from normal cyclic operation?
The answer lies in a small, single-purpose variable: FirstScan (or bInit in older TwinCAT 2 nomenclature). Though it appears only once per startup, its impact on system reliability is profound.
Network interfaces, serial buffers, or fieldbus master configurations may contain stale data after a restart.
IF FirstScan THEN // Clear receive/transmit buffers receiveBuffer := ''; sendBuffer := '';// Reset bus coupler ETC_ClearDeviceState();
END_IF
In Beckhoff’s TwinCAT environment, the First Scan Bit is a fundamental diagnostic tool used to initialize logic, reset variables, or trigger specific startup sequences the moment the PLC transitions from Config/Stop
Unlike a physical switch, this "bit" is a logical pulse that remains TRUE for exactly one task cycle. The Role of Initialization
When a PLC starts, variables often default to zero or their last persisted state. However, many industrial systems require a specific "safe state" or initial configuration before the main control loop takes over. The first scan bit acts as a system trigger , allowing programmers to: Set Initial Values:
Assign setpoints to PID controllers or load recipe parameters from memory. Reset Faults:
Clear old error flags that might have occurred during the previous shutdown. Establish Communication:
Trigger handshakes with external hardware or fieldbus devices. Implementation in TwinCAT 3
Unlike some legacy PLCs that have a dedicated global address (like S7’s
), Beckhoff provides this functionality primarily through the PLC System Information PLC_STARTUP In a typical TwinCAT project, developers often use the PlcTaskSystemInfo structure. By accessing the bFirstCycle
property, the code can detect if it is running for the first time. For example, in Structured Text (ST):
IF _TaskInfo[1].bFirstCycle THEN // Logic here only runs once InitialSetpoint := 50.0; SystemReady := FALSE; END_IF Use code with caution. Copied to clipboard Best Practices and Pitfalls The primary risk with first scan logic is dependency loops
. If multiple Function Blocks rely on the first scan bit to initialize, the order of execution matters. Developers must ensure that hardware I/O is actually "Ready" before the first scan logic attempts to write to it. beckhoff first scan bit
Furthermore, because TwinCAT is based on PC architecture, a "Warm Start" versus a "Cold Start" can behave differently regarding Persistent and Retain variables
. The first scan bit should typically be used to compliment these memory features, not fight against them. Conclusion
The First Scan Bit is the "ignition switch" of a TwinCAT program. By isolating startup logic into this single-cycle window, engineers ensure that the system begins its operation from a known, predictable state
, which is the cornerstone of both safety and reliability in automation. Should I show you how to map the System Info variables so you can use this bit in your own project?
In Beckhoff TwinCAT, there is no single pre-defined global "First Scan" variable like the
bit in Allen-Bradley systems. Instead, you must either access task-specific system information or create a custom flag. Method 1: Using Built-in System Info (Recommended) TwinCAT provides task-specific diagnostics through the array. This contains a FirstCycle boolean that is automatically set to for exactly one scan when the PLC starts or restarts. Structured Text Example:
PROGRAM MAIN VAR fbGetCurTaskIndex : GETCURTASKINDEX; // Function block to find current task index END_VAR
fbGetCurTaskIndex(); // Call the FB to refresh the index
IF _TaskInfo[fbGetCurTaskIndex.index].FirstCycle THEN // Place initialization logic here // Example: Resetting counters or setting default setpoints END_IF Use code with caution. Copied to clipboard : This method utilizes the PlcTaskSystemInfo data structure available in the TwinCAT system.
: Highly accurate; resets every time the PLC transitions from STOP to RUN or after a power cycle. Method 2: Manual Flag (The "Standard" Way)
A common practice among developers is to manually declare a global variable that initializes as and is immediately set to after use. Structured Text Example:
PROGRAM MAIN VAR bFirstScan : BOOL := TRUE; // Initializes to TRUE on startup END_VAR
IF bFirstScan THEN // Perform one-time tasks bFirstScan := FALSE; // Permanent off for the remainder of runtime END_IF Use code with caution. Copied to clipboard
: Simplest to implement and easy to read for engineers coming from other platforms.
: May not behave identically to system flags during certain "Online Change" scenarios depending on how variables are re-initialized. AllTwinCAT Comparison of Methods _TaskInfo.FirstCycle Manual Flag ( BOOL := TRUE Setup Complexity Moderate (Requires FB call) Reliability High (System-managed) Medium (Dependent on initialization) Task Awareness Specific to the calling task Global or Local depending on declaration Standard Usage Professional/Library level General Application level Use Cases for First Scan Bits Initialization
: Setting initial values for PID loops or communication buffers. Resetting Sequences : Ensuring SFC (Sequential Function Chart) sequences start at the initial step. Communication Setup
: Triggering initial requests for external fieldbus devices like EtherNet/IP Beckhoff Information System Function Block Diagram
In the world of Beckhoff TwinCAT and industrial automation, the "First Scan Bit" is a fundamental tool for ensuring your PLC starts in a predictable, safe state. If you’ve ever worked with Siemens (where it’s a system bit like FirstScan) or Allen-Bradley (using the S:FS bit), you know how vital this is.
In Beckhoff’s TwinCAT 3 environment, there isn’t a single hard-coded bit in the global memory by default, but the system provides a specialized mechanism to create one that is far more powerful than a simple boolean. What is the First Scan Bit?
The First Scan Bit is a flag that is TRUE for exactly one PLC cycle when the controller moves from "Config" or "Stop" mode into "Run" mode. After that first execution of the logic, the bit turns FALSE and remains so until the PLC is restarted or the code is re-downloaded. Why Do You Need It?
Without a initialization bit, your PLC logic simply resumes from its last state or starts with default values that might not be appropriate for a running machine. Common use cases include:
Initializing Setpoints: Setting default temperatures, speeds, or timers.
Resetting State Machines: Ensuring your sequences (SFC) start at "Step 0."
Clearing Alarms: Wiping the slate clean on startup so old errors don't prevent a start.
Communication Handshakes: Establishing a "heartbeat" or initial connection status with HMIs or third-party devices. How to Implement "First Scan" in TwinCAT 3 There are two primary ways to handle this in Beckhoff. 1. The Manual Method (Most Common)
Most TwinCAT developers create a global boolean variable and set it to TRUE by default. At the very end of their main program, they set it to FALSE. Variable Declaration (GVL): VAR_GLOBAL bFirstScan : BOOL := TRUE; END_VAR Use code with caution. Main Logic (MAIN PRG):
IF bFirstScan THEN // Perform Initialization Tasks here iTargetVelocity := 1500; bMachineReady := FALSE; END_IF // All other machine logic goes here... // The very last line of the program: bFirstScan := FALSE; Use code with caution. 2. Using FB_GetCurTaskIndex (The Pro Method)
TwinCAT provides internal system information via the Tc2_System library. You can check if the current cycle is the very first one by looking at the system task info. In the world of industrial automation, a clean
VAR fbGetTaskIndex : FB_GetCurTaskIndex; nCycleCount : UDINT; END_VAR fbGetTaskIndex(); nCycleCount := _TaskInfo[fbGetTaskIndex.index].CycleCount; IF nCycleCount = 1 THEN // This is the first scan END_IF Use code with caution.
Note: This method is more robust because it relies on the system's own cycle counter rather than a variable you might accidentally overwrite elsewhere. Best Practices
Placement Matters: If you use the manual variable method, ensure the line bFirstScan := FALSE; is at the very bottom of your MAIN task. If you put it in a sub-function, other parts of your program might miss the "True" state.
Avoid "Retain" Variables: Never make your First Scan bit a RETAIN or PERSISTENT variable. It needs to reset every time the PLC power cycles.
Safety First: Use the first scan to ensure all physical outputs are in a "Safe/Off" state before the logic takes over.
The Beckhoff First Scan bit is your "clean slate" button. Whether you use a simple boolean flag or the system's cycle counter, implementing this ensures that your machine starts up with the correct parameters every time, preventing "ghost" data from causing erratic behavior during commissioning.
Here’s a technical feature article exploring the First Scan Bit in Beckhoff TwinCAT systems.
PROGRAM MAIN VAR bFirstScan : BOOL := TRUE; // Initialize as TRUE bFirstScanDone: BOOL := FALSE; END_VAR// First scan detection IF bFirstScan AND NOT bFirstScanDone THEN bFirstScanDone := TRUE; // First scan logic here END_IF
// Reset the flag after first cycle IF bFirstScanDone THEN bFirstScan := FALSE; END_IF
⚠️ Caveat: This method can fail if the PLC is stopped/started without power cycle. Always prefer the system library method.
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.
| Brand | First Scan Mechanism | Beckhoff Equivalent |
|--------|----------------------|----------------------|
| Siemens | OB100 | INIT section or FB_FirstScan |
| Rockwell | S:FS (First Scan) bit | bInit variable manually set |
| Codesys | bInit in PLC_PRG | Same (TwinCAT is based on Codesys) |
| Beckhoff Native | INIT, FB_Init, or F_TRIG | Choose based on scope |
Key difference: Beckhoff's system does not have a mandatory, automatic FirstScan system bit. You must explicitly create it. This gives you more control but requires discipline. END_IF