0
0
FreertosHow-ToBeginner · 3 min read

How to Create Flashing Light Using Timer in PLC

To create a flashing light in a PLC, use a Timer instruction to toggle an output bit on and off at set intervals. Configure the timer to run cyclically, and use its done bit to switch the light state, creating a flashing effect.
📐

Syntax

In PLC programming, a timer is used to measure time intervals. The common timer types are TON (Timer On Delay) and TOF (Timer Off Delay). For flashing lights, TON is often used.

  • TON: Starts timing when input is true.
  • PT: Preset time for the timer.
  • ET: Elapsed time since timer started.
  • Q: Timer done bit, true when elapsed time >= preset time.

You toggle the output based on the timer's done bit to create a flashing effect.

structured_text
TON TimerName(IN:=Input, PT:=T#1s);
// IN: Timer start input
// PT: Preset time (e.g., 1 second)
// Q: Timer done output
// Use Q to toggle output for flashing
💻

Example

This example uses a TON timer to flash a light every second. The timer runs continuously, and the output light toggles on when the timer is done and off when it resets.

structured_text
VAR
  FlashTimer : TON;
  LightOutput : BOOL := FALSE;
  TimerEnable : BOOL := TRUE;
END_VAR

// Call the timer with 1 second preset
FlashTimer(IN := TimerEnable, PT := T#1s);

// Toggle light when timer done
IF FlashTimer.Q THEN
  LightOutput := NOT LightOutput;
  TimerEnable := FALSE; // Reset timer
ELSE
  TimerEnable := TRUE;
END_IF;
Output
LightOutput toggles TRUE and FALSE every 1 second, creating a flashing light effect.
⚠️

Common Pitfalls

Common mistakes when creating flashing lights with timers include:

  • Not resetting the timer properly, causing it to stop toggling.
  • Using the timer done bit directly without toggling the output, resulting in the light staying on or off.
  • Setting the preset time too short or too long, making the flash too fast or too slow.

Always ensure the timer input is controlled to restart the timer and toggle the output bit.

structured_text
(* Wrong approach: Using timer done bit directly without toggle *)
LightOutput := FlashTimer.Q; // Light stays on after timer done

(* Correct approach: Toggle output when timer done *)
IF FlashTimer.Q THEN
  LightOutput := NOT LightOutput;
  TimerEnable := FALSE; // Reset timer
ELSE
  TimerEnable := TRUE;
END_IF;
📊

Quick Reference

Tips for flashing light using timer in PLC:

  • Use TON timer with a suitable preset time (e.g., 1 second).
  • Toggle output bit when timer done bit is true.
  • Reset timer input to restart timing cycle.
  • Adjust preset time to control flash speed.

Key Takeaways

Use a TON timer to measure intervals for flashing light control.
Toggle the output bit when the timer done bit is true to create flashing.
Reset the timer input to restart the timing cycle continuously.
Adjust the timer preset time to change the flash speed.
Avoid using the timer done bit directly as output without toggling.