Reading 4-20 mA pressure and temperature sensors on an ESP32 PLC
The two-line conversion
value = min + raw/1023 * (max - min) — one formula reused for both sensors with different ranges. Keeping ranges as named constants makes swapping a 250 bar transmitter for a 400 bar one a one-line change.Wire-break detection for free
Filtering next to power electronics
[T] and [P] frames, and the same pressure reading drives an overpressure stop that works independently of the mobile app.A snippet from the implementation
Straight from the example as deployed on the ESP32 PLC 38R — copy it freely:
void setup() {
Serial.begin(115200);
pinMode(INPUT_TEMP, INPUT);
pinMode(INPUT_PRESSURE, INPUT);
Serial.println("4-20 mA reading ready (ESP32 PLC 38R)");
}The full example is a complete program — wiring header, setup and main loop — ready to adapt to your application.
Frequently asked questions
Why use 4-20 mA instead of 0-10 V sensors?
Current loops are immune to voltage drop along the cable and to most electrical noise, and the live-zero at 4 mA lets the PLC detect a broken wire. On mobile machines with long, vibrating harnesses this is decisive.
What does a reading below 4 mA mean?
A broken loop — cut cable, loose terminal or unpowered transmitter. The firmware should flag the value as invalid rather than treating it as the minimum of the measurement range.
How accurate is the ESP32 PLC analog input for 4-20 mA?
The dedicated 4-20 mA inputs of the PLC 38R give roughly 10-bit useful resolution, about 0.25 bar per count on a 250 bar transmitter. With averaging, that is ample for monitoring and protection tasks.