Skip to Content

← All functionalities

Hydraulic moving floor (BLE app)ESP32 PLC 38RGPIOAcquisition

Reading 4-20 mA pressure and temperature sensors on an ESP32 PLC

Industrial transmitters speak 4-20 mA for good reasons: noise immunity over long cables and built-in fault detection. This example, taken from a real mobile hydraulic machine deployment, reads a 0-250 bar pressure transmitter and a 5-90 °C temperature probe on the analog inputs of an ESP32 PLC 38R, converts the raw readings to engineering units with a two-line linear map, and flags any sensor whose loop current has dropped below 4 mA.

The two-line conversion

The PLC digitizes the 4-20 mA loop as 0-1023, where 0 corresponds to 4 mA and full scale to 20 mA. Converting to physical units is then 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

A healthy 4-20 mA loop never drops below 4 mA, so a near-zero reading can only mean a cut cable or an unpowered transmitter. The example maps that condition to a validity flag instead of reporting a misleading "0 bar". On a machine that travels on public roads and vibrates constantly, this distinction reaches the operator's phone before it becomes a breakdown.

Filtering next to power electronics

Solenoid valves switching near the analog wiring inject spikes into the readings, and on a compact mobile machine you rarely get the luxury of separating power and signal cables properly. A ten-sample average per measurement is enough to stabilize the displayed values without adding meaningful lag. The filtered values are then streamed over BLE as [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.

Related functionalities