In this project, we’ll be creating an energy meter using an ESP32 development board and an INA219 current sensor. This energy meter will be able to measure DC current, voltage, and power. We’ll be using the ESP32s and 16×2 I2C displays to show all of the energy data in real-time. checkout previous post- Arduino Energy Meter Using INA219 DC Current Sensor
Thank You NextPCB:
Thanks to the support and sponsorship from NextPCB, Guys if you have a PCB project, I would like to recommend NextPCB for all your PCBs. Be sure to see their website for exciting discounts, coupons, and more
.NextPCB offers high-quality, reliable PCB starting at $1.9, and multilayer starting at $6.9. Also, everyone can enjoy free PCB assembly for 5 boards!
Required Components
Before you start, make sure you have the necessary components:
- ESP32 development board
- INA219 current sensor
- LiquidCrystal_I2C display module (16×2)
- Jumper wires
- Breadboard (optional)
- Power source for the ESP32
The ESP32 Energy Meter uses an ESP32 as the main controller. It measures current and voltage using the INA219 current sensor.
INA219 DC Current Sensor
The INA219 is a DC current sensor that is commonly used in electronic projects to measure and monitor current flow. It is a high-precision sensor capable of measuring current up to 3.2A with a resolution of 0.1mA.
it operates with a method where a small resistor is placed in series with the load, and the voltage across this resistor is converted by the INA219 to determine the amount of current flowing through it. it uses the I2C communication protocol, allowing for easy integration with other I2C devices. How to use DC INA219 Current Sensor with Arduino
Specifications
- Resistor: 0.1 ohms, 1% accuracy, 2W power.
- Voltage Range: Up to +26V.
- Current Measurement: ±3.2A with ±0.8mA resolution.
- Size: 0.9″ x 0.8″ PCB.
- Voltage Sensing: 0V to 26V range.
- Interface: I2C communication.
- Data: Measures current, voltage, and power.
- Addresses: 16 programmable addresses.
- Filtering: Filtering options available.
- Calibration: Calibration registers included.
Wiring Diagram & Connections
Connections for this setup are quite straightforward and easy to understand.
INA219 / LCD -> ESP32
- VCC -> 5V
- GND – > GND
- SDA -> D21
- SCL-> D22
Source Code & Libriries
- Install the required libraries:
Upload the below code to your ESP32 board.
Source Code Program of ESP32 Energy Meter using INA219 Current Sensor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#include <Wire.h> #include <LiquidCrystal_I2C.h> #include <Adafruit_INA219.h> Adafruit_INA219 ina219; LiquidCrystal_I2C lcd(0x27, 16, 2); float busvoltage = 0; float current_mA = 0; float power_mW = 0; void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(0, 0); lcd.print("ESP32 Energy"); lcd.setCursor(0, 4); lcd.print("Monitor"); delay(2000); if (!ina219.begin()) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("FAILED TO FIND"); lcd.setCursor(0, 1); lcd.print("INA219 MODULE"); while (1) { delay(10); } } Serial.begin(115200); } void loop() { measureValues(); // Other operations you might want to perform in the loop } void measureValues() { busvoltage = ina219.getBusVoltage_V(); current_mA = ina219.getCurrent_mA(); power_mW = ina219.getPower_mW(); lcd.clear(); lcd.setCursor(0, 1); lcd.print("V:"); lcd.print(busvoltage); lcd.setCursor(9, 1); lcd.print("I:"); if (current_mA < 3) { lcd.print("000"); } else if (current_mA < 10) { lcd.print("00"); lcd.print((int)current_mA); } else if (current_mA < 100) { lcd.print('0'); lcd.print((int)current_mA); } else { lcd.print((int)current_mA); } lcd.setCursor(0, 0); lcd.print("Power(mW):"); lcd.print((int)power_mW); delay(2000); } |
Project Working & Demo
After uploading the code, it’s time to test the ESP32 energy meter.
- Connect a battery as a Supply
- Connect an LED as the load.
The energy usage of the device is expressed in milliwatts (mW), while the current flow is measured in milliamperes (I), and the voltage level is represented in volts (V).