openenergymonitor / EmonLib

Electricity monitoring library - install in Arduino IDE's libraries folder then restart the IDE
openenergymonitor.org
GNU Affero General Public License v3.0
596 stars 422 forks source link

High reading at the beginning #75

Open hayderismael opened 1 year ago

hayderismael commented 1 year ago

I have a problem measuring the Amps and kWhs for a few devices. The lib is so precise; nevertheless, after a while. In the beginning, I received a very high reading, and it kept reduced for a few cycles of the code until it settled to the precise value. Please advise.

Thank you.

peteDDD commented 9 months ago

I am experiencing the same issue. Here is an example of my data for Vrms on a 120VAC 60hz line. My calls are

emon1.voltage(VOLTAGE_PIN, CALIBRATION_VOLTAGE, 1.7)
emon1.calcVI(30,2000)

and here is my data, collected using double-buffering (into two buffers) and freeRTOS tasks, one for collecting the data and one for formatting and transmitting it. Each buffer collects 60 samples of 30 zero-crossings. The processor is an ESP32-S2 which has a 13-bit ADC (odd one in the ESP32 family). It is running at 240MHz. I experimentally found the zero crossing to be around 5080 ADC counts out of the 8191 total counts. So, I modified the zero crossing code as follows:

#define OFFSET_COUNT 5080

 // if ((startV < (ADC_COUNTS*0.55)) && (startV > (ADC_COUNTS*0.45))) break;  //check its within range
 if(startV < (OFFSET_COUNT * 1.01) && (startV >(OFFSET_COUNT *.99))) break;

This always finds the zero cross and never times out.

The data really is "all over the place". Suggestions?

