In previous posts we have described how to make a volt/amp/watt meter to measure power consumption, or production (solar, wind, etc). I never fully explained the watt hour and amp hour calculation, so here is a sample sketch that assumes you are measuring voltage and current with the appropriate sensors.
http://arduinotronics.blogspot.com/2015/05/reading-current-shunt-with-arduino.html
http://arduinotronics.blogspot.com/2012/04/voltage-monitor.html
I’m flushing the daily data every 24 hours, but keeping a running total. You could save this data to a sd card or publish it to a web server.
http://arduinotronics.blogspot.com/2014/04/add-logging-to-your-project-for-250.html
http://arduinotronics.blogspot.com/2015/12/sending-sensor-data-to-web-server.html
This is much easier to read on a LCD instead of the serial monitor.
Become the Maker you were born to be. Try Arduino Academy for FREE!
Code:
float volts = 120; // get value from voltage sensor
float amps = 100; // get value from current sensor
float watts = volts * amps;
float kwh;
unsigned long totalET;
float dailykwh;
float totalkwh;
void setup() {
Serial.begin(9600);
}
void loop() {
uint32_t ts1 = millis();
delay(1000); //reports at 1 second intervals
uint32_t ts2 = millis();
// print the time interval in seconds
uint32_t ts3 = (ts2-ts1)/1000;
totalET = totalET + ts3;
Serial.print(“Seconds: “);
Serial.println(totalET);
kwh = watts * ts3 / 3600000;
dailykwh = dailykwh + kwh;
Serial.print(“Volts: “);
Serial.println(volts, 4);
Serial.print(“Amps: “);
Serial.println(amps, 4);
Serial.print(“Watts: “);
Serial.println(watts, 4);
//Serial.print(“Current Ah: “);
// numbers so small it’s not relevant.
//Serial.println(kwh/volts, 4);
//Serial.print(“Current KWh: “);
// numbers so small it’s not relevant.
//Serial.println(kwh, 4);
Serial.print(“Daily Ah: “);
Serial.println(dailykwh/volts, 4);
Serial.print(“Daily KWh: “);
Serial.println(dailykwh, 4);
totalkwh = totalkwh + dailykwh;
Serial.print(“Total KWh: “); //from reboot
Serial.println(totalkwh, 4);
if (totalET >= 86400){ //restart every 24 hours
ts1 = 0;
ts2 = 0;
totalET = 0;
dailykwh = 0;
}
}
Rather than using two fixed value resistors i opt for using a potentiometer and using its center as the reference. The advantage is i can tweak the levels