24 min read · Automation

A tank level sensor sends its value to a PLC, and the screen shows a level of 0 percent. The pump keeps filling until the tank overflows, when in fact the signal wire has come loose from the terminal. If the signal is 0–10 V, a reading of 0 volts is also a valid lowest level, so the system cannot tell the two cases apart. With 4–20 mA, the reading is 0 mA, which is outside the working range and tells you at once that the loop is broken.
This article compares the two signals with code that actually runs: converting values to engineering units, detecting broken wires and out-of-range values, filtering, calculating whether the loop's power supply is sufficient, and the reasons behind noise and cable length. It also comes with a Structured Text function block for a PLC, but that file has never been compiled, so you must check it yourself before use, as stated again in the cautions section.
The real problem: one zero that can mean two things
The two most common analog signals in control work are 4–20 mA current and 0–10 V voltage. People usually ask which one is better, but the question to ask first is: when something fails, will our system know? Consider three hypothetical situations. None of them is a BT San customer project.
- A water tank in a small factory: the level transmitter is wired to the PLC at 0–10 V. Vibration shakes the wire loose from the terminal, the input floats close to 0 V, the screen shows a level of 0 percent, and the pump keeps filling without stopping.
- A pressure gauge 200 meters from the control room: the 0–10 V signal runs in the same cable tray as the motor cables. The value swings whenever a motor starts, and the grounds at the two ends differ by half a volt. The reading shifts by five percent of the measuring range and nobody knows it has shifted.
- A newly installed 4–20 mA loop: it uses a 12 V supply and a 250 ohm current-sense resistor. At the 12 mA test point everything is normal, but as the value approaches 20 mA the transmitter does not get enough voltage and the reading sticks at one level, because nobody ever calculated the loop's voltage budget.
All three cases are solved by three things: a signal whose zero is not a true zero (live zero), receiving logic that does not display a bad value as if it were real, and calculating the circuit before installation.
What the documents confirm, and what they do not
| Topic | What the source says | Effect on our code |
|---|---|---|
| Live zero | The Wikipedia section on the current loop states that a live zero (elevated zero) makes it possible to power the device even when there is no process signal. | 4 mA is 0 percent. A reading of 0 mA must be treated as a fault. |
| Range treated as a fault | The same page states that the loop is self-checking: a current below 3.8 mA or above 20.5 mA indicates a fault, and it cites the NAMUR NE 43 standard as the source. | The author did not open the NE 43 original, so the thresholds in the code are neutral, adjustable values and are not claimed to be NE 43 values. |
| Wire resistance | Wikipedia states that signal accuracy is not affected by the voltage drop in the connecting wires, and that the signal can be sent as far as the limit set by the resistance relative to the applied voltage. | Calculate the loop voltage budget instead of worrying about long cables. |
| Noise | Wikipedia states that the loop is a low-impedance circuit, usually run with twisted-pair cable, and the twisted pair page explains that twisting makes noise common-mode, so it can be cancelled at the receiving end. | Use twisted-pair cable, and estimate the error of a voltage signal separately. |
| Measuring and checking the loop | Fluke states that 4 mA is the sensor's zero level and 20 mA is full scale, and that a check should cover the transmitter, the wiring, the input card and the return wire. | Scale linearly from 4 to 20 mA to the engineering-unit range. |
The parts that are our own calculations, such as the loop voltage budget and the error estimate for 0–10 V, come from Ohm's law (V = IR), which was confirmed from Wikipedia. They are not data from any manufacturer. All numbers in the examples are hypothetical.
Overview of the example
The code is in the folder examples/current-loop-4-20ma-vs-0-10v/. The TypeScript side uses only Node 24 built-ins, so no npm install is needed. The Structured Text file is a translation of the same logic into a PLC language.
| File | Purpose |
|---|---|
| loop.ts | scaling, clamp, status classification, EMA filter, debounce, loop voltage budget, error of the voltage signal |
| fb_loop420.st | a Structured Text function block with the same logic, never compiled |
| samples.ts | hypothetical data, including a broken wire, a dead transmitter, NaN and abnormally high values |
| demo.ts / loop.test.ts | a script that runs the example in six parts, and a node:test test suite |
| output.txt | the actual output of the demo |
Step 1: convert to engineering units with a clamp, and never show a bad value
The formula is a linear scale from the 4–20 mA range to the range you want, for example a level of 0–100 percent. But short examples often skip two points. First, values slightly out of range, such as 3.9 or 20.4 mA, can really occur because of transmitter and input tolerance, so you must clamp them to avoid a negative level or one above 100. Second, values far from the range, such as 0 mA or 21.8 mA, should not be converted to a number at all. The function returns null and lets the caller decide.
export function classify(x: number, w: Window): Level {
if (!Number.isFinite(x)) return "open_wire";
if (x < w.faultLow) return "open_wire";
if (x < w.lo) return "under";
if (x <= w.hi) return "ok";
if (x <= w.faultHigh) return "over";
return "high_fault";
}
// Engineering value, clamped to the range. Returns null when the reading is a fault: never show a number then.
export function scale(x: number, w: Window, engLo: number, engHi: number): number | null {
const level = classify(x, w);
if (level === "open_wire" || level === "high_fault") return null;
return clamp(linear(x, w.lo, w.hi, engLo, engHi), Math.min(engLo, engHi), Math.max(engLo, engHi));
}There are five statuses: open_wire (below the lower threshold), under (slightly below 4 mA), ok, over (slightly above 20 mA), and high_fault (above the upper threshold). The four thresholds are in an adjustable Window structure. The defaults of 3.6 and 21 mA are neutral values we chose ourselves. If your organization uses NE 43, or the device manual gives other values, change them in this one place.
Read the abnormally high current with care. A current far above the range indicates a fault, but the cause could be the transmitter sending a high-level alarm signal, a short in the loop, or a failed input. The logic cannot tell these apart by itself. You need an on-site check and the transmitter's manual.
Step 2: why 0–10 V cannot tell you the wire is broken, and how live zero helps
A voltage input with a broken wire usually reads zero or a floating value, and a valid 0–10 V signal can also be zero, so the two cases look identical. The fix at the signal level is to raise the zero, for example to 2–10 V (live zero), and treat anything below 1.5 V as a fault. The cost is that the measuring resolution drops to eight tenths of the range, and the receiving device must support this kind of signal, because not every transmitter offers it.
console.log("\n== 3. 0-10 V: zero volts is a valid reading and a broken wire ==");
for (const v of [0, 0.4, 5]) {
console.log(`0-10 V ${f(v, 1).padStart(4)} V ${classify(v, V_PLAIN).padEnd(10)} -> ${f(scale(v, V_PLAIN, 0, 100))}`);
}
for (const v of [0, 1.0, 2.0, 6.0]) {
console.log(`2-10 V ${f(v, 1).padStart(4)} V ${classify(v, V_LIVE_ZERO).padEnd(10)} -> ${f(scale(v, V_LIVE_ZERO, 0, 100))}`);
}Step 3: filter the signal and delay the fault decision
Readings contain noise and spikes. The EMA (exponential moving average) filter, whose formula is: new value times alpha plus old value times (1 minus alpha), is the simplest first-order filter to write. An alpha close to 1 filters little and responds quickly. A small alpha filters heavily but responds slowly. One caution: the filter must not take bad samples in, or the filtered value will slowly drift down to zero when the wire breaks. So the example keeps the last good value while a fault is present.
The other layer is debounce of the fault status. The example must see 3 bad samples in a row before it latches, and 2 good samples in a row before it clears. This prevents the alarm from flickering on a single spike. The sample counts must be chosen to match the scan cycle and how fast the process changes.
export class FaultDebounce {
latched = false;
private bad = 0;
private good = 0;
private persist: number;
private clear: number;
constructor(persist: number, clear: number) {
this.persist = persist;
this.clear = clear;
}
update(isBad: boolean): boolean {
if (isBad) {
this.bad++;
this.good = 0;
if (this.bad >= this.persist) this.latched = true;
} else {
this.good++;
this.bad = 0;
if (this.good >= this.clear) this.latched = false;
}
return this.latched;
}
}Step 4: loop voltage budget: is the supply enough?
A 4–20 mA loop needs enough voltage for the maximum current to flow through the current-sense resistor (burden), through the wire out and back, and still leave enough for the transmitter to work. By Ohm's law, the remaining voltage equals the supply minus the maximum current times (burden plus total wire resistance), minus the minimum voltage the transmitter requires. If the result is negative, the loop cannot be installed. This function returns the voltage drop, the remaining margin, and the maximum burden the loop can still tolerate. Use a maximum current equal to the transmitter's high alarm current. The example uses 22 mA.
export type Budget = { dropV: number; marginV: number; maxBurdenOhm: number; ok: boolean };
// Loop power: supply must cover transmitter minimum voltage + I*(burden + wire) at the highest current.
export function loopBudget(p: {
supplyV: number;
txMinV: number;
burdenOhm: number;
wireOhmPerKm: number;
lengthM: number; // one-way length; the loop is out and back
maxMa: number; // use the alarm-high current if the transmitter can drive it
}): Budget {
const wire = (p.wireOhmPerKm * p.lengthM * 2) / 1000;
const i = p.maxMa / 1000;
const dropV = i * (p.burdenOhm + wire);
const marginV = p.supplyV - dropV - p.txMinV;
return { dropV, marginV, maxBurdenOhm: (p.supplyV - p.txMinV) / i - wire, ok: marginV >= 0 };
}A real run: good and bad data through the same logic
demo.ts is divided into six parts. Part one walks through ten sample currents. Part two converts ADC counts. Part three compares 0–10 V with 2–10 V. Part four tests the filter and debounce. Part five calculates the voltage budget. Part six estimates the error of a voltage signal. The main loop of part four is this code.
console.log("\n== 4. EMA filter and fault debounce (persist 3, clear 2) ==");
let s: number | null = null;
const deb = new FaultDebounce(3, 2);
for (const x of noisy) {
const bad = x < 5; // simplified: a dropout to 0 stands for open wire
if (!bad) s = ema(s, x, 0.3); // faulty samples never enter the filter: the last good value is held
console.log(`raw ${f(x).padStart(5)} filtered ${f(s).padStart(5)} fault latched: ${deb.update(bad)}`);
}
== 1. 4-20 mA to 0..100 percent level (clamped, faults give no number) ==
bottom of range 4.00 mA ok -> 0.0
25 percent 8.00 mA ok -> 25.0
mid 12.00 mA ok -> 50.0
top of range 20.00 mA ok -> 100.0
slightly under 3.90 mA under -> 0.0
slightly over 20.40 mA over -> 100.0
broken wire 0.00 mA open_wire -> --
dead transmitter 3.20 mA open_wire -> --
alarm high / short 21.80 mA high_fault -> --
ADC glitch (NaN) NaN mA open_wire -> --
== 2. ADC counts (0..4095 = 0..25 mA) ==
655 counts = 4.00 mA -> 0.0
2048 counts = 12.50 mA -> 53.1
3277 counts = 20.01 mA -> 100.0
0 counts = 0.00 mA -> --
== 3. 0-10 V: zero volts is a valid reading and a broken wire ==
0-10 V 0.0 V ok -> 0.0
0-10 V 0.4 V ok -> 4.0
0-10 V 5.0 V ok -> 50.0
2-10 V 0.0 V open_wire -> --
2-10 V 1.0 V open_wire -> --
2-10 V 2.0 V ok -> 0.0
2-10 V 6.0 V ok -> 50.0
== 4. EMA filter and fault debounce (persist 3, clear 2) ==
raw 50.0 filtered 50.0 fault latched: false
raw 50.4 filtered 50.1 fault latched: false
raw 49.7 filtered 50.0 fault latched: false
raw 50.2 filtered 50.1 fault latched: false
raw 80.0 filtered 59.0 fault latched: false
raw 50.1 filtered 56.4 fault latched: false
raw 49.8 filtered 54.4 fault latched: false
raw 0.0 filtered 54.4 fault latched: false
raw 0.0 filtered 54.4 fault latched: false
raw 0.0 filtered 54.4 fault latched: true
raw 0.0 filtered 54.4 fault latched: true
raw 50.3 filtered 53.2 fault latched: true
raw 50.0 filtered 52.2 fault latched: false
raw 49.9 filtered 51.5 fault latched: false
== 5. Loop power budget (made-up numbers) ==
24 V, 250 ohm, 300 m drop 5.70 V margin 6.30 V max burden 536 ohm OK
24 V, 500 ohm, 300 m drop 11.20 V margin 0.80 V max burden 536 ohm OK
12 V, 250 ohm, 100 m drop 5.57 V margin -3.57 V max burden 88 ohm NOT ENOUGH
== 6. Voltage signal errors ==
wire 9 ohm vs 100 kohm input: 0.009 percent of reading
wire 9 ohm vs 10 kohm input: 0.090 percent of reading
ground offset 0.5 V on 10 V span: 5.0 percent of spanReading the results one by one:
- Part 1: 4, 8, 12 and 20 mA give 0, 25, 50 and 100 percent. 3.9 and 20.4 mA are clamped to 0 and 100. 0 mA, 3.2 mA, 21.8 mA and NaN get no number (shown as --) because they are faults.
- Part 2: the hypothetical ADC gives 0 to 4095 counts for 0 to 25 mA. 655 counts is about 4 mA. 0 counts is 0 mA, so it is a broken wire.
- Part 3: plain 0–10 V at 0 V gives status ok and a value of 0 percent, exactly the same as a broken wire. 2–10 V at 0 V and at 1 V is open_wire immediately.
- Part 4: a spike of 80 pulls the filtered value up to only 59, and it falls back by itself. During four samples of zero, the filtered value stays at 54.4 and the fault latches at the third sample, then clears after two good samples in a row.
- Part 5: 24 V with a 250 ohm burden and a 300 meter cable leaves 6.30 V, which passes. 12 V with a 250 ohm burden comes out at negative 3.57 V, which fails, and the burden it can tolerate is only about 88 ohms (like the loop that sticks as it nears 20 mA).
- Part 6: a nine-ohm long cable with a 100 kilohm voltage input causes an error of only 0.009 percent, but a 0.5 V ground difference on a 10 V range is an error of 5 percent of the range.
Noise and cable length: the reasons behind the numbers above
Part 6 of the run corrects a common misunderstanding. Wire resistance is not the main problem for a voltage signal when the input has high impedance. The real problem is voltage that mixes into the signal: the ground difference between the two ends, and noise induced from motor cables or inverters, which adds directly to the signal. A current loop is a low-impedance circuit, so the same noise voltage drives less current, and because the current in one loop is the same at every point, it does not depend on the voltage drop in the wire. (The limit is that the total voltage must be enough, which is Step 4.)
Practical conclusions: use twisted-pair cable, route it separately from power cables, and ground the shield at one end following plant practice. If you must use 0–10 V, keep the cable short, use it inside the same cabinet or with a common ground, and accept that the usable distance depends on the noise conditions on site. There is no fixed number of meters.
PLC side: a Structured Text function block that has never been compiled
fb_loop420.st translates the same logic (fault thresholds, scale, clamp, filter and debounce) into an IEC 61131-3 Structured Text function block. The file was written by comparing it with loop.ts and has never been compiled or run on any PLC or simulator. All testing done so far is on the TypeScript side only. Before use, you must compile it in the manufacturer's tool, check the data types, and test it with the same values used in loop.test.ts.
IF bBad THEN
iBad := iBad + 1;
iGood := 0;
IF iBad >= iPersist THEN bFault := TRUE; END_IF
ELSE
iGood := iGood + 1;
iBad := 0;
IF iGood >= iClear THEN bFault := FALSE; END_IF
(* scale and clamp, only from good samples *)
rScaled := rEngLo + (iMa - rLo) * (rEngHi - rEngLo) / (rHi - rLo);
IF rScaled < rEngLo THEN rScaled := rEngLo; END_IF
IF rScaled > rEngHi THEN rScaled := rEngHi; END_IF
IF bFirst THEN
rFiltered := rScaled;
bFirst := FALSE;
ELSE
rFiltered := rAlpha * rScaled + (1.0 - rAlpha) * rFiltered;
END_IF
END_IFTest suite
loop.test.ts has seven cases. They cover scale and clamp, faults that must return null, adjustable thresholds, the difference between 0–10 V and 2–10 V, counts conversion, the filter and debounce (including the case where an interrupted run of bad samples must not latch), and the voltage budget for both pass and fail cases. Run it with node --test loop.test.ts. All seven pass. The tests do not verify timing, because that depends on the machine.
What to add for real use
- Use numbers from the real devices' documents: the transmitter's alarm current, the transmitter's minimum voltage, the burden value and the input module's resolution, not the hypothetical values in the example.
- The example uses a hypothetical linear ADC. A real system needs calibration of the offset and gain of the input channel and the current-sense resistor.
- The filter works per sample and is not tied to time. In a real system, alpha should relate to the scan cycle and the process time constant.
- You need alarms and acknowledgement following alarm management principles, and a test that disconnects a real wire during installation.
- The example assumes a 2-wire (loop-powered) current loop. Other types, such as 4-wire or active inputs, use different circuits, so read the manual for that model.
Hypothetical example: adapting it to the three businesses
- A small drinking-water plant: the tank level transmitter sits in a wet area with pump motors. Use 4–20 mA so that a broken wire becomes an alarm signal, not a level of zero.
- A cold room: the temperature sensor is in the same cabinet as the controller, with a short cable. 0–10 V is acceptable, but you must set conditions that check for values outside what is physically possible, such as a temperature change that cannot happen within one second.
- A production line that connects a control valve and several pressure transmitters: make a voltage budget table for every loop before ordering parts, because one loop with extra devices in series, such as a local display, may push the total burden beyond what the loop can tolerate.
Common mistakes and a checklist
- Using 0–10 V for work that needs to know about a broken wire, with no other way to detect it.
- Converting every value to a number, even faults, so the screen shows a wrong value that looks real.
- Feeding bad samples into the filter, so the value slowly drifts toward zero instead of raising an alarm.
- Not calculating the voltage budget, and testing only at a mid-range current.
- Connecting current to a voltage input, or voltage to a current input, without checking the module's circuit first.
- Grounding the shield at both ends without considering the effect of a ground loop.
- Identify the module's input type (current or voltage, active or passive) and the transmitter type from the manuals.
- Set the measuring range and engineering units, then choose fault thresholds from the documents you use.
- Test with simulated data covering low, mid, high, slightly out of range, broken wire, and abnormally high values.
- Calculate the voltage budget at maximum current, including the longest cable case.
- Set the filter and debounce to match the scan cycle.
- Disconnect a real wire during installation and check that the alarm appears and the value does not mislead.
- Compile and test the PLC code in the real tool before handover.
Cautions for the example: all data and thresholds are hypothetical and do not claim to follow the NAMUR NE 43 standard. The Structured Text file has never been compiled. The test results come from the TypeScript side only. Signal selection and real wiring must follow the device manuals, and a person with electrical knowledge must check them before use on a real system.
References
- Wikipedia — Current loop (live zero, 3.8 / 20.5 mA fault levels, wiring voltage drop) (accessed 2026-11-21)
- Fluke — How to measure a 4–20 mA loop signal (accessed 2026-11-21)
- Wikipedia — Twisted pair (common-mode noise rejection) (accessed 2026-11-21)
- Wikipedia — IEC 61131-3 (Structured Text, function blocks) (accessed 2026-11-21)
- Wikipedia — Exponential smoothing (accessed 2026-11-21)
- Wikipedia — Ohm's law (accessed 2026-11-21)




