Water pumps in Bangladesh burn out for the same few reasons: the tank or line runs dry, the impeller jams, or the motor is overloaded. This ESP32 smart pump protector watches the pump’s current with a clip-on sensor, uses a small AI model to recognise dry running and unusual behaviour, and switches the pump off through a contactor before damage happens. It is a practical, real-world AI project – ideal for a final-year project or for your own house pump.

ESP32 smart pump protector tutorial cover: AI detects dry run, jamming and overload
ESP32 smart pump protector: AI that listens to your pump’s current

Why use AI instead of a simple current threshold?

A classic dry-run relay switches off when current falls below a fixed value. That works, but the right value is different for every pump, changes with supply voltage (very common in Bangladesh), and needs manual adjustment. An AI model learns your pump’s normal current pattern – its level and its small fluctuations – and recognises the dry-run pattern you showed it. An extra anomaly detection block also warns you about patterns it has never seen, such as a failing bearing.

ESP32 smart pump protector: example current patterns for normal, dry run and jammed pump
When a centrifugal pump runs dry its current drops; a jammed rotor or overload makes it rise

Important: the AI is an extra layer. The firmware below still keeps a hard over-current limit, and your panel must still have an MCB and a thermal overload relay. Never rely on AI alone for motor protection.

How it works

ESP32 smart pump protector block diagram with CT sensor, ESP32-S3, relay and contactor
Block diagram: the ESP32 only measures and controls a relay; the contactor switches the motor
  • A clip-on SCT-013 current transformer goes around the pump’s live wire – no cutting or soldering on the mains side.
  • The ESP32-S3 samples the CT signal at 2 kHz and computes the RMS current every 200 ms (10 mains cycles).
  • The last 10 seconds of RMS values go into a TinyML model trained in Edge Impulse.
  • If the model is confident it sees dry run for 2 seconds in a row, the ESP32 releases a relay, which drops the contactor coil and stops the pump.
  • A buzzer, OLED and optional Wi-Fi alert tell you why it tripped. It retries automatically after 30 minutes (the tank may have refilled), up to 3 times, then waits for a manual reset.

Hardware list

PartPurposeNotes
ESP32-S3 dev boardMeasurement + AI + controlSee the ESP32 AI hardware guide
SCT-013 voltage-output CT (e.g. 15A/1V or 20A/1V)Non-invasive current sensingChoose a rating about 2× the pump’s running current
2 × 10 kΩ, 10 µF, 1 kΩ, 100 nFBias and filter for the ADCDetails below
Opto-isolated 5 V relay moduleDrives the contactor coilRelay ON = pump allowed (fail-safe)
Contactor + thermal overload relay + MCBSwitches and protects the motorMust be installed by an electrician
OLED, active buzzer, push buttonStatus, alarm, manual resetOptional but recommended
5 V 1 A supply (isolated)Powers the ESP32Use a certified, isolated adapter

CT sensor interface (with calculations)

The ESP32 ADC cannot read negative voltage, so we lift the CT’s AC signal to the middle of the 3.3 V range:

  • Two 10 kΩ resistors from 3.3 V to GND make a 1.65 V bias; a 10 µF capacitor from the midpoint to GND keeps it steady.
  • Connect one CT lead to the 1.65 V midpoint and the other lead through a 1 kΩ series resistor to the ADC pin (GPIO1).
  • Add 100 nF from the ADC pin to GND. With 1 kΩ this forms a low-pass filter at about 1.6 kHz and limits current into the pin during spikes.

Will the signal fit? Espressif’s Arduino ADC documentation lists about 150–3100 mV as the input range at 11 dB attenuation (Arduino-ESP32 ADC docs). Example: a pump running at 6 A with an SCT-013-015 (15 A → 1 V rms):

StepCalculationResult
CT output (rms)6 A × (1 V / 15 A)0.40 V rms
CT output (peak)0.40 V × 1.414±0.57 V
Voltage at ADC pin1.65 V ± 0.57 V1.08 V to 2.22 V ✔
At the 15 A rating (worst case)1.65 V ± 1.41 V0.24 V to 3.06 V – just inside the range

That is why we pick a CT rated about 2× the running current: normal operation sits comfortably in the middle, and even a start-up surge stays readable. Our detailed guide on CT current measurement with a microcontroller ADC covers burden resistors and calibration.

