EMON sample rate critical?

EMON batch timing comes from analog read of Volt and Amp 

and some calculations.

On Arduino we see a samplerate of 2.6kHz, what is about 52 samples per 50Hz sinus.

I just run it on a PIC cpu and see about 31.1kHz. I dont think we need that.

But how much is the samplerate effecting the quality of the EMON calculation?

because when i started thinking what to do with the extra time

i had the idea to change from

read Volt, read Amp, calc filter, calc phase, calc powers


read Volt, read Amp, read Volt again and make a average what creates a perfect phaseshift, 

allign Volt with Amp reading,

calc filter, calc powers.


this takes much longer but eliminates the need for phase correction of the timedelay between the two V , I samples.

on a arduino it would change the samplerate down to 2.05kHz with 41 samples per 50Hz sinus.

is this acceptable? and worth it?


stuart's picture

Re: EMON sample rate critical?

Interesting idea, does it definately take away the phase shift though ?  Even with non-resistive loads?

kllsamui's picture

Re: EMON sample rate critical?

i hope NOT!

JBecker's picture

Re: EMON sample rate critical?

There are two components that have to be compensated by the phase calibration:

- time delay between voltage and current measurement

- the individual phase shift values of the voltage sensor and the current sensor.

So it does not help too much to eliminate only one component.

mharizanov's picture

Re: EMON sample rate critical?

 I have never done this, and am not sure it will work for the purpose either, but let me shoot an idea:

How about using AVR's differential ADC mode to sample the V and I at the same time? Can it be done?


Apologies, if this is silly, just had the idea and wanted to share.

kllsamui's picture

Re: EMON sample rate critical?

@JBecker, very good, thank you,

are there any numbers for that available? how much for sample delta timer error ( cpu related ) and how much for instrumentation (VT,CT)

like a user feedback about that PHASECAL TUNING?  +-1.7

( because i remember when i did a simulation in online excel a year ago i see some danger in the used numeric formula,

with a noise of 600Hz (possible?) can be amplified by that phase shifting??) openenergymonitor.org/emon/node/236



But for this sample frequency related question:

what you would do with extra time in the batch loop?

what sample rate would you think is best? ( if free adjustable?)

kllsamui's picture

Re: EMON sample rate critical?

@mharizanov, i play arduino some time, but i can not HACK it or do AVR specific code.

But reading the difference of 2 sinus amplitudes ( V - I ) would give us what? while we actually need the product of them?

but for error correction???


JBecker's picture

Re: EMON sample rate critical?

For sampling delay compensation the PHASECAL factor is always between 0.0 and 1.0. This is because the current sampling will happen between two voltage samplings. So the mating voltage value it has to be interpolated between these two voltage samples. Factor 0.0 would mean that the current sample was taken exactly at the same time as the first voltage sample (so the value of the first voltage sample is taken for calculation) and with a factor of 1.0 the value of the second voltage sample is taken for calculation.The real value is somewhere in between. If the time interval between two voltage samples (or the periode of the complete sampling loop measuring voltage(s) and current(s)) is 400us and the delay between voltage and current sample is 100us, then the PHASECAL factor for this compensation is 100us/400us=0.25.

The question for phase lag of the voltage and current transformers is harder to answer. I would assume that they lag by less than 5° (which corresponds to ~280us @50Hz). This 5° or 280us has to be seen in relation to the periode ofthe sampling loop again.  If this is again 400us than the PHASECAL factor for this compensation would be 280us/400us=~0.7.

But for this calculation you always have to take the phase lag difference between the two sensors in question. If the voltage sensor has 5° phase lag and current sensor has 3°, then the phase lag difference is -2°!

kllsamui's picture

Re: EMON sample rate critical?

Thanks @JBecker, i have to read that slowly, not so fit in the usec and ° thinking.

the EMON samplerate ( at my arduino uno hardware ) is about 2.575kHz. = 2060/800

,this is trusting the millis() info from arduino 799..800 and the samplecounter 2040 .. 2080 in EMON.

(1/Hz  = 388usec)


When i do a read 2 Ain to array ( also in a while loop ) i have max 4.3 Khz, thats 232usec ( for 2 measurements plus loop..),

