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
The Beckhoff "First Scan Bit" refers to a signal used in TwinCAT PLC programming to execute initialization logic exactly once when the controller starts or enters run mode. Unlike some other PLC platforms that have a fixed system bit like Allen-Bradley's S:FS, Beckhoff TwinCAT provides this functionality through specific system variables or custom logic. Standard Implementation Methods
There are two primary ways to access or create a "First Scan" signal in Beckhoff TwinCAT:
System Info Variable (PlcTaskSystemInfo):The most reliable built-in method in TwinCAT 3 is using the FirstCycle boolean found within the PlcTaskSystemInfo structure.
How to use it: Access the global array _TaskInfo[index] using the GETCURTASKINDEX function block to retrieve the current task's index. Snippet Example:
IF _TaskInfo[fbGetCurTaskIndex.index].FirstCycle THEN // Your initialization code here END_IF Use code with caution. Copied to clipboard
Manual Bit Creation:A common "homemade" solution involves declaring a boolean variable with a default value of TRUE.
Logic: Place code at the very end of your main program that sets this bit to FALSE. Because the variable is initialized to TRUE, it remains so for the entire first scan before being permanently toggled off. Comparison and Review PlcTaskSystemInfo.FirstCycle Manual Custom Bit Reliability Native to TwinCAT; handles task-specific restarts. Highly reliable if implemented at the program's end. Complexity Requires calling GETCURTASKINDEX. Extremely simple to declare and use. Best Use Case
Large projects with multiple tasks or complex initialization. Quick, single-task projects or basic logic. Summary of Benefits beckhoff first scan bit
Initialization: Essential for setting default values for retentive memory variables.
State Reset: Ensures equipment begins in a safe, known state.
Task Specificity: Allows initialization to be tied to the specific timing of individual tasks rather than just global power-up. RSLogix 5000 First Scan Bit (S:FS) Programming Guide
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)
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 .
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
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 In Beckhoff TwinCAT, there is no single pre-defined
), 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.
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, the First Scan Bit is a system flag used to execute logic exactly once when the PLC transitions from Config/Stop to Run mode. It is essential for initializing variables, resetting timers, or triggering one-time communication handshakes. How to Access the First Scan Bit
TwinCAT provides a built-in system variable for this purpose within the PlcAppSystemInfo structure. You do not need to create a global variable manually; you can access it via the TwinCAT_SystemInfoVarList. Variable Name: _AppInfo.bFirstCycle Data Type: BOOL
Behavior: This bit is TRUE during the very first execution cycle of the PLC task and automatically switches to FALSE for all subsequent cycles. Implementation Example (ST)
You can use this bit in Structured Text to set default values at startup:
IF _AppInfo.bFirstCycle THEN // Initializing setpoints fTargetTemperature := 22.5; bSystemReady := FALSE; // Resetting operational counters nCycleCounter := 0; END_IF Use code with caution. Copied to clipboard Key Use Cases
Variable Initialization: Setting non-persistent variables to a known starting state.
Pulse Triggers: Triggering a TP (Timer Pulse) or R_TRIG that needs to fire immediately upon startup.
Communication Reset: Sending a "Reset" or "Init" command to external devices (like drives or Vision systems) over EtherCAT.
Loading Recipes: Reading a default parameter set from a file or database during the first task execution. Important Considerations
Multiple Tasks: If your project has multiple PLC tasks, _AppInfo.bFirstCycle is local to the context of the task it is called in.
Persistent Variables: Do not use the first scan bit to overwrite PERSISTENT or RETAIN variables unless you intentionally want to ignore their saved values upon every reboot.
Manual Implementation: If you prefer not to use the system global, you can create a local "Init" flag:
IF NOT bInitDone THEN // Do startup logic here bInitDone := TRUE; END_IF Use code with caution. Copied to clipboard
In Beckhoff TwinCAT systems, there is no single global "S:FS" bit like those found in Rockwell (Allen-Bradley) controllers . Instead, users typically leverage the PlcTaskSystemInfo : Highly accurate; resets every time the PLC
structure or create a custom initialization variable to manage first-scan logic. Beckhoff Information System Key Ways to Implement a First Scan Bit
There are two primary methods used in TwinCAT to achieve "first scan" functionality: System Variable Method : The most robust way is using the FirstCycle member of the PlcTaskSystemInfo structure. How it works : Every PLC task has a system variable which contains a boolean FirstCycle . This bit is only during the very first cycle of that specific task.
: It is built into the runtime and is highly reliable for initializing state machines or variables. Implementation Example
IF _TaskInfo[GETCURTASKINDEX()].FirstCycle THEN // Your initialization logic here END_IF; Use code with caution. Copied to clipboard Custom Variable Method
: You can manually create a non-retentive boolean variable initialized as How it works : Define a with an initial value of
. At the very end of your main program, set this variable to
. It will then act as a first-scan bit for every subsequent cycle. : Simple to understand and portable across different IEC 61131-3 platforms. Beckhoff Information System User Experience and "Reviews" Behavioral Quirks : Some users have noted that the FirstCycle
bit may only trigger when the TwinCAT runtime is fully restarted, rather than just stopping and starting the PLC code execution via the developer interface. Initialization Importance
: In industrial settings, a first-scan bit is considered essential for resetting retentive memory and ensuring equipment starts in a safe, predictable state. Alternative for Advanced Users
: For complex setups, some developers prefer using a dedicated Initialization (INIT) block
or state machine that runs once before the main cyclic logic begins. DigiKey TechForum
For official documentation on these system variables, you can refer to the Beckhoff Information System Are you looking to initialize specific variables or are you migrating logic from another PLC platform RSLogix 5000 First Scan Bit (S:FS) Programming Guide
Here’s a concise guide to the First Scan Bit in Beckhoff TwinCAT (IEC 61131-3).
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
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
It’s a system flag that is TRUE only during the first PLC cycle after:
It automatically resets to FALSE after that first scan.
The Beckhoff First Scan Bit is a simple but powerful tool for safe PLC initialization. Always use the system library version (FB_FirstScan), and remember: first scan ≠ warm start. Use it to enforce a clean startup state, especially after program downloads or power cycles.
Have you encountered any unexpected behavior with first scan in TwinCAT? Let me know in the comments — I’ve debugged many tricky startup issues and can help!
TRUE as a Constant First ScanNever do this:
IF TRUE THEN // This will run every cycle, not just first
Initialize();
END_IF