Step 1: Record your pump

Upload this data-collection sketch. It prints one RMS current value every 200 ms, which the Edge Impulse data forwarder reads over USB (edge-impulse-data-forwarder, axis name amps).

#define CT_PIN 1
const float AMPS_PER_VOLT = 15.0f;   // SCT-013-015: 15 A -> 1 V rms
static float offset_mv = 1650.0f;

float read_rms_amps() {               // 400 samples at 2 kHz = 200 ms = 10 cycles at 50 Hz
  double sum = 0;
  uint32_t next = micros();
  for (int i = 0; i < 400; i++) {
    float mv = analogReadMilliVolts(CT_PIN);
    offset_mv += 0.0005f * (mv - offset_mv);   // slowly track the 1.65 V bias
    float d = mv - offset_mv;
    sum += (double)d * d;
    next += 500;
    while ((int32_t)(micros() - next) < 0) { }
  }
  return (float)(sqrt(sum / 400.0) / 1000.0) * AMPS_PER_VOLT;
}

void setup() { Serial.begin(115200); }
void loop()  { Serial.println(read_rms_amps(), 3); }
  • Check the reading with a clamp meter first and adjust AMPS_PER_VOLT until they match.
  • Record normal running: at least 10 minutes in total, at different times of day (supply voltage changes).
  • Record dry run: close the suction valve or let the source run low, but only for 10–15 seconds at a time, with a cool-down between recordings. Dry running damages seals quickly – never record it for long.
  • Do not try to jam the motor to collect data. The hard over-current limit and the anomaly detector cover jams and overloads.

Step 2: Train the model in Edge Impulse

  1. Create impulse: window 10000 ms, stride 1000 ms, frequency 5 Hz.
  2. Processing block: Flatten (it gives mean, min, max, RMS and standard deviation of the window).
  3. Learning blocks: Classification (labels normal and dry_run) plus Anomaly detection (K-means) on the flatten features. Edge Impulse explains the anomaly block in its documentation.
  4. Train and check the test set. Dry run is usually very easy to separate – expect over 95% accuracy.
  5. Deploy as an Arduino library and add the ZIP in Arduino IDE.

Step 3: The protector firmware

This is the complete protection loop. The hard limits run first and don’t depend on the AI.

#include <pump_guard_inferencing.h>   // name comes from your Edge Impulse project

#define CT_PIN      1     // ADC pin with the biased CT signal
#define RELAY_PIN   10    // HIGH = pump allowed (relay drives the contactor coil)
#define BUZZER_PIN  11
#define RESET_PIN   12    // push button to GND

const float AMPS_PER_VOLT = 15.0f;   // calibrated in step 1
const float RUN_AMPS      = 0.5f;    // below this the pump is OFF (float switch, timer)
const float MAX_AMPS      = 9.0f;    // hard over-current limit (~1.3 x nameplate current)
const float FAULT_SCORE   = 0.80f;
const int   CONFIRM_COUNT = 10;      // 10 x 200 ms = 2 s of agreement before tripping
const uint32_t RETRY_MS   = 30UL * 60UL * 1000UL;   // auto-retry after a dry run
const int   MAX_RETRIES   = 3;

static float window_buf[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE];   // last 10 s of RMS values
static size_t filled = 0;
static int fault_count = 0, retries = 0;
static bool tripped = false, manual_reset_only = false;
static uint32_t trip_time = 0;
static float offset_mv = 1650.0f;

float read_rms_amps() {
  double sum = 0;
  uint32_t next = micros();
  for (int i = 0; i < 400; i++) {
    float mv = analogReadMilliVolts(CT_PIN);
    offset_mv += 0.0005f * (mv - offset_mv);
    float d = mv - offset_mv;
    sum += (double)d * d;
    next += 500;
    while ((int32_t)(micros() - next) < 0) { }
  }
  return (float)(sqrt(sum / 400.0) / 1000.0) * AMPS_PER_VOLT;
}

void trip(const char *why, bool manual_only) {
  digitalWrite(RELAY_PIN, LOW);        // contactor drops, pump stops
  digitalWrite(BUZZER_PIN, HIGH);
  tripped = true;
  manual_reset_only = manual_only;
  trip_time = millis();
  filled = 0;
  fault_count = 0;
  Serial.printf("TRIP: %s\n", why);  // add a Wi-Fi / SMS alert here
}

