0
0
FreertosHow-ToBeginner · 4 min read

How to Implement Data Logging in PLC: Simple Guide

To implement data logging in a PLC, use internal memory buffers or registers to store data periodically, then write this data to external storage like an SD card or send it to a SCADA system. Use timers to control logging intervals and file handling or communication protocols for saving or transmitting data.
📐

Syntax

Data logging in PLC typically involves these parts:

  • Data Storage: Use arrays or registers to hold data samples.
  • Timer: Controls how often data is recorded.
  • Write Operation: Saves data to external memory or sends it via communication.
structured_text
VAR
  DataBuffer : ARRAY[1..10] OF INT; // Storage for logged data
  LogIndex : INT := 1;                // Current position in buffer
  LogTimer : TON;                     // Timer for logging interval
END_VAR

// Timer setup
LogTimer(IN:=TRUE, PT:=T#5S); // Log every 5 seconds

IF LogTimer.Q THEN
  DataBuffer[LogIndex] := SensorValue; // Save sensor value
  LogIndex := LogIndex + 1;
  IF LogIndex > 10 THEN
    LogIndex := 1; // Wrap around buffer
  END_IF;
  LogTimer(IN:=FALSE); // Reset timer
END_IF;
💻

Example

This example shows how to log temperature readings every 5 seconds into a buffer and then send the data to an external system when the buffer is full.

structured_text
VAR
  TempBuffer : ARRAY[1..5] OF REAL;
  Index : INT := 1;
  LogTimer : TON;
  SendFlag : BOOL := FALSE;
  Temperature : REAL; // Assume this is updated from a sensor
END_VAR

// Start timer for 5 seconds
LogTimer(IN:=TRUE, PT:=T#5S);

IF LogTimer.Q THEN
  TempBuffer[Index] := Temperature; // Store temperature
  Index := Index + 1;
  IF Index > 5 THEN
    Index := 1;
    SendFlag := TRUE; // Buffer full, ready to send
  END_IF;
  LogTimer(IN:=FALSE); // Reset timer
END_IF;

// Send data when buffer is full
IF SendFlag THEN
  // Pseudocode for sending data
  // SendData(TempBuffer);
  SendFlag := FALSE;
END_IF;
⚠️

Common Pitfalls

  • Buffer Overflow: Not resetting the index causes data overwrite or errors.
  • Timer Misuse: Forgetting to reset the timer can stop logging.
  • Data Loss: Not saving or sending data before buffer wraps causes lost logs.

Always ensure timers reset and buffers are managed carefully.

structured_text
VAR
  Buffer : ARRAY[1..3] OF INT;
  Pos : INT := 1;
  Timer : TON;
END_VAR

// Wrong: No timer reset
Timer(IN:=TRUE, PT:=T#2S);
IF Timer.Q THEN
  Buffer[Pos] := InputValue;
  Pos := Pos + 1;
  // Missing: Timer(IN:=FALSE) to reset
END_IF

// Right: Reset timer and wrap buffer
Timer(IN:=TRUE, PT:=T#2S);
IF Timer.Q THEN
  Buffer[Pos] := InputValue;
  Pos := Pos + 1;
  IF Pos > 3 THEN
    Pos := 1;
  END_IF;
  Timer(IN:=FALSE);
END_IF;
📊

Quick Reference

StepDescription
1. Setup TimerUse a timer to trigger data capture at intervals.
2. Store DataSave sensor or process values in an array or registers.
3. Manage BufferReset index to avoid overflow and data loss.
4. Save or SendWrite data to external memory or send via communication.
5. Reset TimerAlways reset timer after logging to continue cycles.

Key Takeaways

Use timers to control when data is logged in the PLC.
Store data in arrays or registers and manage buffer indexes carefully.
Reset timers after each logging event to maintain consistent intervals.
Send or save data before buffers overflow to prevent data loss.
Test logging logic thoroughly to avoid common mistakes like buffer overflow.