Reading only one analog input ( store to array and while loop ) i reached max 8.6kHz. is 116usec.

I have this funny thing that the samples for 800msec ( 40 fullwave 50Hz) also vary with the current reading? with higher current i get more samples in the same time???

the ARDUINO states

It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

and that would be also the time between 2 readings.

NOW i understand the 100us/400us


for the PHASE calculation

    // E) Phase calibration
    phaseShiftedV = lastFilteredV + PHASECAL * (filteredV - lastFilteredV);


if the calibration factor PHASECAL is 0 we use a 400usec old filtered V value, if it is 1 we use the new filtered V value.

but the sampling of Volt is first, the Amp reading is 100usec later! / newer!

so PHASECAL 0 means we relate a 500usec old Volt with Amp,

PHASECAL 1 means we relate a 100usec old Volt with Amp.

so to allign Volt and Amp, Volt must be shifted not backward, instead forward

i think thats what the PHASECAL factor is about:

one year ago the default was 2.3, and with new lib its 1.7 ?? but never 0.25? between 0 .. 1 ???


So using the idea V1IV2 i think PHASECAL could be more close to 1 and take care of the instrumentation problem ONLY.


update: the combination original phasecal AND V1IV2 give disturbing results,

no change in "p" but different "W".

pls see attached snap, i think i forget that idea???

 But if someone want to try again, pls find also attached code ( PIC / Arduino compatible )


Transformer phase lag ? is it depending on the LOAD? then i worry my cheap CT.

or on the frequency? what is the 60Hz country tuning for PHASECAL? different?

But first what are °?   one 50 Hz sinus has 360°, so one ° is 20msec /360 = 55usec?

in ° we could say that we measure every 8° with a sampling time error between Volt and Amp of 2°  ( 50Hz only )



 Back to the original question?

in case i do this add sampling or anything else inside the batch, the samplerate from 2.6kHz is reduced.

What is the rule for a good sample rate?

 according sampling theorem  its the question what max frequency we need to measure ?

harmonics of 50Hz, transformer, LOAD depending like switching power supplies and energy saving lamps???

i hope using the PIC i might be able to do EMON at the ARDUINO rate

AND do SCOPE and FFT on the same data.


JBecker's picture

Re: EMON sample rate critical?

so PHASECAL 0 means we relate a 500usec old Volt with Amp,

PHASECAL 1 means we relate a 100usec old Volt with Amp.


Yes, that is correct (I am not so fit with the original sketch, sorry). We had a discussion about that already. A PHASECAL factor of >1 means extrapolating a voltage value for calculation, which sould not normally be necessary.


in ° we could say that we measure every 8° with a sampling time error between Volt and Amp of 2°  ( 50Hz only )



Yes. I made some tests yesterday evening with the original sketch and found a sampling rate (for one voltage and one current sensor) of 394us (jittering by +/-10us. This corresponds to 7.09° @50Hz. The samples for voltage and current are ~105us apart and then comes a slightly variable time for the float calculations before the loop starts again.

Sampling rate:

From my experience with mains voltage and current measurements, 80-100 samples per input per full wave with a 10 bit ADC are fully adequate for a better than 1% accuracy. This is dependant on a lot of things, but should be about ok in 'real life'. Doing it more often does not harm. Modern controllers are capable of much higher sampling rates combined with higher resolution (12 bits) so that multiple TRMS measurements are not a big deal anymore.


José's picture

Re: EMON sample rate critical?

Hi, I 'm trying to find the constant phase correction for a frequency of 60Hz, taking into account the considerations in this post :
I meet some questions ,

1) Given that the sampling ADC of Arduino is 100uS , and the time that elapses between one sample and another is 400us , additional time it takes to execute each instruction (16 MIPS = 62.5nS per instruction) this is a approximate 2.5US to taking the next sample.
  If I calculate this way would not have any effect 502.5uS/100uS frequency which does not think it's right.

2) If the analysis performed on the basis of frequency would have the following:

60Hz = 16.67mS = 166.67 samples per cycle

46.3 uS by (° ) what Dated 9° error between a sample and another .

Is this correct ? .

Finally , what would the constant phase correction for 60Hz signal ? .

