How to measure AC current with ESP32 and SCT-013 sensor
Learn how to instrument the ESP32 with the non-invasive SCT-013 sensor to monitor alternating current, calculate power, and track home electrical consumption in real time with functional code.
Summary
- The SCT-013 sensor operates as a current transformer that induces a safe, low-amperage signal proportional to the magnetic field of the conductor
- The ESP32 microcontroller requires a signal conditioning circuit with a voltage divider to shift the AC sine wave into the positive range supported by its analog converter
- High-frequency sampling in dedicated loops captures hundreds of points per cycle to calculate root mean square current values without distortion errors
- Practical calibration using a known resistive load and a standard multimeter eliminates reading skews caused by component and sensor tolerances
- The C++ code implements root mean square mathematical calculations and can transmit data via MQTT protocol to home automation platforms
Why monitor electrical energy with the ESP32?
Knowing exactly how much electricity your home appliances or main electrical panel consumes stops being a mystery once you build your own smart meter. In practice, this means you can identify consumption bottlenecks, detect equipment operating with hidden faults, or simply understand the financial impact of every outlet. The ESP32 microcontroller, a compact low-cost chip featuring built-in Wi-Fi and Bluetooth, serves as the perfect brain to collect, process, and send this data to the cloud or a local dashboard on your network.
Unlike complex industrial meters that require breaking into physical power lines, the approach we use here employs a simple and safe electromagnetic principle. Instead of cutting wires or messing directly with dangerous high-voltage terminals, we install an external component that clamps around the cable in a completely isolated manner. This ease of assembly opens doors for both home automation enthusiasts and engineers looking to prototype rapid energy efficiency solutions.
How the non-invasive SCT-013 current sensor works
The SCT-013 sensor is a current transformer in a clamp-on or flexible loop format that detects the magnetic field generated by the flow of electrons through a conductor wire. In practice, when alternating current from the grid passes through the cable, it induces a much smaller proportional current in the sensor's internal winding. This galvanic isolation completely protects your control circuit and microcontroller against dangerous electrical shocks or destructive short circuits in the main grid.
Different variations of this sensor exist on the market, with the SCT-013-000 model being one of the most popular for providing a current output signal typically ranging from zero to fifty milliamperes. For the ESP32 to read this variation, we must transform this current into a readable voltage by connecting a burden resistor. Furthermore, because alternating current oscillates cyclically between positive and negative values, we must bias the signal so that it operates entirely within the positive range supported by the microcontroller's analog converter.
Preparing the signal conditioning circuit for the ESP32
The ESP32 analog-to-digital converter accepts voltages strictly ranging between zero and three point three volts. Because the electrical grid alternates its polarity sixty times per second, the signal generated by the sensor would drop below zero and damage the reading pin or yield incorrect readings. To solve this on the workbench, we build a simple circuit with a voltage divider using two equal-value resistors, generally one hundred kiloohms, connected between the three-point-five volt supply and ground.
This divider creates a stable reference level at exactly one point six hundred and fifty volts, acting as a false ground or center bias. When the sensor is measuring no current, the reading on the ESP32 pin sits parked right at this central value. As current flows through the cable, the voltage oscillates above and below this resting point, perfectly tracing the electrical grid's sine wave that the reading program will process without severe distortions.
Implementing reading and RMS calculation in code
To transform the raw voltage samples collected by the ESP32 into useful current and power numbers, we use the root mean square concept, technically known as RMS. In practice, the instantaneous reading from the analog pin constantly oscillates, meaning we must sample the signal hundreds of times during several consecutive cycles of the electrical grid. We sum the squares of these readings, average them, and take the square root to find the actual value that an ordinary multimeter would display.
Another key detail we cover in instrumentation guides, such as how to calibrate analog sensors on ESP32 properly, is removing the DC level introduced by our center-bias circuit. In the code below, we subtract this reference value from each sample before applying scale conversion math.
#include <Arduino.h>const int sensorPin = 34;const int numSamples = 1480;const float vCC = 3.3;const float adcMax = 4095.0;const float calibrationFactor = 30.0;void setup() { Serial.begin(115200); pinMode(sensorPin, INPUT);}void loop() { long sumSquares = 0; for (int i = 0; i < numSamples; i++) { int rawValue = analogRead(sensorPin); float voltage = (rawValue * vCC) / adcMax; float adjustedVoltage = voltage - 1.65; sumSquares += (adjustedVoltage * adjustedVoltage); } float meanSquares = sumSquares / (float)numSamples; float rmsVoltage = sqrt(meanSquares); float currentRMS = rmsVoltage * calibrationFactor; float power = currentRMS * 127.0; Serial.print("RMS Current: "); Serial.print(currentRMS); Serial.print(" A | Estimated Power: "); Serial.print(power); Serial.println(" W"); delay(1000);}Practical workbench calibration and validation
Even when using high-precision components, minor variations in resistor tolerances and sensor inductance require an empirical adjustment process to ensure reading accuracy. To calibrate the system, you should connect a known resistive load appliance, such as a hair dryer on maximum heat or an electric heater, whose real power consumption in watts is provided by the manufacturer or measured with a reference power meter.
Next, you compare the raw value displayed on the ESP32 serial monitor with the real value measured by a real multimeter or utility meter. Adjust the multiplier constant in the code until the numbers match precisely. If you notice unstable readings or ghost noise when no device is plugged in, verify that cable connections are secure and that there is no electromagnetic interference nearby from switching power supplies or fluorescent lights. To explore these architectural concepts further in production, see our related technical analysis on How to create custom global keyboard shortcuts on macOS.
Final considerations
Measuring real-time energy consumption with the ESP32 and the SCT-013 sensor combines accessible electronics with efficient programming to deliver professional results right in your home. By mastering AC current reading, voltage divider signal conditioning, and RMS calculation, you gain the autonomy to expand your project with local displays, automated alerts, or full integration with open-source platforms. With patience during assembly and attention to calibration details, your smart meter will be ready to operate safely and reliably for years.