jsonPayload = [202.36,207.04,204.42,204.58,204.11,197.25,196.75,195.85,196.28,202.12,194.21,199.55,196.19,196.18,192.40,196.45,192.51,189.70,187.56,192.37,192.68,197.98,192.90,192.92,188.10,191.15,191.23,192.68,190.68,187.25,182.89,187.23,179.76,179.20,184.23,185.53,186.32,190.52,181.94,179.35,180.94,175.87,185.37,176.22,187.83,178.59,179.37,177.65,169.26,171.55,184.82,175.99,177.49,176.13,179.60,175.19,175.88,175.01,176.00,178.54] jsonPayload = [178.95,178.73,167.17,174.13,169.15,167.00,174.95,168.27,171.48,172.97,172.02,166.65,169.44,163.00,165.33,174.17,157.81,160.53,165.53,164.59,171.21,162.92,161.55,161.96,166.52,164.31,166.81,160.37,155.86,158.92,162.05,160.01,161.94,161.75,160.87,157.65,155.46,164.46,157.27,161.30,146.14,166.31,149.56,156.16,155.73,149.56,154.96,151.46,150.04,146.32,148.66,156.74,151.79,144.44,157.80,150.40,151.88,157.49,152.02,151.68] jsonPayload = [146.02,147.17,142.78,142.74,140.22,143.21,152.82,146.29,140.65,146.33,142.48,148.87,144.78,144.33,147.51,150.55,148.85,144.35,143.15,148.61,139.87,142.09,146.45,143.20,154.86,141.16,139.90,142.43,138.82,150.43,141.49,142.40,145.36,147.64,143.99,145.77,145.47,142.04,136.57,134.35,139.22,134.03,143.82,140.02,138.39,142.15,143.30,143.84,135.95,141.61,139.65,139.01,141.85,142.53,140.13,140.76,142.70,137.92,141.00,134.13] jsonPayload = [135.76,138.38,136.53,133.57,139.09,137.12,133.53,131.51,134.40,135.74,132.56,133.50,135.83,131.14,131.41,141.12,132.41,129.55,129.30,137.33,132.29,137.85,137.17,132.49,128.87,133.46,130.94,134.24,138.21,130.14,132.28,132.38,130.79,130.34,130.55,129.52,133.08,136.06,134.96,134.03,137.98,130.93,132.58,127.52,126.12,131.50,133.73,132.63,124.59,124.71,125.15,131.38,131.87,135.70,135.85,131.78,127.81,130.53,128.30,128.41] jsonPayload = [125.78,131.92,125.57,126.13,132.20,133.93,124.51,133.90,136.84,126.44,132.61,129.57,127.82,130.01,134.26,132.23,125.34,129.54,125.18,131.73,131.89,124.27,124.88,124.75,123.71,130.66,130.50,125.12,129.13,126.07,124.70,127.34,129.44,127.68,126.73,126.25,122.39,126.75,120.17,126.87,132.80,130.39,132.67,123.35,124.90,120.86,124.03,128.26,129.48,124.02,126.76,124.59,124.01,124.05,126.58,127.88,122.15,125.74,127.04,125.15] jsonPayload = [129.77,131.52,133.33,127.67,122.44,120.13,125.60,119.21,127.80,124.19,119.35,122.83,125.91,127.79,123.27,117.16,127.55,127.39,126.72,127.04,122.38,123.16,123.01,121.02,126.77,121.82,119.30,130.32,127.23,126.80,123.78,124.89,120.18,124.80,125.98,124.94,122.53,125.66,125.87,124.80,122.42,121.85,128.73,118.43,123.98,121.23,124.47,121.57,123.64,119.34,120.51,120.23,119.17,128.06,125.78,127.24,125.95,118.55,116.31,122.76] jsonPayload = [119.10,127.64,124.51,120.73,116.40,118.65,125.03,121.32,116.83,120.18,125.48,125.34,125.97,118.28,122.83,119.66,118.47,121.30,119.16,123.20,125.97,123.59,124.58,116.35,119.83,121.81,126.24,121.15,118.89,117.36,116.08,123.69,126.95,119.06,122.84,124.02,124.73,118.98,122.46,120.00,116.72,117.82,127.88,115.69,121.91,118.17,117.57,117.55,125.45,120.12,122.89,126.17,118.92,123.37,123.29,117.34,120.68,118.33,119.60,118.54] jsonPayload = [118.51,123.91,124.47,121.30,117.94,118.33,118.46,117.05,122.31,118.26,125.32,123.92,119.54,124.10,120.71,115.72,116.85,116.97,123.60,118.27,121.43,119.87,115.31,117.03,115.29,120.94,121.32,119.21,116.50,122.12,124.05,125.48,125.93,125.06,124.68,122.56,120.24,124.39,123.19,120.22,121.51,119.36,116.44,115.28,116.64,120.56,116.62,118.67,121.95,125.64,120.13,120.76,120.18,121.02,118.82,116.61,124.95,121.73,122.05,118.95] jsonPayload = [123.81,119.46,122.24,124.01,123.70,118.15,118.36,118.39,119.53,122.15,118.77,118.45,116.08,116.59,121.01,119.65,120.99,122.09,124.64,123.36,122.37,119.16,120.77,117.60,120.56,118.78,114.38,123.12,119.04,121.37,126.02,124.63,122.07,123.17,124.63,115.21,118.18,118.40,117.25,122.07,121.50,119.88,121.45,119.01,120.81,120.85,121.13,117.76,117.79,126.42,124.14,125.36,121.32,122.45,121.87,122.10,118.83,119.05,118.05,121.83] jsonPayload = [125.73,117.25,121.30,117.76,115.67,117.10,117.88,115.98,119.83,114.77,122.81,121.84,120.50,114.31,119.92,117.67,118.99,115.24,123.27,123.92,122.35,118.50,122.63,123.41,121.71,116.10,120.79,123.99,116.59,116.37,120.21,119.42,116.29,120.68,124.15,123.29,118.64,114.34,118.54,118.70,116.95,119.96,122.60,121.65,120.47,123.89,116.61,117.19,124.67,122.91,117.51,123.53,120.79,120.59,120.18,121.66,113.21,122.79,117.78,119.02] jsonPayload = [124.94,123.38,116.92,117.37,118.89,119.69,119.07,116.45,117.92,120.37,126.97,117.13,117.85,119.52,119.96,119.71,122.99,117.93,122.94,116.62,121.20,121.20,122.46,120.27,123.44,117.72,115.20,117.26,120.24,117.38,116.15,121.22,116.24,118.14,116.82,115.45,124.40,125.32,120.45,114.85,119.46,119.56,120.34,119.15,116.29,122.72,121.94,123.40,122.82,115.33,117.37,114.18,114.50,122.13,122.09,116.49,115.02,119.22,122.30,117.98] jsonPayload = [125.82,117.18,116.43,116.46,115.13,119.73,122.43,122.75,116.38,124.37,124.00,117.03,117.95,115.05,118.40,119.46,122.27,114.62,117.41,116.81,115.33,119.06,119.79,117.15,122.72,118.53,118.24,116.47,118.94,123.41,120.84,122.58,122.33,123.28,117.28,115.37,116.29,122.24,122.95,117.70,115.08,118.91,123.34,122.80,122.23,122.19,118.54,121.47,120.27,115.45,112.56,123.01,122.01,115.65,115.49,118.64,115.23,122.81,122.57,120.88] jsonPayload = [120.95,124.33,121.80,124.46,115.21,118.70,114.38,120.75,120.08,122.52,119.00,116.06,119.19,117.75,117.23,122.19,115.64,121.98,115.17,115.18,122.01,123.15,120.27,125.16,119.63,124.79,124.70,119.00,119.14,120.59,127.01,115.38,123.49,119.53,122.52,122.00,117.76,117.97,117.17,115.38,114.60,125.16,123.31,126.33,123.24,115.56,116.68,119.46,120.76,115.30,113.61,114.52,121.72,121.22,115.09,119.93,114.98,117.91,116.22,121.10] jsonPayload = [125.88,121.78,118.90,119.81,120.90,121.49,121.25,116.25,117.65,120.69,122.93,123.51,117.37,116.08,112.31,122.65,122.04,121.28,122.06,117.54,120.35,125.82,124.32,120.89,124.46,124.21,121.09,123.86,115.33,116.64,115.22,117.27,112.93,115.23,116.55,120.13,116.73,120.32,120.14,123.36,119.53,119.76,118.18,120.75,120.91,122.65,121.68,119.48,119.18,113.61,123.07,115.11,123.07,123.83,116.94,115.45,118.36,120.91,123.10,123.63] jsonPayload = [115.71,117.52,123.79,120.02,121.11,124.42,118.45,114.86,122.40,118.74,115.84,116.94,113.28,116.77,116.63,121.56,123.33,122.70,122.99,121.56,115.33,126.55,125.23,123.72,120.51,119.83,123.72,121.36,118.88,117.33,117.89,122.64,112.69,115.76,115.30,113.17,122.13,121.36,118.41,124.17,125.55,118.56,120.00,115.13,115.32,116.84,121.02,114.32,121.29,122.45,120.78,120.82,117.33,120.06,118.67,116.83,118.25,116.86,114.53,113.59] jsonPayload = [116.20,114.33,121.77,117.18,122.34,116.44,117.63,116.96,120.10,118.73,125.66,122.81,121.63,121.66,120.38,122.08,123.29,119.00,122.95,116.49,116.86,120.30,122.62,120.00,116.15,114.23,114.84,118.62,118.36,122.57,122.13,123.56,124.31,116.44,116.00,114.36,117.06,116.91,123.09,117.34,122.35,123.79,121.23,122.18,116.47,121.71,126.58,123.06,125.55,117.93,117.89,118.19,121.03,120.98,115.78,123.01,120.58,122.75,115.49,124.68] jsonPayload = [121.13,118.75,121.29,119.26,122.12,125.06,122.41,121.99,123.58,122.92,115.63,116.69,119.01,113.95,120.91,122.25,124.59,122.33,120.66,116.19,124.39,121.69,123.79,115.20,118.74,116.86,117.57,116.32,116.87,124.24,122.91,124.66,118.13,120.57,121.89,120.44,124.49,126.20,116.18,118.61,115.25,112.88,110.94,114.85,112.76,119.29,120.10,116.11,119.04,122.36,122.05,117.12,120.53,117.01,123.45,125.27,110.86,122.25,123.14,122.87] jsonPayload = [114.71,116.31,115.35,121.00,115.11,115.76,121.01,119.23,117.45,117.90,123.09,122.02,117.50,116.92,119.55,120.94,122.45,120.52,120.08,119.39,119.26,124.38,120.61,122.04,120.26,122.88,123.95,126.28,123.43,125.61,120.63,120.95,120.21,112.49,117.83,118.94,113.45,117.19,116.92,121.46,116.23,118.14,118.73,122.46,115.28,115.27,115.56,122.60,121.95,125.46,124.89,122.29,124.40,122.63,115.75,118.07,115.74,123.40,121.29,125.38] jsonPayload = [119.03,116.77,123.68,127.89,123.47,120.75,116.76,120.93,119.51,120.52,118.79,119.93,121.67,123.94,116.44,120.14,116.90,116.48,122.98,121.56,119.42,121.57,120.69,125.73,124.38,125.00,125.24,123.18,124.40,120.05,115.51,114.34,116.68,121.38,118.65,114.21,115.70,116.29,119.75,122.79,120.32,119.22,123.14,121.91,124.84,127.15,120.11,122.14,122.16,124.33,123.56,124.55,118.29,118.33,119.92,121.25,124.67,121.35,122.56,123.88] jsonPayload = [115.44,117.50,119.55,121.81,118.46,121.69,120.32,121.79,115.92,118.77,119.91,122.54,116.88,116.98,119.11,120.38,122.48,117.04,121.70,117.35,121.55,125.03,122.62,122.15,120.70,115.85,118.26,115.56,114.30,121.91,123.78,123.43,121.38,124.49,120.53,117.73,121.43,122.01,114.96,118.42,121.24,123.29,122.21,124.71,122.86,123.42,118.75,117.22,122.97,122.57,122.77,120.12,113.46,115.53,122.38,124.57,123.13,123.86,123.86,121.14] jsonPayload = [113.27,122.56,118.38,120.08,123.28,121.61,121.11,114.32,117.45,117.06,118.36,119.82,117.63,115.70,119.40,119.36,122.67,117.66,121.53,125.51,122.85,116.36,121.31,118.02,122.30,117.31,122.21,121.80,122.44,126.29,122.48,117.78,117.44,116.33,114.54,116.81,122.85,125.07,119.91,119.18,121.06,115.53,118.69,117.31,117.06,113.24,115.56,116.45,121.65,123.13,118.31,121.71,121.35,120.49,112.28,116.71,116.53,120.58,121.22,118.09] jsonPayload = [121.47,123.64,119.01,116.50,118.07,117.37,117.29,115.04,114.45,117.66,119.03,118.44,124.92,120.59,118.39,122.35,118.57,122.03,122.47,116.93,115.91,115.76,117.10,114.70,116.71,119.34,123.96,125.89,117.13,118.36,120.60,122.78,122.11,122.66,122.64,117.27,115.82,121.06,121.82,122.77,123.00,123.21,116.07,118.39,119.93,121.79,119.78,122.90,116.51,116.54,121.55,114.04,116.03,116.56,116.49,121.64,117.13,115.69,116.33,114.46]

