Be careful when trying to clear or initialize variables marked as RETAIN or PERSISTENT using a classic first scan bit. Persistent variables are designed to survive power cycles. If your first scan bit overwrites them every time the PLC boots, you defeat the purpose of persistent storage (like saving machine calibration offsets). 3. Execution Order Matters
PROGRAM MAIN VAR bInitDone : BOOL; myCounter : INT; END_VAR
BEGIN IF FirstScan THEN // Initialize variables MyVar1 := 10; MyVar2 := 20.5; InitDone := TRUE; END_IF END_PROGRAM
Unlike some traditional PLCs (like Allen-Bradley or Siemens) that provide a dedicated, built-in system variable for this purpose out of the box, Beckhoff’s TwinCAT environment handles this differently. Because TwinCAT is based on the IEC 61131-3 standard, developers have a few explicit, flexible methods to detect and utilize the first execution cycle. Method 1: Using the Standard System Variable ( PLC_STARTUP )
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 beckhoff first scan bit
Use these in combination:
The most robust way is to use the PlcTaskSystemInfo structure, which contains a FirstCycle boolean. This bit is only during the very first execution of that specific task after the TwinCAT runtime starts. Implementation Example (Structured Text):
Clearing or setting standard function block triggers (like R_TRIG or F_TRIG ) so they do not falsely fire on startup.
In PLC programming, the first scan bit (also known as the first cycle bit) is a special flag that is after the controller is started. After this initial cycle, the flag is reset to FALSE for the remainder of the program's execution. Its primary purpose is to distinguish the initial start-up sequence from all subsequent normal operation cycles. Be careful when trying to clear or initialize
), 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
The Beckhoff First Scan Bit, also known as the "FirstScan" bit, is a special bit in the TwinCAT 3 system that indicates when the PLC (Programmable Logic Controller) is executing its first scan cycle. In other words, it is a flag that is set during the initial scan of the PLC program. This bit is typically used to execute specific code or actions only once, during the first scan of the PLC.
TwinCAT provides system variables via its standard libraries that can detect system startup conditions. Implementation
// Main logic uses bIsFirstScan... // Final line of code: bIsFirstScan := FALSE; Use code with caution. Copied to clipboard 3. SFC Initialization Flag Method 1: Using the Standard System Variable (
No manual coding is required to reset the bit; it is inherently tied to the task execution. 2. Manual Logic (The Classic Way)
Built-in FirstScan flag (recommended if provided)
TwinCAT 3 fully supports Object-Oriented Programming (OOP). If you are encapsulating your machine parts inside custom Function Blocks (FBs), you shouldn’t rely on a global first scan bit to initialize them. Instead, use the built-in system method FB_init .
If you are using Function Blocks, TwinCAT 3 supports the FB_init method. This is a specialized sub-method that runs when the block is instantiated (during PLC startup or after a download), making it the cleanest way to handle block-specific initializations. Why use a First Scan Bit?
bInitTriggered is FALSE . The code enters the first IF statement. bFirstScan becomes TRUE . At the end of the block, bInitTriggered is flipped to TRUE .