Beckhoff First Scan Bit May 2026
The First Scan bit is a simple but essential mechanism in Beckhoff/TwinCAT systems to ensure deterministic, safe startup behavior. Implement it consistently, coordinate initialization ordering, protect hardware and retained data, and provide clear diagnostics to maintain reliable system startup.
In Beckhoff TwinCAT, first scan detection is achieved via the PlcTaskSystemInfo.FirstCycle
boolean, which signals the initial task execution, or by creating a custom global variable initialized to TRUE. Alternative methods include utilizing
for function block-specific initialization. These approaches enable reliable initialization of communication, variables, and logging upon system startup. AllTwinCAT First cycle - AllTwinCAT
In Beckhoff's TwinCAT environment, there isn't a single "fixed" system bit like the
found in Allen-Bradley controllers. Instead, developers typically use one of two methods to detect the first scan cycle of a PLC task: a built-in system variable or a manual "home-made" boolean flag. 1. Built-in Method: PlcTaskSystemInfo TwinCAT provides a structured data type called PlcTaskSystemInfo that contains a specific boolean member for this purpose: FirstCycle
To use it, you must retrieve the current task index and access the system info array: Function Block GETCURTASKINDEX to find which task is currently running. System Variable : Access the global array using that index. Example Implementation (Structured Text):
VAR fbGetCurTaskIndex : GETCURTASKINDEX; END_VAR
fbGetCurTaskIndex(); // Call the function block
IF _TaskInfo[fbGetCurTaskIndex.index].FirstCycle THEN // Logic here only executes on the very first scan END_IF Use code with caution. Copied to clipboard 2. Manual Method: Initialized Boolean
A common practice in IEC 61131-3 programming is to create a local or global boolean variable that defaults to and is turned at the end of the first cycle. Initialization : Declare a variable like bFirstScan with an initial value of : At the very end of your main program, set it to
: This method is portable across different PLC brands and doesn't require specific TwinCAT system function calls. Example Implementation:
VAR bFirstScan : BOOL := TRUE; // Initialized to TRUE END_VAR
IF bFirstScan THEN // Perform initialization (e.g., setting default values) END_IF
// Last line of the program bFirstScan := FALSE; Use code with caution. Copied to clipboard Why use a First Scan Bit? Initialization
: Setting initial values for variables that aren't retentive. Communication Setup
: Triggering handshake or connection logic that only needs to happen once upon startup. Safety Resets beckhoff first scan bit
: Ensuring certain outputs or states are cleared before the main logic begins. hardware initialization How to Configure the FirstScan Bit in Siemens TIA Portal
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
Here’s a concise guide to the First Scan Bit in Beckhoff TwinCAT (IEC 61131-3).
In the world of industrial automation, specifically within the Beckhoff TwinCAT environment, the "first scan bit" is a fundamental concept used to initialize logic, reset variables, or trigger one-time events when a PLC program transitions from Stop to Run mode. The First Scan bit is a simple but
Understanding how to implement and utilize this bit effectively ensures that your machines start up in a safe, predictable state every time the controller is powered on or the code is restarted. What is a First Scan Bit?
A first scan bit is a boolean flag that remains TRUE for exactly one execution cycle of the PLC task. After the first logic solve is complete, the bit drops to FALSE and stays there until the PLC is restarted.
Unlike some traditional PLCs (like Allen-Bradley’s S:FS bit) that have a predefined system variable, Beckhoff’s TwinCAT allows for several ways to achieve this functionality depending on your version and preference. Methods to Implement First Scan in TwinCAT 1. Using the TwinCAT System Info (The Pro Way)
The most robust method involves using the built-in PlcTaskSystemInfo structure. This provides real-time data about the current task. Variable: _TaskInfo[index].FirstCycle
How it works: This system variable is automatically managed by the TwinCAT runtime.
Benefit: No manual coding is required to reset the bit; it is inherently tied to the task execution. 2. Manual Logic (The Classic Way)
If you prefer a portable method that works across almost any IEC 61131-3 platform, you can create your own logic using a global variable.
Step 1: Declare a global boolean, e.g., bFirstScan : BOOL := TRUE;.
Step 2: At the very end of your MAIN routine, add: bFirstScan := FALSE;.
How it works: On startup, the variable initializes to TRUE. The logic runs once, and then the assignment at the bottom kills the bit for all subsequent cycles. 3. Using the 'Init' Attribute
TwinCAT 3 supports the attribute 'init_on_on_online_change' or specific FB_init methods. While more advanced, these are used to run initialization code specifically when a function block is instantiated or the PLC starts. Common Use Cases
🚀 Initialization of SetpointsEnsures that PID gains, speed limits, or timers start with default "safe" values rather than zeros.
🔄 Resetting State MachinesForces your Sequential Function Chart (SFC) or CASE statements to jump to the 'IDLE' or 'INIT' state regardless of where they were during the last shutdown.
📡 Communication HandshakesTriggers a "Hello" or synchronization pulse to external devices, such as HMIs or SQL databases, to signal that the PLC is back online. Best Practices and Pitfalls
Execution Order Matters: If you use a manual first scan bit, ensure it is set to FALSE at the very end of your program. If you do it at the top, the rest of your logic won't see the TRUE state.
Distributed Tasks: If your TwinCAT project has multiple tasks (e.g., a fast 1ms task and a slow 10ms task), remember that each task has its own "first cycle."
Avoid Logic Overload: Don't cram too much heavy processing into the first scan. If you have massive data to load, consider a dedicated "Initialization" state that spans multiple cycles.
⚠️ Key Reminder: Always use the first scan bit to put your machine into a Safe State. Never assume the hardware is in the same position it was when the power went out. A common practice in IEC 61131-3 programming is
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.
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.
If you need a global first scan bit that works across multiple programs and tasks, use the Tc2_System or Tc3_System library.