-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmonLib_ADS1x15.cpp
More file actions
387 lines (318 loc) · 16.4 KB
/
EmonLib_ADS1x15.cpp
File metadata and controls
387 lines (318 loc) · 16.4 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
Emon.cpp - Library for openenergymonitor
Created by Trystan Lea, April 27 2010
GNU GPL
modified to use up to 12 bits ADC resolution (ex. Arduino Due)
by boredman@boredomprojects.net 26.12.2013
Low Pass filter for offset removal replaces HP filter 1/1/2015 - RW
*/
// Proboscide99 10/08/2016 - Added ADMUX settings for ATmega1284 e 1284P (644 / 644P also, but not tested) in readVcc function
//#include "WProgram.h" un-comment for use on older versions of Arduino IDE
#include "EmonLib_ADS1x15.h"
#include <Adafruit_ADS1X15.h>
//--------------------------------------------------------------------------------------
// Sets the pins to be used for voltage and current sensors
//--------------------------------------------------------------------------------------
void EnergyMonitor::voltage(unsigned int _ads, bool _init, adsGain_t gain, uint16_t datarate, double _VCAL, double _PHASECAL)
{
VCAL = _VCAL;
PHASECAL = _PHASECAL;
offsetV = ADC_COUNTS>>1;
if (_init) {
ads.setGain(gain);
ads.setDataRate(datarate);
ads.begin(_ads);
}
}
void EnergyMonitor::current(unsigned int _ads, bool _init, adsGain_t gain, uint16_t datarate, double _ICAL)
{
ICAL = _ICAL;
offsetI = ADC_COUNTS>>1;
// ads.setGain(GAIN_ONE);
ads.setGain(gain);
ads.setDataRate(datarate);
ads.begin(_ads);
}
//--------------------------------------------------------------------------------------
// emon_calc procedure
// Calculates realPower,apparentPower,powerFactor,Vrms,Irms,kWh increment
// From a sample window of the mains AC voltage and current.
// The Sample window length is defined by the number of half wavelengths or crossings we choose to measure.
//--------------------------------------------------------------------------------------
void EnergyMonitor::calcVI(unsigned int pinI, unsigned int pinV, unsigned int crossings, unsigned int timeout)
{
int SupplyVoltage=3300;
unsigned int crossCount = 0; //Used to measure number of times threshold is crossed.
unsigned int numberOfSamples = 0; //This is now incremented
filteredV = 0;
//-------------------------------------------------------------------------------------------------------------------------
// 1) Waits for the waveform to be close to 'zero' (mid-scale adc) part in sin curve.
//-------------------------------------------------------------------------------------------------------------------------
unsigned long start = millis(); //millis()-start makes sure it doesnt get stuck in the loop if there is an error.
while(1) //the while loop...
{
// startV = analogRead(inPinV);
switch (pinV) {
case 0 ... 3:
startV = ads.readADC_SingleEnded(pinV); //using the voltage waveform readADC_SingleEnded
break;
case 10:
startV = ads.readADC_Differential_0_1(); //using the voltage waveform
break;
case 32:
startV = ads.readADC_Differential_2_3(); //using the voltage waveform
break;
default:
startV = ads.readADC_SingleEnded(1);
}
if ((startV < (ADC_COUNTS*0.55)) && (startV > (ADC_COUNTS*0.45))) break; //check its within range 563 > X > 460
if ((millis()-start)>timeout) break;
}
//-------------------------------------------------------------------------------------------------------------------------
// 2) Main measurement loop
//-------------------------------------------------------------------------------------------------------------------------
start = millis();
while ((crossCount < crossings) && ((millis()-start)<timeout))
{
numberOfSamples++; //Count number of times looped.
lastFilteredV = filteredV; //Used for delay/phase compensation
//-----------------------------------------------------------------------------
// A) Read in raw voltage and current samples
//-----------------------------------------------------------------------------
// sampleV = analogRead(inPinV); //Read in raw voltage signal
// sampleI = analogRead(inPinI); //Read in raw current signal
switch (pinI) {
case 0 ... 3:
sampleI = ads.readADC_SingleEnded(pinI); //using the voltage waveform readADC_SingleEnded
break;
case 10:
sampleI = ads.readADC_Differential_0_1(); //using the voltage waveform
break;
case 32:
sampleI = ads.readADC_Differential_2_3(); //using the voltage waveform
break;
default:
sampleI = ads.readADC_SingleEnded(0);
}
switch (pinV) {
case 0 ... 3:
sampleV = ads.readADC_SingleEnded(pinI); //using the voltage waveform readADC_SingleEnded
break;
case 10:
sampleV = ads.readADC_Differential_0_1(); //using the voltage waveform
break;
case 32:
sampleV = ads.readADC_Differential_2_3(); //using the voltage waveform
break;
default:
sampleV = ads.readADC_SingleEnded(1);
}
//-----------------------------------------------------------------------------
// B) Apply digital low pass filters to extract the 2.5 V or 1.65 V dc offset,
// then subtract this - signal is now centred on 0 counts.
//-----------------------------------------------------------------------------
offsetV = offsetV + ((sampleV-offsetV)/1024); // x = 0 + (600-0)/1024 = 0 + 0.5859 = 0.5859 -> x = 0.5859 + (700-0.5859)/1024 = 0.5859 + 0.6830 = 1.2689 x=1.2689 + (800-1.2689)/1024 = 1.2689+0.78 = 2.0489
// x = 0 + (600-0)/1024 = 0 + 0.5859 = 0.5859 -> x = 0.5859 + (500-0.5859)/1024 = 0.5859 + 0.4877 = 1.0736
filteredV = sampleV - offsetV; // 600-0.5859 = 599.4141 700 - 1.2689 = 698.73
// 600-0.5859 = 599.4141 500 - 1.0736 = 498.92
offsetI = offsetI + ((sampleI-offsetI)/1024);
filteredI = sampleI - offsetI;
//-----------------------------------------------------------------------------
// C) Root-mean-square method voltage
//-----------------------------------------------------------------------------
sqV= filteredV * filteredV; //1) square voltage values
sumV += sqV; //2) sum
//-----------------------------------------------------------------------------
// D) Root-mean-square method current
//-----------------------------------------------------------------------------
sqI = filteredI * filteredI; //1) square current values
sumI += sqI; //2) sum
//-----------------------------------------------------------------------------
// E) Phase calibration
//-----------------------------------------------------------------------------
phaseShiftedV = lastFilteredV + PHASECAL * sqrt(sq(filteredV - lastFilteredV));
//-----------------------------------------------------------------------------
// F) Instantaneous power calc
//-----------------------------------------------------------------------------
// InternalVoltage = filteredI;
instP = phaseShiftedV * filteredI; //Instantaneous Power
sumP +=instP; //Sum
//-----------------------------------------------------------------------------
// G) Find the number of times the voltage has crossed the initial voltage
// - every 2 crosses we will have sampled 1 wavelength
// - so this method allows us to sample an integer number of half wavelengths which increases accuracy
//-----------------------------------------------------------------------------
lastVCross = checkVCross;
if (sampleV > startV) checkVCross = true;
else checkVCross = false;
if (numberOfSamples==1) lastVCross = checkVCross;
if (lastVCross != checkVCross) crossCount++;
}
//-------------------------------------------------------------------------------------------------------------------------
// 3) Post loop calculations
//-------------------------------------------------------------------------------------------------------------------------
//Calculation of the root of the mean of the voltage and current squared (rms)
//Calibration coefficients applied.
double V_RATIO = VCAL *((SupplyVoltage/1000.0) / (ADC_COUNTS));
Vrms = V_RATIO * sqrt(sumV / numberOfSamples);
double I_RATIO = ICAL *((SupplyVoltage/1000.0) / (ADC_COUNTS));
Irms = I_RATIO * sqrt(sumI / numberOfSamples);
//Calculation power values
realPower = V_RATIO * I_RATIO * sumP / numberOfSamples;
apparentPower = Vrms * Irms;
powerFactor=realPower / apparentPower;
//Reset accumulators
sumV = 0;
sumI = 0;
sumP = 0;
//--------------------------------------------------------------------------------------
}
double EnergyMonitor::calcVrms(unsigned int pinV, unsigned int crossings, unsigned int timeout)
{
int SupplyVoltage=3300;
unsigned int crossCount = 0; //Used to measure number of times threshold is crossed.
unsigned int numberOfSamples = 0; //This is now incremented
filteredV = 0;
//-------------------------------------------------------------------------------------------------------------------------
// 1) Waits for the waveform to be close to 'zero' (mid-scale adc) part in sin curve.
//-------------------------------------------------------------------------------------------------------------------------
unsigned long start = millis(); //millis()-start makes sure it doesnt get stuck in the loop if there is an error.
while(1) //the while loop...
{
// startV = analogRead(inPinV);
switch (pinV) {
case 0 ... 3:
startV = ads.readADC_SingleEnded(pinV); //using the voltage waveform readADC_SingleEnded
break;
case 10:
startV = ads.readADC_Differential_0_1(); //using the voltage waveform
break;
case 32:
startV = ads.readADC_Differential_2_3(); //using the voltage waveform
break;
default:
startV = ads.readADC_SingleEnded(1);
}
if ((startV < (ADC_COUNTS*0.55)) && (startV > (ADC_COUNTS*0.45))) break; //check its within range 563 > X > 460
if ((millis()-start)>timeout) break;
}
//-------------------------------------------------------------------------------------------------------------------------
// 2) Main measurement loop
//-------------------------------------------------------------------------------------------------------------------------
start = millis();
while ((crossCount < crossings) && ((millis()-start)<timeout))
{
numberOfSamples++; //Count number of times looped.
lastFilteredV = filteredV; //Used for delay/phase compensation
//-----------------------------------------------------------------------------
// A) Read in raw voltage and current samples
//-----------------------------------------------------------------------------
// sampleV = analogRead(inPinV); //Read in raw voltage signal
// sampleI = analogRead(inPinI); //Read in raw current signal
switch (pinV) {
case 0 ... 3:
sampleV = ads.readADC_SingleEnded(pinV); //using the voltage waveform readADC_SingleEnded
break;
case 10:
sampleV = ads.readADC_Differential_0_1(); //using the voltage waveform
break;
case 32:
sampleV = ads.readADC_Differential_2_3(); //using the voltage waveform
break;
default:
sampleV = ads.readADC_SingleEnded(0);
}
//-----------------------------------------------------------------------------
// B) Apply digital low pass filters to extract the 2.5 V or 1.65 V dc offset,
// then subtract this - signal is now centred on 0 counts.
//-----------------------------------------------------------------------------
offsetV = offsetV + ((sampleV-offsetV)/1024); // x = 0 + (600-0)/1024 = 0 + 0.5859 = 0.5859 -> x = 0.5859 + (700-0.5859)/1024 = 0.5859 + 0.6830 = 1.2689 x=1.2689 + (800-1.2689)/1024 = 1.2689+0.78 = 2.0489
// x = 0 + (600-0)/1024 = 0 + 0.5859 = 0.5859 -> x = 0.5859 + (500-0.5859)/1024 = 0.5859 + 0.4877 = 1.0736
filteredV = sampleV - offsetV; // 600-0.5859 = 599.4141 700 - 1.2689 = 698.73
// 600-0.5859 = 599.4141 500 - 1.0736 = 498.92
//-----------------------------------------------------------------------------
// C) Root-mean-square method voltage
//-----------------------------------------------------------------------------
sqV= filteredV * filteredV; //1) square voltage values
sumV += sqV; //2) sum
//-----------------------------------------------------------------------------
// E) Phase calibration
//-----------------------------------------------------------------------------
phaseShiftedV = lastFilteredV + PHASECAL * sqrt(sq(filteredV - lastFilteredV));
//-----------------------------------------------------------------------------
// G) Find the number of times the voltage has crossed the initial voltage
// - every 2 crosses we will have sampled 1 wavelength
// - so this method allows us to sample an integer number of half wavelengths which increases accuracy
//-----------------------------------------------------------------------------
lastVCross = checkVCross;
if (sampleV > startV) checkVCross = true;
else checkVCross = false;
if (numberOfSamples==1) lastVCross = checkVCross;
if (lastVCross != checkVCross) crossCount++;
}
//-------------------------------------------------------------------------------------------------------------------------
// 3) Post loop calculations
//-------------------------------------------------------------------------------------------------------------------------
//Calculation of the root of the mean of the voltage and current squared (rms)
//Calibration coefficients applied.
double V_RATIO = VCAL *((SupplyVoltage/1000.0) / (ADC_COUNTS));
Vrms = V_RATIO * sqrt(sumV / numberOfSamples);
//Reset accumulators
sumV = 0;
sumI = 0;
sumP = 0;
return Vrms;
//--------------------------------------------------------------------------------------
}
//--------------------------------------------------------------------------------------
double EnergyMonitor::calcIrms(unsigned int pinI, unsigned int Number_of_Samples)
{
int SupplyVoltage=3300;
for (unsigned int n = 0; n < Number_of_Samples; n++)
{
switch (pinI) {
case 0 ... 3:
sampleI = ads.readADC_SingleEnded(pinI); //using the voltage waveform readADC_SingleEnded
break;
case 10:
sampleI = ads.readADC_Differential_0_1(); //using the voltage waveform
break;
case 32:
sampleI = ads.readADC_Differential_2_3(); //using the voltage waveform
break;
default:
sampleI = ads.readADC_SingleEnded(0);
}
// Digital low pass filter extracts the 2.5 V or 1.65 V dc offset,
// then subtract this - signal is now centered on 0 counts.
offsetI = (offsetI + (sampleI-offsetI)/1024);
filteredI = sampleI - offsetI;
// Root-mean-square method current
// 1) square current values
sqI = filteredI * filteredI;
// 2) sum
sumI += sqI;
}
double I_RATIO = ICAL *((SupplyVoltage/1000.0) / (ADC_COUNTS));
Irms = I_RATIO * sqrt(sumI / Number_of_Samples);
//Reset accumulators
sumI = 0;
//--------------------------------------------------------------------------------------
return Irms;
}
void EnergyMonitor::serialprint()
{
Serial.print(realPower);
Serial.print(' ');
Serial.print(apparentPower);
Serial.print(' ');
Serial.print(Vrms);
Serial.print(' ');
Serial.print(Irms);
Serial.print(' ');
Serial.print(powerFactor);
Serial.println(' ');
delay(100);
}
//thanks to http://hacking.majenko.co.uk/making-accurate-adc-readings-on-arduino
//and Jérôme who alerted us to http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/