Difficulty Level: Intermediate
This tutorial will guide you through creating a smart energy meter using an ESP32 microcontroller and an ACS712 current sensor. The energy meter will measure power consumption and can display the data on an OLED screen or send it to a server.
Connect the ACS712 current sensor to the ESP32 as follows:
ACS712 Pin | ESP32 Pin
-------------|-------------
VCC | 3.3V
GND | GND
OUT | GPIO 34 (analog input)
For real-time display of the measured values, connect an OLED screen to the ESP32:
OLED Pin | ESP32 Pin
-------------|-------------
VCC | 3.3V
GND | GND
SCL | GPIO 22
SDA | GPIO 21
Below is the code to measure current, calculate power, and optionally send the data to a server. The code also includes an OLED display output for local data visualization.
#include
#include
#include
// OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
// Initialize OLED display (if used)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// ACS712 sensor pin
#define SENSOR_PIN 34
const float calibrationFactor = 0.066; // For 5A module, adjust if using 20A or 30A module
// Variables for readings
float voltage = 230.0; // Set your local AC voltage (e.g., 230V or 120V)
float current, power;
// Function to initialize OLED
void setupDisplay() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED init failed");
while (true);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
}
// Function to measure current
float getCurrent() {
int sensorValue = analogRead(SENSOR_PIN);
float voltageValue = (sensorValue / 4095.0) * 3.3; // Convert ADC to voltage
float current = (voltageValue - 1.65) / calibrationFactor; // Calculate current
return abs(current); // Return absolute current value
}
// Function to calculate power
float calculatePower(float current, float voltage) {
return current * voltage; // Power (W) = Current (A) * Voltage (V)
}
void setup() {
Serial.begin(115200);
pinMode(SENSOR_PIN, INPUT);
// Initialize OLED display
setupDisplay();
}
void loop() {
// Measure current and calculate power
current = getCurrent();
power = calculatePower(current, voltage);
// Display on Serial Monitor
Serial.print("Current (A): ");
Serial.println(current);
Serial.print("Power (W): ");
Serial.println(power);
// Update OLED display (optional)
display.clearDisplay();
display.setCursor(0, 0);
display.print("Current (A): ");
display.println(current);
display.print("Power (W): ");
display.println(power);
display.display();
// Add a delay for stable readings
delay(1000);
}
To make the energy meter "smart," you can send the data to a server or cloud platform for remote monitoring. Here’s an example of how to send data to a server using HTTP.
#include
#include
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
const char* serverURL = "http://your-server.com/api/data"; // Replace with your server URL
void setupWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
}
// Function to send data to server
void sendData(float current, float power) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverURL);
http.addHeader("Content-Type", "application/json");
String payload = "{\"current\":" + String(current) + ", \"power\":" + String(power) + "}";
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.print("Data sent, response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error in sending data: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi not connected");
}
}
void loop() {
current = getCurrent();
power = calculatePower(current, voltage);
Serial.print("Current (A): ");
Serial.println(current);
Serial.print("Power (W): ");
Serial.println(power);
sendData(current, power);
delay(10000); // Send data every 10 seconds
}
Compile the code in the Arduino IDE, select the correct ESP32 board and COM port, and upload it. If using the WiFi and server setup, ensure the ESP32 is connected to a stable WiFi network for reliable data transmission.
With this setup, you’ve created a basic smart energy meter using an ESP32, ACS712 current sensor, and an optional OLED display. This setup can be extended with more sensors or integrated with IoT platforms for advanced monitoring and analytics.