And I'm sorry if I ask a lot, but I have spent much time trying to fix this and my only help I have RECEIVED from here.

Robert Wall's picture

Re: EMON sample rate critical?

Sample timing is not the only consideration. Both input transformers (voltage and current) also contribute a phase shift, which varies a little from item to item (due to manufacturing tolerances and variation in the materials) but it also varies rather more with the quantity being measured - the voltage or the current (see the test reports in Building Blocks). So any calculation you make can only be approximate. The phase shift is certainly not constant, unfortunately. The best you can do is adjust the phase calibration constant at your usual voltage and current so that a purely resistive load reads maximum real power and power factor = 1.00  An error in phase makes little difference when the power factor of the combined load is near to 1 (which it should be), but becomes increasingly important as the power factor gets smaller.

José's picture

Re: EMON sample rate critical?

Excuse me, I checked the link where ye correction phase  ..http://openenergymonitor.org/emon/buildingblocks/explanation-of-the-phase-correction-algorithm
but (60Hz), the phase correction would be the same as the time between samples being the same secure (fewer samples). the values ​​1, 0 and 2 would have no correction efeto nungún simply moves right to left, or. I think this would not correct the desfease,
also exemplified 1.5, but why this value in ual if you notice that there is a corrción. But how to know that constant corresponds to 60Hz?.

"By modifying the software to report the time it takes to complete the inner measurement loop and the number of samples recorded, the time between samples was measured as 377 μs. This equates to 6.79° (360° takes 20 ms)."

I'm can calculate my phasecal with 377uS, and changing the value of 8.14 ° (360 ° Takes 16.67 ms).

Sorry I'm asking way too, but it is somewhat limited by these post are not my language, I try to understand as much.

Robert Wall's picture

Re: EMON sample rate critical?

First, I live in a 50 Hz country! I'm sorry if I do not think 60 Hz.  But as I wrote, you cannot calculate phasecal. There are three parts to it, you know only one of those parts, the time between samples. The other two parts are the phase error in the current transformer that changes with the current, and the phase error in the voltage transformer that changes as your voltage changes. I cannot generate a 60 Hz supply for testing, so I cannot measure the phase error of the AC-AC adapter nor the current transformer at 60 Hz. All I can say for sure is the error will not be the same as it is at 50 Hz, because it depends on the losses in the transformers and those will depend on frequency.

This is a real picture of the output of a current transformer (red trace) and the current (green trace).

You can see phase error of the transformer very clearly. The output is ahead of the input by about 500 µs.

(The flat top of the green current wave really is the shape of the mains voltage, the slope on the top of the red output wave is because the transformer has a poor frequency response below 50 Hz).


José's picture

Re: EMON sample rate critical?

Ok, I understand ... "you only know One of Those parts, the time between samples". That time is about 400us.

Is Necessary to do some tests with a resistive load and watch the behavior of waves, the total phase shift would be, the sum of the mismatch that would get the testing, but the phase shift caused by the time between samples.

How to reach the end value once you test?. What should I do with the rebound?. For a constant as shown in the examples of the Emonib library (phasecal = 1.7).

Robert Wall's picture

Re: EMON sample rate critical?

I am sorry, your message has suffered in translation.

Phasecal = 1.7 is only an approximate value to begin with.  To set phasecal, you must use a resistive load, for example an electric heater, and adjust phasecal until the real power is maximum and power factor = 1.00.

The complete calibration procedure is here.

José's picture

Re: EMON sample rate critical?

Excuse me, for the translation.

Clearly I should test and see waveforms with resistive load, however there are other codes like this, where the phase calibration is performed otherwise. I'll have to review and interpret what is being done, I do not dare to comment on the code or because not enough studied.

Thanks for the help!

José's picture

Re: EMON sample rate critical?

Greetings dear friend, spent the days studying much about this, but I think now I miss classes to ask questions :-) I never did check on digital filters, here, there is no better explanation than this. One question remains for me on this. What have cutoff frequency in the high-pass filter?.

Reading a bit I tried to answer the same question, though I'm not sure why I think that some experts as you can help me.

