Bird
Raised Fist0
PLC Programmingscripting~10 mins

Trend charts for process variables in PLC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Trend charts for process variables
Start Data Collection
↓
Read Process Variable
↓
Store Value in Buffer
↓
Update Trend Chart Display
↓
Check If More Data Needed?
No→Stop
↩Back to Read Process Variable
The flow shows how process variable values are read repeatedly, stored, and used to update a trend chart until data collection stops.
Execution Sample
PLC Programming
VAR
  buffer: ARRAY[1..5] OF INT := [0,0,0,0,0];
  index: INT := 1;
  value: INT;
END_VAR

value := ReadProcessVariable();
buffer[index] := value;
index := (index MOD 5) + 1;
This code reads a process variable, stores it in a circular buffer of size 5, and updates the index for the next value.
Execution Table
Stepindexvalue (Read)buffer stateAction
11100[100, 0, 0, 0, 0]Read value 100, store at buffer[1], index updated to 2
22105[100, 105, 0, 0, 0]Read value 105, store at buffer[2], index updated to 3
33110[100, 105, 110, 0, 0]Read value 110, store at buffer[3], index updated to 4
44108[100, 105, 110, 108, 0]Read value 108, store at buffer[4], index updated to 5
55107[100, 105, 110, 108, 107]Read value 107, store at buffer[5], index updated to 1
61109[109, 105, 110, 108, 107]Read value 109, overwrite buffer[1], index updated to 2
72111[109, 111, 110, 108, 107]Read value 111, overwrite buffer[2], index updated to 3
83115[109, 111, 115, 108, 107]Read value 115, overwrite buffer[3], index updated to 4
94117[109, 111, 115, 117, 107]Read value 117, overwrite buffer[4], index updated to 5
105120[109, 111, 115, 117, 120]Read value 120, overwrite buffer[5], index updated to 1
111-[109, 111, 115, 117, 120]Stop reading, data collection complete
💡 Data collection stops after 10 readings; buffer holds latest 5 values in circular fashion.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
index123451234511
buffer[0,0,0,0,0][100,0,0,0,0][100,105,0,0,0][100,105,110,0,0][100,105,110,108,0][100,105,110,108,107][109,105,110,108,107][109,111,110,108,107][109,111,115,108,107][109,111,115,117,107][109,111,115,117,120][109,111,115,117,120]
value-100105110108107109111115117120-
Key Moments - 3 Insights
Why does the index reset to 1 after reaching 5?
The index resets to 1 to overwrite the oldest value in the buffer, creating a circular buffer as shown in execution_table rows 5 to 6.
Why does the buffer hold only 5 values even after 10 readings?
Because the buffer size is fixed at 5, new values overwrite old ones in a circular way, keeping only the latest 5 values (see buffer state in rows 6 to 10).
What happens if we don't update the index after storing a value?
The same buffer position would be overwritten repeatedly, losing previous data. The index update ensures new values go to the next position (see index changes in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the buffer state?
A[100, 105, 0, 0, 0]
B[100, 105, 110, 108, 0]
C[100, 105, 110, 0, 0]
D[0, 0, 0, 0, 0]
💡 Hint
Check the 'buffer state' column in execution_table row 4.
At which step does the index first reset to 1?
AStep 10
BStep 5
CStep 6
DStep 11
💡 Hint
Look at the 'index' column in execution_table rows 5 and 6.
If the buffer size was 3 instead of 5, how would the buffer state change at step 4?
A[100, 105, 110]
B[100, 105, 110, 108]
C[108, 0, 0]
D[0, 0, 0]
💡 Hint
Buffer size limits how many values are stored; check buffer length in variable_tracker.
Concept Snapshot
Trend charts store recent process variable values in a buffer.
Use a circular buffer to overwrite oldest data when full.
Read variable, store in buffer[index], then update index.
Index resets to 1 after reaching buffer size.
This keeps the latest values for chart display.
Full Transcript
This lesson shows how to collect and store process variable values for trend charts using a circular buffer. The program reads a variable, stores it in a fixed-size buffer, and updates the index to the next position. When the index reaches the buffer size, it resets to 1 to overwrite the oldest data. This way, the buffer always holds the latest values for the trend chart. The execution table traces each step, showing index, value read, buffer state, and actions. Key moments clarify why the index resets and how the buffer size limits stored data. The visual quiz tests understanding of buffer states and index behavior.

Practice

(1/5)
1. What is the main purpose of a trend chart in PLC programming for process variables?
easy
A. To show how process variables change over time
B. To control the speed of the PLC processor
C. To reset the PLC memory automatically
D. To display error messages only

Solution

  1. Step 1: Understand the role of trend charts

    Trend charts are used to visualize data changes over time, especially for process variables like temperature or pressure.
  2. Step 2: Eliminate unrelated options

    Options about controlling processor speed, resetting memory, or showing errors do not relate to trend charts.
  3. Final Answer:

    To show how process variables change over time -> Option A
  4. Quick Check:

    Trend charts = show variable changes over time [OK]
Hint: Trend charts visualize data changes over time [OK]
Common Mistakes:
  • Confusing trend charts with control functions
  • Thinking trend charts reset PLC memory
  • Assuming trend charts only show errors
2. Which of the following is the correct way to declare an array to store 10 samples of a process variable in a PLC program?
easy
A. ARRAY samples[10] REAL;
B. samples = [10] REAL;
C. VAR samples: REAL[10]; END_VAR
D. VAR samples: ARRAY[1..10] OF REAL; END_VAR

Solution

  1. Step 1: Recognize PLC array declaration syntax

    In many PLC languages, arrays are declared with VAR, specifying index range and type, e.g., ARRAY[1..10] OF REAL.
  2. Step 2: Check each option

    VAR samples: ARRAY[1..10] OF REAL; END_VAR matches correct syntax. Options A, B, and D use incorrect or invalid syntax for PLC arrays.
  3. Final Answer:

    VAR samples: ARRAY[1..10] OF REAL; END_VAR -> Option D
  4. Quick Check:

    Correct PLC array syntax = VAR samples: ARRAY[1..10] OF REAL; END_VAR [OK]
Hint: Use VAR and ARRAY with index range for PLC arrays [OK]
Common Mistakes:
  • Using incorrect brackets or missing VAR keyword
  • Confusing array syntax with other languages
  • Not specifying index range properly
3. Given the following PLC code snippet that samples a temperature every cycle and stores it in an array of size 5, what will be the content of the array after 7 cycles?
VAR samples: ARRAY[1..5] OF REAL; index: INT := 1; temp: REAL; END_VAR

// On each cycle:
temp := ReadTemperature();
samples[index] := temp;
index := index + 1;
IF index > 5 THEN
  index := 1;
END_IF
medium
A. The array is empty because index resets too early
B. The array contains the first 5 temperature samples from cycles 1 to 5
C. The array contains the last 5 temperature samples from cycles 3 to 7
D. The array contains only the temperature from cycle 7 repeated

Solution

  1. Step 1: Understand the circular buffer logic

    The index increments each cycle and resets to 1 after 5, so the array stores the last 5 samples in a loop.
  2. Step 2: Analyze after 7 cycles

    After 7 cycles, index has wrapped around once. The array holds samples from cycles 3 to 7, overwriting the first two samples.
  3. Final Answer:

    The array contains the last 5 temperature samples from cycles 3 to 7 -> Option C
  4. Quick Check:

    Circular buffer stores last 5 samples = The array contains the last 5 temperature samples from cycles 3 to 7 [OK]
Hint: Index resets after max size to overwrite oldest samples [OK]
Common Mistakes:
  • Thinking array holds first 5 samples only
  • Assuming array is empty due to index reset
  • Believing array repeats last sample only
4. Identify the error in this PLC code snippet intended to sample a pressure value every 10 cycles and store it in an array:
VAR samples: ARRAY[1..5] OF REAL; counter: INT := 0; index: INT := 1; pressure: REAL; END_VAR

counter := counter + 1;
IF counter = 10 THEN
  samples[index] := ReadPressure();
  index := index + 1;
  counter := 0;
  IF index > 5 THEN
    index := 1;
  END_IF
END_IF
medium
A. The counter increments every cycle but sampling happens only when counter equals 10
B. The index reset condition should be inside the first IF block
C. The counter should reset after index increments, not before
D. The index reset condition is outside the sampling IF block, causing incorrect indexing

Solution

  1. Step 1: Review index reset placement

    The index reset IF was outside the sampling IF, causing it to run every cycle and reset index prematurely.
  2. Step 2: Correct placement

    Moving the index reset condition inside the sampling IF block ensures index resets only after a new sample is stored, preventing incorrect indexing.
  3. Final Answer:

    The index reset condition should be inside the first IF block -> Option B
  4. Quick Check:

    Index reset must be inside sampling block [OK]
Hint: Keep index reset inside sampling condition to avoid errors [OK]
Common Mistakes:
  • Placing index reset outside sampling condition
  • Resetting counter at wrong time
  • Misunderstanding counter increment logic
5. You want to create a trend chart that samples a flow rate every 5 seconds and stores the last 12 samples. Which approach correctly implements this in a PLC program?
hard
A. Use a timer that triggers every 5 seconds to read flow rate, store it in a 12-element array, and use a circular index to overwrite oldest samples.
B. Sample flow rate every cycle without timing, store in a 12-element array, reset index after 12 cycles.
C. Use a timer to sample every 12 seconds, store in a 5-element array, and reset index after 5 samples.
D. Sample flow rate only once at start, store in a 12-element array, then never update.

Solution

  1. Step 1: Match sampling frequency and storage size

    The requirement is to sample every 5 seconds and keep last 12 samples, so timer interval and array size must match.
  2. Step 2: Use circular buffer logic

    Using a timer to trigger sampling every 5 seconds and a circular index to overwrite oldest samples in a 12-element array fits the need.
  3. Final Answer:

    Use a timer that triggers every 5 seconds to read flow rate, store it in a 12-element array, and use a circular index to overwrite oldest samples. -> Option A
  4. Quick Check:

    Timer + circular buffer = correct trend sampling [OK]
Hint: Timer triggers sampling; circular buffer stores last samples [OK]
Common Mistakes:
  • Sampling every cycle without timing control
  • Using wrong timer interval or array size
  • Not overwriting oldest samples properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard