| Start type | First scan bit behavior | |------------|--------------------------| | Cold start | TRUE once | | Warm start | TRUE once | | Stop → Run | FALSE (not set) |
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.
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. beckhoff first scan bit
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 | Start type | First scan bit behavior
🚀 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. In TwinCAT 3 (and TwinCAT 2), the FirstScan
⚠️ 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.
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
In TwinCAT 3 (and TwinCAT 2), the FirstScan bit is a built-in boolean flag available inside every Programmable Organization Unit (POU) — specifically within the VAR_TEMP or implicitly as FirstScan in the PLC runtime. It is TRUE for exactly one PLC cycle after the application transitions from STOP to RUN or after a cold/warm start. From the second cycle onward, it becomes and remains FALSE until the next restart.
| TwinCAT Version | Variable Name | Scope |
|----------------|---------------|-------|
| TwinCAT 2 | bInit | Global (in Standard.lib) |
| TwinCAT 3 | FirstScan | Per-POU (automatic) |
PROGRAM MAIN VAR fbFirstScan : FB_FirstScan; bInitDone : BOOL; END_VAR
fbFirstScan(); IF fbFirstScan.bFirstScan THEN // One-time initialization code here bInitDone := TRUE; END_IF