0
0
FreertosHow-ToBeginner · 2 min read

PLC Program to Convert Analog Input to Engineering Units

To convert an analog input in a PLC, read the raw input value and apply a scaling formula like ScaledValue = (RawValue / MaxRaw) * MaxEngineeringValue to get the engineering units.
📋

Examples

InputRawValue = 0
OutputScaledValue = 0
InputRawValue = 2048 (mid-scale)
OutputScaledValue = 50 (if MaxRaw=4095 and MaxEngineeringValue=100)
InputRawValue = 4095 (max)
OutputScaledValue = 100
🧠

How to Think About It

First, understand that the PLC reads analog signals as raw digital numbers from an analog-to-digital converter (ADC). To make these numbers meaningful, you scale them proportionally to the real-world measurement range, like temperature or pressure. Use a simple formula to map the raw input range to the engineering unit range.
📐

Algorithm

1
Get the raw analog input value from the PLC input channel.
2
Identify the maximum raw value from the ADC (e.g., 4095 for 12-bit).
3
Identify the maximum engineering unit value (e.g., 100 degrees).
4
Calculate the scaled value using the formula: (RawValue / MaxRaw) * MaxEngineeringValue.
5
Output or use the scaled value in your PLC program.
💻

Code

plc_programming
VAR
  RawValue : INT; (* Raw analog input from ADC *)
  ScaledValue : REAL; (* Converted engineering unit value *)
  MaxRaw : INT := 4095; (* Max ADC value for 12-bit input *)
  MaxEng : REAL := 100.0; (* Max engineering unit, e.g., 100 degrees *)
END_VAR

RawValue := AI_Channel; (* Assume AI_Channel is the analog input tag *)
ScaledValue := (REAL(RawValue) / REAL(MaxRaw)) * MaxEng;

(* Output the scaled value *)
Print('Scaled Value: ' + REAL_TO_STRING(ScaledValue));
Output
Scaled Value: 50.0
🔍

Dry Run

Let's trace RawValue = 2048 through the code

1

Read Raw Input

RawValue = 2048

2

Calculate Scaled Value

ScaledValue = (2048 / 4095) * 100 = 50.0

3

Output Result

Print 'Scaled Value: 50.0'

RawValueScaledValue
204850.0
💡

Why This Works

Step 1: Reading Raw Input

The PLC reads the analog input as a raw integer value from the ADC, which represents the signal strength.

Step 2: Scaling Calculation

The raw value is divided by the maximum raw value to get a fraction, then multiplied by the maximum engineering unit to convert it.

Step 3: Output the Scaled Value

The scaled value is now in real-world units and can be used for control or display.

🔄

Alternative Approaches

Lookup Table Scaling
plc_programming
VAR
  RawValue : INT;
  ScaledValue : REAL;
  LookupTable : ARRAY[0..4] OF REAL := [0.0, 25.0, 50.0, 75.0, 100.0];
  Index : INT;
END_VAR

RawValue := AI_Channel;
Index := RawValue / 819; (* 4095/5 approx 819 *)
ScaledValue := LookupTable[Index];
Print('Scaled Value: ' + REAL_TO_STRING(ScaledValue));
Uses predefined values for faster conversion but less precise scaling.
Linear Interpolation
plc_programming
VAR
  RawValue : INT;
  ScaledValue : REAL;
  RawLow : INT := 0;
  RawHigh : INT := 4095;
  EngLow : REAL := 0.0;
  EngHigh : REAL := 100.0;
END_VAR

RawValue := AI_Channel;
ScaledValue := EngLow + (REAL(RawValue - RawLow) * (EngHigh - EngLow) / REAL(RawHigh - RawLow));
Print('Scaled Value: ' + REAL_TO_STRING(ScaledValue));
More flexible scaling for any input and output range.

Complexity: O(1) time, O(1) space

Time Complexity

The scaling calculation is a simple arithmetic operation done once per input read, so it runs in constant time O(1).

Space Complexity

Only a few variables are used for calculation, so space complexity is O(1).

Which Approach is Fastest?

Direct formula scaling is fastest and simplest; lookup tables add overhead but can speed up repeated conversions with fixed ranges.

ApproachTimeSpaceBest For
Direct Formula ScalingO(1)O(1)Simple, precise scaling
Lookup Table ScalingO(1)O(n)Fast conversion with fixed steps
Linear InterpolationO(1)O(1)Flexible range scaling
💡
Always know your ADC resolution and sensor range before scaling analog inputs.
⚠️
Forgetting to convert integer raw input to real before scaling causes incorrect results.