Asia/Kolkata
Posts

Calibrating the ESP32 ADC for Low-Side Shunt Current Sensing

November 15, 2026

Why Raw ESP32 ADC Readings Drift

When you build a low-cost power board like a solar MPPT charger, a shunt resistor saves space and cash over Hall sensors like the ACS712. Still, reading that shunt voltage with an ESP32's built-in 12-bit ADC brings a known problem. The readings bend sharply near 0 V and 3.3 V. Below 150 mV and above 2.6 V (at 11 dB gain), raw analogRead() counts flatten out or jump. In an MPPT loop, dI/dV picks the next PWM duty step. A small gain error or noise near zero current makes the tracker hunt around the wrong point.

1. Clean Up the Analog Input First

Before you write code, shape the signal so the pin sees a clean DC level:
  1. Op-Amp Gain Stage: Use a rail-to-rail op-amp like the LM358 or INA180. Set the gain so full load current sits near 2.4 V. That keeps the signal inside the ESP32 ADC's clean middle range (150 mV to 2450 mV).
  2. RC Filter: Put a 1 kΩ resistor and 100 nF cap right before the GPIO pin. That gives a ~1.59 kHz cutoff and cuts MOSFET switching noise.
  3. Bypass Cap: Keep a 100 nF ceramic cap next to the pin so the ADC sample cap charges fast with no voltage dip.

2. Average 64 Samples in Firmware

Single ADC reads on an ESP32 with Wi-Fi on can bounce by 15 to 25 counts. Taking 64 samples in a row and averaging them cuts random noise by 8x. It still runs fast enough for a 20 Hz to 50 Hz MPPT loop.
Tip: Always pick ADC1 pins (GPIO32 to GPIO39) if your code uses Wi-Fi or BLE. ADC2 shares hardware with the Wi-Fi radio and can fail or return junk when packets send.

3. Fit a Third-Order Curve on the Bench

Espressif's built-in esp_adc_cal tool helps with chip drift, yet it cannot fix board parts. Resistor errors, op-amp offset, and ground drops still skew your numbers. A quick bench sweep fixes the whole chain at once:
  1. Pull known loads (0 A, 2 A, 5 A, 10 A, 15 A, 20 A, 25 A, 30 A) with a DC load.
  2. Log the averaged raw ADC count x and the true current I from a good bench meter.
  3. Fit a third-order curve I = a*x^3 + b*x^2 + c*x + d in Python with numpy.polyfit(x, y, 3).
Writing the math as ((a*x + b)*x + c)*x + d skips slow pow() calls and runs in microseconds.

Results on the 800W Buck Board

With 64x averaging, a simple RC filter, and this four-term curve fit, current error dropped from ±6.8% down to within ±1.5% across the whole 0 to 30 A range. I did not need an extra 16-bit I2C ADC chip like the ADS1115.