void allow_pump() {
  tripped = false;
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(RELAY_PIN, HIGH);
}

float score_of(const ei_impulse_result_t &r, const char *label) {
  for (size_t i = 0; i < EI_CLASSIFIER_LABEL_COUNT; i++) {
    if (strcmp(r.classification[i].label, label) == 0) return r.classification[i].value;
  }
  return 0.0f;
}

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(RESET_PIN, INPUT_PULLUP);
  allow_pump();
}

void loop() {
  if (tripped) {
    if (digitalRead(RESET_PIN) == LOW) { retries = 0; allow_pump(); }
    else if (!manual_reset_only && retries < MAX_RETRIES && millis() - trip_time > RETRY_MS) {
      retries++;
      allow_pump();
    }
    delay(50);
    return;
  }

  float amps = read_rms_amps();
  if (amps > MAX_AMPS) { trip("over-current", true); return; }     // hard limit, no AI needed
  if (amps < RUN_AMPS) { filled = 0; fault_count = 0; return; }     // pump is simply off

  // slide the 10-second window
  memmove(window_buf, window_buf + 1, (EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE - 1) * sizeof(float));
  window_buf[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE - 1] = amps;
  if (filled < EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE) { filled++; return; }   // also skips start-up surge

  signal_t signal;
  numpy::signal_from_buffer(window_buf, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);
  ei_impulse_result_t r = { 0 };
  if (run_classifier(&signal, &r, false) != EI_IMPULSE_OK) return;

  float dry = score_of(r, "dry_run");
  fault_count = (dry > FAULT_SCORE) ? fault_count + 1 : 0;
  Serial.printf("%.2f A  dry_run=%.2f", amps, dry);
#if EI_CLASSIFIER_HAS_ANOMALY == 1
  Serial.printf("  anomaly=%.2f", r.anomaly);
  if (r.anomaly > 0.5f) Serial.print("  <- unusual pattern, inspect the pump");
#endif
  Serial.println();

  if (fault_count >= CONFIRM_COUNT) {
    if (retries >= MAX_RETRIES) trip("dry run (repeated)", true);
    else trip("dry run", false);
  }
}

Set MAX_AMPS to about 1.3× the current on your pump’s nameplate. Because the relay must be energised for the pump to run, the system is fail-safe: if the ESP32 crashes or loses power, the pump stops. If you prefer the pump to keep running when the protector fails, use the relay’s normally-closed contact instead – but then a dead controller gives no protection.

Safety and testing

  • All 230 V wiring – contactor, overload relay, MCB – must be done by a qualified electrician, in a proper enclosure with earthing.
  • The ESP32 side must be powered from an isolated, certified 5 V adapter. Never build a transformerless supply for this.
  • Test the logic first with a lamp or heater as the “pump”, then on the real pump while watching it.
  • Log a few days of data and check for false trips before you leave it unattended.

Also useful: our AquaCon automatic water pump controller (level-probe based control), which pairs well with this protector, and the wire size and voltage drop calculator for long pump cables.

Upgrade ideas

  • Add an MPU6050 on the pump body and train on vibration too – it catches bearing wear earlier than current.
  • Add a voltage sensor and block restarts during very low supply voltage.
  • Send the trip reason and daily running hours to a phone dashboard over Wi-Fi.
  • New to ESP32 AI? Start with the easy AI on ESP32 voice-light tutorial to learn the workflow.

Frequently asked questions

Will this work with a three-phase pump?

Yes, with one CT per phase (or at least one CT and phase-failure protection). Train the model on all three RMS values together.

Can I skip the contactor and switch the pump with the relay module?

No. Small relay modules are not rated for motor starting current. Use the relay only to drive a contactor coil.

Is AI really needed for dry-run protection?

Not strictly – a well-set threshold also works. The AI version adapts to your specific pump and voltage conditions, adds anomaly warnings, and makes a much stronger final-year project.

Does it need internet?

No. Everything runs on the ESP32. Wi-Fi is only needed if you want phone alerts.

Want a ready pump-protection board for your home, factory or product? We design custom PCBs, firmware and complete working prototypes for students and companies. Message us on WhatsApp or use the contact form. You can write in Bangla or English.


MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *