Ir al contenido

← Sensores de presión y temperatura 4-20 mA en PLC ESP32

Piso móvil hidráulico (app BLE)ESP32 PLC 38RGPIOAdquisición

Sensores de presión y temperatura 4-20 mA en PLC ESP32 — ejemplo completo

Lee transmisores de presión y temperatura 4-20 mA en un ESP32 PLC 38R, convierte a bar y grados Celsius y detecta lazos rotos. Ejemplo Arduino completo.

Programa completo y ejecutable para el ESP32 PLC 38R (pressure-temperature-4-20ma-sensors-38r.ino): incluye cabecera de conexionado, requisitos y notas de integración.

Descarga el pack completo del proyecto — gratisEste ejemplo + los relacionados + lista de materiales

Vista de solo lectura.

/*
 * 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);
}
Descarga el pack completo del proyecto — gratisEste ejemplo + los relacionados + lista de materiales