Internal relay (memory bit) addressing in PLC Programming - Time & Space Complexity
When using internal relays (memory bits) in PLC programming, it's important to understand how the program's execution time changes as you use more bits.
We want to see how the number of memory bits accessed affects the time the PLC takes to run the program.
Analyze the time complexity of the following code snippet.
// Example: Setting multiple internal relays
FOR i := 0 TO n-1 DO
M[i] := TRUE; // Set internal relay bit
END_FOR
This code sets a series of internal relay bits from 0 up to n-1 to TRUE one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Setting each internal relay bit (M[i]) inside the loop.
- How many times: The loop runs n times, once for each bit.
As the number of bits n increases, the program sets more bits one after another.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sets |
| 100 | 100 sets |
| 1000 | 1000 sets |
Pattern observation: The number of operations grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to set internal relay bits grows linearly with the number of bits you set.
[X] Wrong: "Setting multiple internal relays at once takes the same time as setting one."
[OK] Correct: Each bit set requires an operation, so more bits mean more time, not the same time.
Understanding how loops over memory bits affect execution time helps you write efficient PLC programs and explain your reasoning clearly in interviews.
What if we replaced the loop with a single command that sets all bits at once? How would the time complexity change?
Practice
Solution
Step 1: Understand internal relay function
Internal relays store ON/OFF states inside the PLC memory but do not control physical devices.Step 2: Compare options
Only To store ON/OFF states inside the PLC without controlling physical devices correctly describes this purpose; others mention controlling devices or power, which is incorrect.Final Answer:
To store ON/OFF states inside the PLC without controlling physical devices -> Option AQuick Check:
Internal relay = memory bit for logic [OK]
- Confusing internal relays with output devices
- Thinking internal relays supply power
- Assuming internal relays display information
Solution
Step 1: Identify internal relay address format
Internal relays are commonly addressed with the letter 'M' followed by a number, e.g., M10.Step 2: Match options to address format
M10 uses 'M10', which is correct. Other options use letters for inputs (X), outputs (Y), or data registers (D).Final Answer:
M10 -> Option DQuick Check:
Internal relay = M + number [OK]
- Using input (X) or output (Y) addresses for internal relays
- Confusing data registers (D) with memory bits
- Omitting the letter prefix
--[ ]--[ ]--(M5)\n I1 I2
Inputs: I1 = ON, I2 = OFF
Solution
Step 1: Analyze input conditions
The rung has two contacts in series: I1 and I2. I1 is ON, I2 is OFF.Step 2: Determine relay output state
Both inputs must be ON for M5 to energize. Since I2 is OFF, M5 remains OFF.Final Answer:
OFF -> Option BQuick Check:
Series contacts require all ON for output ON [OK]
- Assuming relay toggles automatically
- Ignoring OFF input in series
- Confusing parallel and series logic
LD X0\nAND M1\nOUT M2
What is the error?
Solution
Step 1: Review each instruction
LD X0 loads input X0 state, AND M1 uses internal relay M1 as condition, OUT M2 sets internal relay M2.Step 2: Check addressing validity
Inputs (X) and internal relays (M) are correctly used. M1 and M2 are valid internal relay addresses.Final Answer:
No error, code is correct -> Option CQuick Check:
Input and internal relay addresses used properly [OK]
- Thinking internal relays can't be inputs
- Assuming OUT can't target internal relays
- Confusing input and output addresses
Solution
Step 1: Understand latch logic for memory bits
To keep M10 and M11 ON after sensors turn ON, latch logic uses the internal relay itself in parallel with the sensor input.Step 2: Analyze options for latch and reset
--[S1]--(M10)\n--[M10]--(M10)\n--[S2]--(M11)\n--[M11]--(M11)\n--[Reset]--(/M10)\n--[Reset]--(/M11) shows M10 and M11 set by sensors and latched by themselves, with reset contacts to turn them OFF. Others lack latching or proper reset.Final Answer:
--[S1]--(M10)\n--[M10]--(M10)\n--[S2]--(M11)\n--[M11]--(M11)\n--[Reset]--(/M10)\n--[Reset]--(/M11) -> Option AQuick Check:
Latching uses self-holding contact + reset to clear [OK]
- Not using latch (self-holding) contacts
- Resetting only one relay
- Using reset coil instead of reset contact
Ready to prove it?
Pass the test and this pattern moves unknown