peteDDD commented 9 months ago

And for those who prefer a graphical view

image

peteDDD commented 9 months ago

And, I'm pretty sure my power line is not THAT noisy.

hayderismael commented 9 months ago

ّI have precisely the same form shape when I start running my system. It might say that it is just for a few seconds, but I have a big load, and it adds few extra GBPs £ for consuming nothing.

peteDDD commented 9 months ago

I dug into this a bit more and now understand why the high readings at first... It has to do with the calculation in the library which tries to find the vOffset for each reading. It starts with an assumption that the crossing voltage is at exactly half one half of the ADC range: offsetV = ADC_COUNTS>>1; That's fine, if it were correct. In my case though, the zero-crossing ADC count is about 5080 (with a 13-bit ADC that has a top count of 8192). So it is more like 62% of the ADC range. The code then does a "low-pass filter" to get closer to the zero-crossing offset point with each voltage reading:

    //-----------------------------------------------------------------------------
    // B) Apply digital low pass filters to extract the 2.5 V or 1.65 V dc offset,
    //     then subtract this - signal is now centered on 0 counts.
    //-----------------------------------------------------------------------------
    offsetV = offsetV + ((sampleV-offsetV)/ADC_COUNTS);
    filteredV = sampleV - offsetV;

So, overtime, it does in fact get closer but I believe it still tends to "drift" that center point.

