← ESP32 PLC 4-20 mA Pressure and Temperature Sensors
Hydraulic moving floor (BLE app)ESP32 PLC 38RGPIOAcquisition
ESP32 PLC 4-20 mA Pressure and Temperature Sensors — full example
Read 4-20 mA pressure and temperature transmitters on an ESP32 PLC 38R, convert to bar and Celsius, and detect broken loops. Full Arduino example.
Complete, runnable program for the ESP32 PLC 38R (pressure-temperature-4-20ma-sensors-38r.ino): wiring header, requirements and integration notes included.
Download the full project pack — freeThis example + the related ones + bill of materials
Read-only preview.
/*
* COMPLETE EXAMPLE — 4-20 mA pressure and temperature sensors with
* conversion to engineering units
*
* Hardware: ESP32 PLC 38R (Industrial Shields)
* Based on: hydraulic moving floor project, main.cpp (analog reading)
*
* Wiring:
* I0_4 4-20 mA temperature transmitter (range 5-90 degC)
* I0_5 4-20 mA pressure transmitter (range 0-250 bar)
*
* Logic:
* - The 38R analog inputs read the 4-20 mA loop as 0-1023.
* - 4 mA = bottom of scale, 20 mA = top of scale: linear conversion.
* - If the reading drops below the 4 mA threshold, the loop is broken
* (cut wire or disconnected sensor) -> validity flag set to false.
* That broken-wire detection is the big advantage of 4-20 mA
* over 0-10 V.
* - Simple moving average to filter the noise from the power
* electronics (solenoid valves switching near the wiring).
*
* Integration with other catalog modules:
* - Telemetry to the app over BLE as [T]temp [P]pressure
* (see ejemplos/piso-movil/ble-mobile-app-control-38r.ino).
* - Overpressure stop combined with the solenoid valves
* (see ejemplos/piso-movil/load-unload-solenoid-valves-38r.ino).
*/
#define INPUT_TEMP I0_4
#define INPUT_PRESSURE I0_5
// Transmitter scales
#define TEMP_MIN 5.0 // degC at 4 mA
#define TEMP_MAX 90.0 // degC at 20 mA
#define PRES_MIN 0.0 // bar at 4 mA
#define PRES_MAX 250.0 // bar at 20 mA
// Below this reading the loop is broken (less than ~3.5 mA)
#define RAW_BROKEN_LOOP 40
#define N_SAMPLES 10 // moving average
struct Measurement {
float value;
bool ok; // false = sensor disconnected / broken loop
};
// Linear conversion of the raw reading (0-1023 = 4-20 mA) to the physical range
float convertScale(int raw, float vMin, float vMax) {
return vMin + (raw / 1023.0) * (vMax - vMin);
}
// Filtered reading with broken-loop detection
Measurement readSensor(int pin, float vMin, float vMax) {
long sum = 0;
for (int i = 0; i < N_SAMPLES; i++) {
sum += analogRead(pin);
delay(2);
}
int raw = sum / N_SAMPLES;
Measurement m;
m.ok = (raw > RAW_BROKEN_LOOP); // <4 mA => broken wire
m.value = m.ok ? convertScale(raw, vMin, vMax) : 0.0;
return m;
}
void setup() {
Serial.begin(115200);
pinMode(INPUT_TEMP, INPUT);
pinMode(INPUT_PRESSURE, INPUT);
Serial.println("4-20 mA reading ready (ESP32 PLC 38R)");
}
void loop() {
Measurement temp = readSensor(INPUT_TEMP, TEMP_MIN, TEMP_MAX);
Measurement pres = readSensor(INPUT_PRESSURE, PRES_MIN, PRES_MAX);
// In the real project these frames travel over BLE to the mobile app
if (temp.ok) {
Serial.print("[T]");
Serial.print(temp.value, 1);
Serial.print(" C ");
} else {
Serial.print("[T]ERROR sensor disconnected ");
}
if (pres.ok) {
Serial.print("[P]");
Serial.print(pres.value, 0);
Serial.println(" bar");
} else {
Serial.println("[P]ERROR sensor disconnected");
}
// Overpressure alarm: the physical limit overrides any UI
if (pres.ok && pres.value > 230.0) {
Serial.println("[E]OVERPRESSURE -> the solenoid valve stop would go here");
}
delay(1000);
}
Download the full project pack — freeThis example + the related ones + bill of materials