0
0
FreertosDebug / FixBeginner · 4 min read

How to Handle PLC Power Failure: Tips and Code Examples

To handle PLC power failure, implement a power-fail routine that saves critical data to non-volatile memory and uses retentive variables. Also, use startup logic to safely restore the system state when power returns.
🔍

Why This Happens

When a PLC loses power unexpectedly, all volatile memory is cleared, causing loss of important data and system state. This can lead to unsafe machine behavior or data corruption when power returns.

Many PLC programs do not handle power failure explicitly, so they restart as if from scratch, which can cause errors.

structured-text
VAR
  Counter: INT;
END_VAR

// Increment counter every cycle
Counter := Counter + 1;
Output
After power failure, Counter resets to 0 losing all previous count.
🔧

The Fix

Use retentive variables to keep values through power loss. Add a power-fail routine that saves critical data to non-volatile memory before power is lost. On startup, restore system state safely.

structured-text
VAR RETAIN
  Counter: INT;
END_VAR

// Increment counter every cycle
Counter := Counter + 1;

// On startup, check if power was lost and restore state if needed
Output
Counter retains its value after power failure and continues counting correctly.
🛡️

Prevention

To avoid issues from power failure:

  • Use retentive memory for critical variables.
  • Implement a power-fail detection routine using PLC hardware signals or watchdog timers.
  • Regularly save important data to non-volatile memory.
  • Test power failure scenarios during commissioning.
⚠️

Related Errors

Other common issues related to power failure include:

  • Data corruption: Without proper saving, data may become invalid.
  • Unsafe machine states: Machines may start in unexpected states causing hazards.
  • Watchdog timer resets: Improper handling can cause endless restart loops.

Key Takeaways

Always use retentive variables for data that must survive power loss.
Implement power-fail routines to save critical data before shutdown.
Restore system state safely on PLC startup after power returns.
Test power failure scenarios to ensure safe machine behavior.
Use hardware signals or watchdogs to detect power loss early.