The Sensing Principle: Hall Effect vs. Shunt Resistor
An electrical current sensor translates the flow of electrons into a measurable signal, typically relying on one of two physical principles in embedded systems. The Hall effect sensor (like the ubiquitous ACS712) passes the load current through a conductor inside the IC, generating a proportional magnetic field that deflects electrons in a semiconductor element to produce an analog voltage. This method provides galvanic isolation, meaning the high-current load path is electrically separated from the low-voltage microcontroller pins, making it a staple for AC mains and high-side DC measurements.
Conversely, shunt-based sensors (like the INA219) utilize Ohm’s law by placing a precise, low-value resistor (the shunt) in series with the load. An internal instrumentation amplifier measures the microscopic voltage drop across this resistor—often in the millivolt range—and an onboard ADC digitizes it. While shunt sensors lack galvanic isolation and introduce a slight voltage burden to the circuit, they offer vastly superior DC precision, bidirectional measurement capabilities, and built-in digital I2C output that bypasses the analog-to-digital conversion headaches of the microcontroller.
Hardware Pinout and Wiring Matrix
Before wiring, you must resolve the most common bench mistake: voltage logic mismatch. The ACS712 is fundamentally a 5V analog device. If you power it with 5V, its analog output pin will swing up to 4.5V under load, which will permanently damage the 3.3V GPIO pins on an ESP32. The matrix below details the safe 3.3V supply configuration for the ACS712, alongside the digital INA219 alternative.
| Module | VCC Supply Range | GND | Signal Output | Interface | Galvanic Isolation |
|---|---|---|---|---|---|
| ACS712-20A | 3.3V (Strict for ESP32) | ESP32 GND | GPIO 34 (ADC1_CH6) | Analog Voltage | Yes (2.1 kV RMS) |
| INA219 | 3.3V to 5.5V | ESP32 GND | GPIO 21 (SDA) / GPIO 22 (SCL) | Digital I2C | No (Common ground required) |
Never wire an analog electrical current sensor to ESP32 ADC2 pins (GPIO 4, 12-15, 25-27). ADC2 is shared with the WiFi radio and will return garbage data or block reads entirely when WiFi is active. Always use ADC1 pins (GPIO 32-39).
Output Signal Math: Raw ADC to Amps
Let's focus strictly on the analog ACS712-20A to demonstrate raw-to-unit conversion, as digital sensors like the INA219 handle the internal ADC scaling via their driver libraries. The ACS712-20A has a sensitivity of 100 mV/A (0.1 V/A). When powered by a clean 3.3V supply, the zero-current offset (quiescent output) sits exactly at VCC/2, which is 1.65V.
The ESP32 features a 12-bit SAR ADC, yielding raw integer values from 0 to 4095. However, the ESP32 ADC is notoriously non-linear at the extreme top and bottom of its range. Fortunately, our 1.65V zero-offset sits right in the linear sweet spot. Here is the exact mathematical pipeline to convert the raw ADC reading into physical Amps:
- Raw to Voltage: Multiply the raw ADC reading by the voltage resolution (3.3V / 4095 = 0.00080586 V/bit).
- Voltage to Delta: Subtract the 1.65V zero-current offset to find the voltage shift caused by the magnetic field.
- Delta to Amps: Divide the delta voltage by the sensor sensitivity (0.1 V/A).
Consolidated Equation:
Current (A) = ((ADC_Raw * (3.3 / 4095.0)) - 1.65) / 0.1
At a 3.3V supply, the maximum voltage the ADC can read is 3.3V. Subtracting the 1.65V offset leaves 1.65V of positive headroom. Divided by 0.1 V/A, the absolute maximum measurable current before clipping the ESP32 ADC is 16.5 Amps. If your application requires measuring a full 20A continuous load on a 3.3V microcontroller, you must use the ACS712-05B (185 mV/A) or implement an analog voltage divider on the output pin.
// ESP32 ACS712-20A Basic Read (3.3V Logic)
const int adcPin = 34;
const float vRef = 3.3;
const float adcResolution = 4095.0;
const float sensitivity = 0.1; // 100mV/A for 20A module
const float vOffset = 1.65; // VCC / 2
void setup() {
Serial.begin(115200);
analogReadResolution(12);
}
void loop() {
long adcSum = 0;
for(int i = 0; i < 100; i++) {
adcSum += analogRead(adcPin);
delayMicroseconds(500);
}
float adcAvg = adcSum / 100.0;
float voltage = adcAvg * (vRef / adcResolution);
float current = (voltage - vOffset) / sensitivity;
Serial.print("Current: ");
Serial.print(current);
Serial.println(" A");
delay(500);
}
Calibration, Scaling, and Interference
Out of the box, the 1.65V offset assumption is theoretical. Component tolerances and ESP32 internal voltage drops mean your actual zero-current reading might be 1.62V or 1.68V. To calibrate, power the circuit with the load disconnected. Read the raw ADC average over 1,000 samples, convert that to voltage, and hardcode that value as your vOffset variable. For high-precision current sensing topologies, dynamic offset tracking in software is preferred to account for thermal drift.
Interference mitigation depends entirely on the sensor type. Hall effect sensors are inherently susceptible to external magnetic fields. If you mount an ACS712 within two inches of a stepper motor, a relay coil, or a transformer, the stray flux will induce a false current reading. Keep the sensor orthogonal to AC wiring and physically distant from magnetic sources. Shunt-based sensors, on the other hand, are immune to magnetic fields but highly vulnerable to ground loops and electromagnetic interference (EMI) on the analog I2C lines. When using an INA219 in a noisy environment, keep the SDA/SCL traces short, use 4.7kΩ pull-up resistors directly at the master, and ensure the shunt resistor is placed on the high-side (between VCC and the load) to keep the common-mode voltage within the IC's 26V absolute maximum rating.
The ESP32 ADC has a native noise floor of roughly ±15 to 20 raw counts even when perfectly grounded. Always implement a software low-pass filter (like an exponential moving average) or oversample by reading 64 to 100 times and averaging the result before applying the math equation. See the official Espressif ADC documentation for hardware-level noise mitigation strategies.
FAQ: Electrical Current Sensor Integration
What is the difference between an electrical current sensor and a voltage sensor?
An electrical current sensor must be placed in series with the load, meaning the load current physically flows through the sensor (or its internal conductor) to be measured. A voltage sensor is placed in parallel across the load, measuring the electrical potential difference without interrupting the circuit. In embedded systems, voltage sensing usually requires a simple resistor divider network to step down the voltage to microcontroller-safe levels, whereas current sensing requires dedicated ICs to handle the series resistance or magnetic coupling.
How do I wire an electrical current sensor to measure AC mains safely?
To measure 120V/240V AC mains, you must use a sensor rated for the appropriate isolation voltage, such as the ACS712 (rated for 2.1 kV RMS isolation) or a split-core Current Transformer (CT). The mains live wire passes through the sensor's high-voltage terminals, while the low-voltage output pins connect to your microcontroller. Never connect the microcontroller ground to the mains side of the sensor. For bench prototyping, it is vastly safer to use a non-contact split-core CT sensor (like the SCT-013-000) which clamps around the insulated outside of the wire, entirely eliminating the risk of lethal shock or short circuits.
Why is my electrical current sensor reading noisy or drifting?
Drifting in Hall effect sensors is almost always caused by thermal drift or external magnetic interference. The ACS712 sensitivity changes by roughly 0.1% per °C; if your load heats up the PCB, the zero-offset will shift. Noisy readings on the ESP32 are typically caused by the microcontroller's internal digital noise coupling into the ADC, or by switching power supplies (like buck converters) driving the load, which inject high-frequency ripple into the current path. Implement a 0.1µF ceramic capacitor between the analog output pin and ground to filter high-frequency noise, and use software oversampling to smooth the remaining variance.
Can an electrical current sensor measure bidirectional DC current?
Yes, both Hall effect and shunt sensors can measure bidirectional current, which is essential for applications like battery charge/discharge monitoring or H-bridge motor control. For the ACS712, a positive current pushes the output voltage above the 1.65V offset, while a reverse current pulls it below 1.65V. The math equation remains identical; the resulting amperage value will simply be negative. For the INA219, the internal ADC natively supports bidirectional shunt voltage measurements, returning signed 16-bit integers that the driver library translates into positive or negative milliamp readings.