"Why 0.996? It is not a magic number, all that is required is a number reasonably close to unity in order to provide an adequately long time constant so that there is little phase and amplitude distortion at the 50 Hz fundamental frequency being measured. 0.996 gives a filter time constant of 250 sample periods."

This means that the time constant (tau) = 50mS. This taking into account a sampling period = 200uS (voltage or current). Knowing that the cutoff frequency is inversely proportional to the time constant (tau). You can calculate the cutoff frequency from the following equation.

f_c= 1 / 2 π τ = 3.18Hz      Is this analysis right?.

Another thing, I went to read this link and there is A round table in the sampling period, I think I posted much science there I became confused ...

We agree that the Seller EmonLib we loop due to the time taken for sampling is 400US for each pair of samples (current and voltage), 200uS for a sample of a sample of current or voltage. This sampling rate is due to the reading Arduino 100uS takes a sample of the ADC. However, we must consider the time it takes to perform the mathematical calculations. In any case will be greater than always 400uS for sample pairs. But here we speak of 364uS, by changing the lines.

"I have now made some experiments with your original sketch 'emonTx_CT123_Voltage'. I found that the period for measuring one voltage sensor and one CT is around 394us. This is jittering by +/-10us due to different calculation times from one loop to the other.

Then I tried to speed this up. One small change brings the period down to 364us, an improvement of ~8%. This is by just changing the line:

filteredV = 0.996*(lastFilteredV+sampleV-lastSampleV);


  filteredV = 0.996*(lastFilteredV+(sampleV-lastSampleV));"

Lines do not understand how are you able to accelerate the rate the Arduino sampling for 364uS for paired samples . Although I think I'm misinterpreting this.

Another thing , if you avoid working in float, and is used only integers reduces even more the delay , so that the filter is applied as follows .

long shiftedFCL = shifted_filter + (long)((sample-last_sample)<<8);
  shifted_filter = shiftedFCL - (shiftedFCL>>8);
  long filtered_value = (shifted_filter+128)>>8;

If a delay of 400us Nyquist ( 2.5Kz ) is satisfied, we can work on float, with the original code....

"filteredV = 0.996*(lastFilteredV+(sampleV-lastSampleV));
    filteredI = 0.996*(lastFilteredI+(sampleI-lastSampleI));"

This would give an acceptable result..

Thanks your support is always very valuable.!

Robert Wall's picture

Re: EMON sample rate critical?

Taking the points you raise in sequence:

f_c= 1 / 2 π τ = 3.18Hz      Is this analysis right?.  No, the sample rate you need to use in the filter is the rate at which that quantity is being measured, not the rate for every sample. So the rate is one sample every 400 µs, half the number that you have.

[changing] filteredV = 0.996*(lastFilteredV+sampleV-lastSampleV);
filteredV = 0.996*(lastFilteredV+(sampleV-lastSampleV));"

Lines do not understand how are you able to accelerate the rate the Arduino sampling for 364uS for paired samples . Although I think I'm misinterpreting this.

No, you are correct. It is faster. It is because the processor can do the mathematics on an integer much faster than on a floating point (decimal) number. Without the extra "(    )" the processor converts sampleV and lastSampleV to floating point numbers. Then it adds the three numbers. Then it multiplies. Adding the extra "(      )" and writing "(sampleV-lastSampleV)" makes the processor do this subtraction in integer arithmetic first. The result is then converted to floating point. It is added to "lastFilteredV" (a floating point number). Then finally it is multiplied as a pair of floating point values.  This means time is saved because there is one less conversion to floating point and a faster subtraction using integers. Martin Robert's PLL sketch uses much integer arithmetic and runs very fast. But many (most?) people will not understand the full integer arithmetic, therefore for beginners it is mush better to explain the working using mathematics that they can understand. If you have a phase locked loop that runs continuously, it is most important that there is enough time available to do other things, like measuring the temperature and sending the results by radio. That is where the extra speed is very useful because it makes time to do those things and still allows 2500 samples per second with 2 c.t's and with time to spare.


José's picture

Re: EMON sample rate critical?

Thanks for the excellent explanation, and patience. I think I've been confusing these terms; rate at which that quantity is being measured with, rate for every sample...Now I understand many things

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.