Build it step by step
0 of 7 doneRead the meter's manual
Address, speed, registers.
Wire A and B
Low-voltage side only.
Read one register
Print the voltage.
Check against the display
Same value.
Read the other values
Current and energy.
Log to SD with time
One line a minute.
Plot the load profile
In a spreadsheet.
How do you build an energy meter data logger?
Read a Modbus energy meter every minute and save kWh, current and power factor to SD and the cloud. You need ESP32 DevKit V1, RS485 to TTL auto-direction module, MicroSD Card Module, SanDisk microSD card 32 GB (A1, Class 10), DS3231 RTC module and 5 V 2 A USB adapter. It takes 7 steps and about 240 minutes.
- Level
- Intermediate
- Time
- About 240 min
- Parts
- 6
Wiring
| From | To | Why |
|---|---|---|
| RS485 module RXD (receive) and TXD (transmit) | ESP32 GPIO17 (TX2) and GPIO16 (RX2) | Serial to the bus (swap if no reply) |
| RS485 module VCC and GND | ESP32 3V3 or 5V (as the module needs) and GND | Power |
| RS485 A and B | Meter A and B terminals (low voltage communication side) | Modbus bus |
| Meter mains terminals | Wired by a qualified electrician only | Not touched by this build |
| SD module CS, MOSI, MISO, SCK | ESP32 GPIO5, GPIO23, GPIO19, GPIO18 | SPI |
| SD module VCC and GND | ESP32 VIN (5 V) and GND | Power |
| RTC SDA and SCL | ESP32 GPIO21 and GPIO22 | I2C |
Code
Industrial meters often have a small RS485 port, a two-wire bus that carries Modbus messages: the logger asks for a register by number, and the meter answers with the value. The ESP32 needs the RS485 module because the bus uses different voltage levels from the ESP32's pins, and the auto-direction type switches between sending and listening on its own. The hardest part is not the code but the meter's manual: it lists the slave address, speed, data type and register numbers, and every make is different, so the addresses in the sketch are only examples. Test with one register first and check that the value looks right against the meter's display. The communication terminals are low voltage, but the meter itself is connected to the mains, and its installation must be done by a qualified electrician in an enclosure; never open a live meter. If the bus is long, add a 120 ohm resistor across A and B at the far end, and keep the data wires away from mains cables. A plain Modbus bus is not encrypted or secured, so keep the logger on a private network. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Modbus RTU data logger: reads a meter's voltage, current and energy every minute and writes a CSV line to the SD card.
// Libraries: ModbusMaster, SD, RTClib. Board: ESP32 Dev Module.
// IMPORTANT: the register addresses below are examples only. Use your own meter's manual for the real addresses, data types and units.
#include <ModbusMaster.h>
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
ModbusMaster node;
RTC_DS3231 rtc;
const byte METER_ID = 1;
const long BAUD = 9600; // the meter's own setting: 9600 8N1 is common
const uint16_t REG_VOLTS = 0x0000; // example: float, 2 registers
const uint16_t REG_AMPS = 0x0006; // example
const uint16_t REG_KWH = 0x0156; // example
unsigned long lastLog = 0;
float readFloat(uint16_t reg, bool &ok) {
uint8_t r = node.readInputRegisters(reg, 2);
if (r != node.ku8MBSuccess) { ok = false; return 0; }
uint32_t raw = ((uint32_t)node.getResponseBuffer(0) << 16) | node.getResponseBuffer(1); // high word first: check your manual
float f; memcpy(&f, &raw, 4); return f;
}
void setup() {
Serial.begin(115200);
Serial2.begin(BAUD, SERIAL_8N1, 16, 17);
node.begin(METER_ID, Serial2);
rtc.begin();
if (!SD.begin(5)) { Serial.println("SD card error"); while (true) delay(1000); }
File f = SD.open("/meter.csv", FILE_APPEND);
if (f) { if (f.size() == 0) f.println("date,time,volts,amps,kwh"); f.close(); }
}
void loop() {
if (millis() - lastLog >= 60000UL) {
lastLog = millis();
bool ok = true;
float v = readFloat(REG_VOLTS, ok), a = readFloat(REG_AMPS, ok), e = readFloat(REG_KWH, ok);
DateTime n = rtc.now();
char line[96];
if (ok) snprintf(line, sizeof(line), "%04d-%02d-%02d,%02d:%02d:%02d,%.1f,%.3f,%.2f", n.year(), n.month(), n.day(), n.hour(), n.minute(), n.second(), v, a, e);
else snprintf(line, sizeof(line), "%04d-%02d-%02d,%02d:%02d:%02d,read failed", n.year(), n.month(), n.day(), n.hour(), n.minute(), n.second());
File f = SD.open("/meter.csv", FILE_APPEND);
if (f) { f.println(line); f.close(); }
Serial.println(line);
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Voltage | Matches the meter's own display |
| Switch a load on | Current rises in the log |
| CSV file | Opens with correct times |
| Bus unplugged | Lines say read failed |
If it does not work
No reply
Check A and B are not swapped and the address and speed match.
Strange numbers
Check the word order and the data type.
Random failures
Add termination and keep the cable away from mains wiring.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the energy meter data logger?
ESP32 DevKit V1, RS485 to TTL auto-direction module, MicroSD Card Module, SanDisk microSD card 32 GB (A1, Class 10), DS3231 RTC module and 5 V 2 A USB adapter.
How long does the energy meter data logger take to build?
About 240 minutes for a intermediate maker, in 7 steps.
How do I know the energy meter data logger works?
Voltage: Matches the meter's own display. Switch a load on: Current rises in the log. CSV file: Opens with correct times.
No reply: what should I check?
Check A and B are not swapped and the address and speed match.
Strange numbers: what should I check?
Check the word order and the data type.
Random failures: what should I check?
Add termination and keep the cable away from mains wiring.