So I changed the initial offset to my experimentally (on the bench with oscilloscope) zero-crossing count.

 #define OFFSET_COUNT 5080 // experimentally found the crossing point using 3.3 V supply
// (I actually maintain all of these defines in a separate settings.h file)

//offsetV = ADC_COUNTS>>1;
  offsetV = OFFSET_COUNT;  // PD

image

Now, we start off in about the right place, but the noise is still very high...

I am trying some alternatives and will report back.

razarahil commented 2 days ago

I dug into this a bit more and now understand why the high readings at first... It has to do with the calculation in the library which tries to find the vOffset for each reading. It starts with an assumption that the crossing voltage is at exactly half one half of the ADC range: offsetV = ADC_COUNTS>>1; That's fine, if it were correct. In my case though, the zero-crossing ADC count is about 5080 (with a 13-bit ADC that has a top count of 8192). So it is more like 62% of the ADC range. The code then does a "low-pass filter" to get closer to the zero-crossing offset point with each voltage reading:

    //-----------------------------------------------------------------------------
    // B) Apply digital low pass filters to extract the 2.5 V or 1.65 V dc offset,
    //     then subtract this - signal is now centered on 0 counts.
    //-----------------------------------------------------------------------------
    offsetV = offsetV + ((sampleV-offsetV)/ADC_COUNTS);
    filteredV = sampleV - offsetV;

So, overtime, it does in fact get closer but I believe it still tends to "drift" that center point.

So I changed the initial offset to my experimentally (on the bench with oscilloscope) zero-crossing count.

 #define OFFSET_COUNT 5080 // experimentally found the crossing point using 3.3 V supply
// (I actually maintain all of these defines in a separate settings.h file)

//offsetV = ADC_COUNTS>>1;
  offsetV = OFFSET_COUNT;  // PD

image

Now, we start off in about the right place, but the noise is still very high...

I am trying some alternatives and will report back.

Did you fix the issue and manage to get stable reading?