forked from wisewolf/Arduino-Power-Meter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino-Power-Meter.ino
150 lines (121 loc) · 3.94 KB
/
Arduino-Power-Meter.ino
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <Arduino.h>
/*
MAX471
*/
#define max471VT A0
#define max471AT A1
float Voltage = 0;
float Current = 0;
float Power = 0;
float Energy = 0;
float minVoltage = 0;
float maxVoltage = 0;
float minCurrent = 0;
float maxCurrent = 0;
float minPower = 0;
float maxPower = 0;
float minEnergy = 0;
float maxEnergy = 0;
float SumCurrent = 0;
float AvgCurrent = 0;
float CurrentHour = 0;
const long interval = 1000; // interval at which to blink (milliseconds)
unsigned long previousMillis = 0;
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1125300L / result; // Back-calculate AVcc in mV
return result;
}
void showData(){
Serial.print("Voltage = "); // shows the Voltage measured
Serial.print(Voltage);
Serial.print(" V");
Serial.print(" Min = ");
Serial.print(minVoltage,3);
Serial.print(" V");
Serial.print(" Max = ");
Serial.print(maxVoltage,3);
Serial.println(" V");
Serial.print("Current = "); // shows the Current measured
Serial.print(Current,5); // 5 digits after decimal point
Serial.print(" A");
Serial.print(" Min = ");
Serial.print(minCurrent,5);
Serial.print(" A");
Serial.print(" Max = ");
Serial.print(maxCurrent,5);
Serial.println(" A");
Serial.print("Power = "); // shows the Power measured
Serial.print(Power,5); // 5 digits after decimal point
Serial.print(" W");
Serial.print(" Min = ");
Serial.print(minPower,5);
Serial.print(" W");
Serial.print(" Max = ");
Serial.print(maxPower,5);
Serial.println(" W");
Serial.print("Energy = "); // shows the Energy measured
Serial.print(Energy,5); // 5 digits after decimal point
Serial.print(" Wh");
Serial.print(" Min = ");
Serial.print(minEnergy,5);
Serial.print(" Wh");
Serial.print(" Max = ");
Serial.print(maxEnergy,5);
Serial.println(" Wh");
Serial.println("Current per hour = " + String(CurrentHour) + " Ah");
Serial.println("====================================================");
}
void setup(){
pinMode(max471VT, INPUT);
pinMode(max471AT, INPUT);
Serial.begin(115200);
minVoltage = 9999;
maxVoltage = 0;
minCurrent = 9999;
maxCurrent = 0;
minPower = 9999;
maxPower = 0 ;
minEnergy = 9999;
maxEnergy = 0;
}
void loop(){
unsigned long milisec = millis(); // calculate time in milliseconds
long time = milisec/1000; // convert milliseconds to seconds
int RawVT = analogRead(max471VT);
int RawAT = analogRead(max471AT);
float Vcc = readVcc() / 1000.0; // convert mV to V
Voltage = ((RawVT * Vcc) / 1023) * 5 ; // scale the ADC
maxVoltage = max(Voltage, maxVoltage);
minVoltage = min(minVoltage, Voltage);
Current = (RawAT * Vcc) / 1023; // scale the ADC
maxCurrent = max(maxCurrent, Current);
if (Current > 0) {
minCurrent = min(minCurrent, Current);
}
Power = Voltage * Current;
maxPower = max(maxPower, Power);
if (Power > 0) {
minPower = min(minPower, Power);
}
Energy = (Power * time) / 3600; // Watt-sec is again convert to Watt-Hr by dividing 1hr (3600sec)
maxEnergy = max(maxEnergy, Energy);
if (Energy > 0) {
minEnergy = min(minEnergy, Energy);
}
// Energy = (Power * time) / (1000*3600); for reading in kWh
SumCurrent = SumCurrent + Current; // calculate total current
AvgCurrent = SumCurrent / time; // average current
CurrentHour = (AvgCurrent * time) / 3600; // Ah
if (milisec - previousMillis >= interval) {
previousMillis = milisec;
showData();
}
}