Back to All Blog Posts
Tutorial EltroNerd Engineering

Energy Usage Monitoring Smart Plug using ESP32 and INA219

Smart Energy Monitoring Plug using ESP32 and INA219. This project lets students monitor voltage, current, and power usage of any appliance in real-time.

Energy Usage Monitoring Smart Plug using ESP32 and INA219

Monitoring energy consumption has become essential for saving electricity and understanding appliance behavior.
In this project, we will build a Smart Plug using the ESP32 and the INA219 Current Sensor to measure voltage, current, and power of connected devices.
The ESP32 will log data and optionally send it over Wi-Fi for dashboards or notifications.


Hardware Requirements

  • ESP32 Development Board
  • INA219 Current/Voltage Sensor Module
  • Relay Module (5V) to switch appliance ON/OFF
  • Power supply for ESP32
  • Appliance (lamp, fan, etc.) for testing
  • Jumper wires and breadboard

Working Principle

  1. Voltage & Current Measurement:

    • The INA219 sensor measures voltage across and current through the load.
    • It communicates via I2C (SDA/SCL) with ESP32.
  2. Power Calculation:

    • Power (Watts) = Voltage × Current
    • Energy usage can be integrated over time to calculate kWh.
  3. Smart Control:

    • Relay module lets ESP32 switch the appliance ON or OFF.
    • Control can be local via GPIO or remote via Wi-Fi.

Arduino Code Example

We’ll use Adafruit INA219 library. Install via Arduino Library Manager.

c
#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

#define RELAY_PIN 23

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);

  if (!ina219.begin()) {
    Serial.println("Failed to find INA219 sensor!");
    while (1);
  }

  Serial.println("Smart Energy Monitoring Plug Ready...");
}

void loop() {
  // Read voltage, current, power
  float busVoltage = ina219.getBusVoltage_V();
  float current_mA = ina219.getCurrent_mA();
  float power_mW = ina219.getPower_mW();

  Serial.print("Voltage: "); Serial.print(busVoltage); Serial.println(" V");
  Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
  Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW");
  Serial.println("-------------------------");

  delay(1000);

  // Example: Switch relay ON if current > 10 mA
  if (current_mA > 10) {
    digitalWrite(RELAY_PIN, HIGH);
  } else {
    digitalWrite(RELAY_PIN, LOW);
  }
}

How to Use

** Connect the INA219: **

  • VCC → 3.3V or 5V (ESP32)

  • GND → GND

  • SDA → GPIO21 (default ESP32 I2C)

  • SCL → GPIO22

Connect the relay in series with appliance and mains (safely!)

Upload the code to ESP32, open Serial Monitor, and monitor:

  • Voltage (V)

  • Current (mA)

  • Power (mW)

Optionally: log data to Firebase, MQTT, or a web dashboard.

Extensions

Add Wi-Fi dashboard to monitor energy in real-time.

Send notifications when power usage exceeds threshold.

Measure energy consumption over time (kWh).

Combine with Alexa/Google Assistant for smart scheduling.

Try this project in your browser

Compile code and simulate hardware output with EltroNerd Cloud IDE.

Launch Cloud IDE