How to calculate the V rms of the Main

Hey i am working on a project " power Factor correction " and for that i have to calculate the power factor . I am facing the problems while calculating the Vrms of the main. 
Components i am using are AC to AC adapter 230/9V for isolation and then further step downs the voltage to 0.5voltes using the potential divider. The voltage i stepped down is Ac 0.5 v which contains negative cycle with it therefore i added Dc off set of 2.5 v to it. 
 

Code :

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;
}

int value=0;
int sum_squared_voltage=0;
void setup()
{
  Serial.begin(9600);
}

void loop()

  double Vcc;
 for(int i=0;i<40;i++)
 {
 value = analogRead(0);
 }
 Vcc = readVcc()/1000;
  int voltage =(value / 1023.0) * Vcc;
 
 int number_of_samples=60;
for (int n=0; n<number_of_samples; n++)
{

  int squared_voltage = voltage *voltage;

   sum_squared_voltage += squared_voltage;
}

int mean_square_voltage = sum_squared_voltage / number_of_samples;
int root_mean_square_voltage = sqrt(mean_square_voltage);
int Vrms=root_mean_square_voltage;
 Serial.println (Vrms);
}

Description: In Programming i used " long readVcc() " function for correction of Arduino Vcc first. After that in Void loop() i used for-loop for correct Digital samples of analog signals and calculated the voltage which gives me the value of 1.10 volts which is ok as i have given my input analog pin the voltage of 0.5 volts. 

Problem Facing :
1) This do not gives me the 217 vrms as my DMM showed.
 

Queries:
1) Where and why voltage constant is multiplied.
2) In Emonlib,cpp what does this line do " offsetV = ADC_COUNTS>>1; d "  in  "  void  EnergyMonitor::voltage(unsigned int _inPinV, double _VCAL, double _PHASECAL) " function.
3) Is it necessary to extract the DC offset from AC signal before calculations for rms.

Note: I am weak in programming my questions might be simple and stupid so its a humble request to a professional to help me out and clear my problems.

References  : http://hacking.majenko.co.uk/node/57

                     http://openenergymonitor.org/emon/buildingblocks/ct-and-ac-power-adaptor-installation-                        and-calibration-theory

                     https://github.com/openenergymonitor/EmonLib/blob/master/EmonLib.cpp

Robert Wall's picture

Re: How to calculate the V rms of the Main

All you need is in emonLib. The theoretical basis of all (I think all) our code is explained in the Building Blocks section of this website.

I suggest you use the design of the emonTx, and one of the sketches for the emonTx, as your starting point. All our sketches are known to work, and can produce accurate results when correctly calibrated.

[Edit]
Now that I have had time to study you code in detail - I cannot see how it can possibly work.
1. You are reading AI0 40 times but only using the final (40th) value.
2. Next, you take that value, square it and add it to the sum 60 times.
3. Then you take the mean of that and the square root.
This must give you the value that you started with, so it is quite useless.

Question 1. How to scale the input is complicated. You need to study, understand and calculate for yourself, using Building Blocks, "CT and AC power adapter installation and calibration theory" as your guide.
Question 2. It provides an initial value for the low-pass filter.
Question 3. Yes. Make a sine wave in a spreadsheet and do the maths there to see what effect the offset has. But to calculate the average real power, you only need to remove the offset of one of the variables, either voltage or current, and not both. But if you want rms voltage and rms current separately, you must remove the offset from both.

Why are you reducing the voltage to 0.5 V rms? If you are using the 1.1 V range of the ADC, then your d.c. offset of 2.5 V is wrong and 0.5 V is too big; but if your input range is 5 V, the offset is correct but for best accuracy, you should have 5 V peak-peak (or a little less), so approx 1.6 V rms is a good value to aim for.

calypso_rae's picture

Re: How to calculate the V rms of the Main

It may be worth checking how Vrms is calculated in my Mk2i_PV_Router_rev6.ino sketch.  There is a link to this code in my Summary Page which contains the following entry:

Mk2i_PV_Router_rev6.ino  Upgraded to include datalogging via an RFM12B module.  A second current sensor is now supported so that the amount of diverted energy can be accurately monitored.  Posted, on 22/04/14 at http://openenergymonitor.org/emon/node/1912, this code is available at http://openenergymonitor.org/emon/sites/default/files/Mk2i_PV_Router_rev6.ino_.zip

This sketch does not use emonLib, so all the maths can be seen in the sketch itself.

Comment viewing options

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