Try Embedded

Overcurrent trip (ACS712)

Measure a load's current with an ACS712, zero it at start-up, average out the noise, and latch a trip when it stays over the limit.

Sensors · Intermediate

In a real product: Electronic circuit breaker (Energy): Measures the load's current and latches off when it stays over the limit, like a smart fuse.

Motors stall, wires short, batteries get connected backwards. An electronic fuse watches the current and cuts the load before anything burns. The ACS712 measures current without being part of the circuit electrically: the current flows through a copper path inside the chip, and a Hall sensor turns its magnetic field into a voltage.

What you'll learn

  • Converting an ADC reading into amps with an offset and a sensitivity
  • Calibrating the zero point at start-up instead of trusting the datasheet's 2.5 V
  • Averaging many samples to see past noise
  • A latched trip: once tripped it stays off until a person resets it

How it works

The 5 A ACS712 puts out half its supply with no current and moves 185 mV for every amp. One ADC step is 4.9 mV, so a single reading is only good to about 26 mA, and on a real board noise is worse. The sketch averages 64 readings for each measurement.

At start-up the load is off, so the average then is the zero point; every later reading is measured from it. A trip needs three measurements in a row over 3 A, so a single spike (a motor starting) does not trip it. Once tripped, the red LED stays on and the sketch ignores the current until the reset button is pressed and the current is back under the limit.

Try this

  • Click the ACS712 and raise the current past 3 A: it trips about 30 ms later. Lower it and press Reset.
  • Turn on the sensor's noise fault (±50 mV) and see how far a single reading moves, and how little the average does.
  • Change the range to 20 A: the sensitivity is 100 mV/A, so the sketch needs changing too.

sketch.ino

// An ACS712 (5 A) on A0: zeroed at start-up, averaged over 64 readings, and a latched trip after three readings in a
// row over 3 A. Pin 7's LED shows the trip; the button on pin 4 resets it.
const float SENSITIVITY = 0.185;  // volts per amp, 5 A version
const float LIMIT = 3.0;          // amps
const int TRIP_LED = 7, RESET = 4;

float zero = 0;
int over = 0;
bool tripped = false;

float averageVolts() {
  long sum = 0;
  for (int i = 0; i < 64; i++) sum += analogRead(A0);
  return sum / 64.0 * 5.0 / 1023;
}

void setup() {
  Serial.begin(9600);
  pinMode(TRIP_LED, OUTPUT);
  pinMode(RESET, INPUT_PULLUP);
  zero = averageVolts();  // the load is off at start-up
  Serial.print("Zero at ");
  Serial.print(zero, 3);
  Serial.println(" V");
}

void loop() {
  float amps = (averageVolts() - zero) / SENSITIVITY;
  if (!tripped) {
    over = amps > LIMIT ? over + 1 : 0;
    if (over >= 3) {
      tripped = true;
      Serial.println("TRIPPED");
    }
  } else if (digitalRead(RESET) == LOW && amps < LIMIT) {
    tripped = false;
    over = 0;
    Serial.println("Reset");
  }
  digitalWrite(TRIP_LED, tripped);
  Serial.print(amps, 2);
  Serial.println(" A");
  delay(10);
}

Lessons behind it