All articles

    Measuring Current in Projects: Shunts, the INA219 and the ACS712

    How a microcontroller measures current: a shunt resistor and Ohm's law, low-side vs high-side sensing, amplifying a shunt with an op-amp, the INA219 with complete code, the Hall-effect ACS712 with zero calibration and its real resolution, measuring AC, logging consumption in Wh, and a comparison.

    Updated 6 min read

    Want to know how much your project draws from its battery? Or stop a motor if it stalls and starts drawing too much current? Or build an energy meter for a solar panel? You need to measure current from your code. And the whole basic idea rests on Ohm's law.

    The shunt resistor

    A very small resistor (say 0.1 Ω) placed in the current path. The current flowing through it creates a small voltage across it, and that voltage is measured:

    2 A through a 0.1 Ω resistor makes 0.2 V, and from that we calculate the current
    2 A through a 0.1 Ω resistor makes 0.2 V, and from that we calculate the current
    I = V ÷ R

    Choosing the resistor

    • Smaller = less voltage lost, but the signal is smaller and harder to measure.
    • Its power: P = I² × R. For example 3 A through 0.1 Ω = 0.9 W, so choose at least a 2 W resistor.
    • The voltage across the shunt is usually too small to read directly with the ADC, so it needs amplifying with an op-amp or a dedicated chip.
    • Accuracy and temperature: choose a 1% or better resistor, of the type made for measurement (current sense). An ordinary resistor's value drifts as it heats up.

    Kelvin connection: measure the voltage directly at the resistor's own legs, not at the far end of the track or wire. At 0.1 Ω, a joint or track with an extra 0.01 Ω causes a 10% error.

    Low side or high side?

    Low-side is easy but the load's ground is no longer zero; high-side keeps a clean ground but needs a dedicated chip
    Low-side is easy but the load's ground is no longer zero; high-side keeps a clean ground but needs a dedicated chip
    • Low-side: easy, because the voltage is close to zero, but the load's "ground" is no longer exactly zero. If that load talks to the microcontroller (an I2C sensor, for example), this small difference can disrupt communication.
    • High-side: the load's ground is clean, but you need a chip that can measure a small voltage difference while sitting up at the supply voltage. That's the job of chips like the INA219.

    A shunt with an op-amp

    If you build it yourself on the low side: a 0.1 Ω shunt at 1 A gives only 0.1 V. A non-inverting amplifier with a gain of about 20 (say 100 kΩ and 5.1 kΩ resistors) turns that into about 2 V, so you can measure up to about 2.4 A over the Uno's ADC range. Use an op-amp whose output swings close to ground and the supply (rail-to-rail), such as the MCP6002. The LM358 can't swing higher than about 1.5 V below its supply. The calculation is in the op-amp calculator.

    The INA219 (and INA226)

    A small module with a shunt (usually 0.1 Ω) and a measurement chip:

    • It measures current, voltage and power, and sends them over I2C.
    • High-side, up to about 26 V.
    • With the default shunt, the range is about ±3.2 A.
    • Excellent for measuring the consumption of battery projects and solar panels.
    #include <Wire.h>
    #include <Adafruit_INA219.h>
    
    Adafruit_INA219 ina;
    
    void setup() {
      Serial.begin(9600);
      ina.begin();                     // default address 0x40
    }
    
    void loop() {
      float v  = ina.getBusVoltage_V();   // load voltage
      float mA = ina.getCurrent_mA();
      float mW = ina.getPower_mW();
      Serial.print(v); Serial.print(" V  ");
      Serial.print(mA); Serial.print(" mA  ");
      Serial.print(mW); Serial.println(" mW");
      delay(500);
    }

    The library has preset range settings: if the current is small (under 400 mA), choose the 16 V / 400 mA setting for better resolution. The INA226 is more accurate and handles up to 36 V, and it suits measuring sleep currents in microamps with a larger shunt.

    Logging consumption in Wh

    Sum the power over time, and you'll know how much the project used over a whole day, or how much the solar panel charged:

    energy_Wh += (mW / 1000.0) * (dt_ms / 3600000.0);

    where dt_ms is the time since the last reading in milliseconds (from millis()). That's the figure you compare with your battery life calculations.

    The ACS712: Hall effect

    It measures the magnetic field of the current, so the circuit you're measuring is isolated from the sensor:

    • 5, 20 and 30 A versions.
    • Analog output: at zero amps about half the supply voltage (2.5 V on 5 V), rising or falling with current in either direction. The 5 A version gives about 185 mV per amp, and the 30 A version only about 66 mV.
    • With an ESP32 (3.3 V): the sensor runs on 5 V and its output can exceed 3.3 V, so bring it down with a voltage divider.
    • Drawbacks: sensitive to noise and nearby magnetic fields, and poor accuracy at small currents.
    const float SENS = 0.185;      // volts per amp (5 A version)
    float zero;
    
    float readVolts() {
      long s = 0;
      for (int i = 0; i < 100; i++) s += analogRead(A0);
      return s / 100.0 * 5.0 / 1023.0;
    }
    
    void setup() {
      Serial.begin(9600);
      zero = readVolts();           // zero calibration: with no current flowing
    }
    
    void loop() {
      float amps = (readVolts() - zero) / SENS;
      Serial.println(amps, 2);
      delay(200);
    }

    The real resolution: each step of the Uno's ADC is about 4.9 mV, so with the 5 A version the smallest step is about 26 mA, and with the 30 A version about 74 mA. So the ACS712 will never measure a sleeping ESP32, but it's excellent for a motor or a heater. Zero calibration in code matters because the real zero point isn't exactly 2.5 V, and it shifts with the supply voltage.

    Measuring alternating current

    The ACS712 measures AC too, but the output is a wave around the zero point. A single reading means nothing, so you take many readings over a full cycle or more (20 ms at 50 Hz) and calculate the RMS: the square root of the mean of the squared values. The difference between AC and DC is in the AC vs DC article.

    And to measure household appliance consumption without cutting a wire, there are clamp-on current transformers (such as the SCT-013) that close around one of the two wires. That's far safer than routing a 220 V wire through a module.

    Which should you choose?

    You needBest fit
    Small to medium current accurately, at low voltageINA219 / INA226
    High current or isolationACS712 or newer Hall sensors
    Cheap and simple measurementShunt + op-amp
    Household appliance consumptionClamp-on current transformer (SCT-013)
    Sleep current in microampsINA226 with a larger shunt, or a dedicated meter

    Warning

    Don't measure mains (220 V) current with these modules unless you understand the safety well. Even with the isolated ACS712, cheap modules don't always guarantee the isolation distances. Read mains safety before anything else.

    FAQ

    The INA219 reads a negative current? The shunt is wired backwards: the current enters at VIN− and leaves at VIN+. Swap the two wires, or invert the sign in code.

    The ACS712 reads a few milliamps with the load disconnected? Normal: noise and an imperfect zero calibration. Average more readings, and treat anything under about 50 mA as zero.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.