Computing AC voltage & phase difference(HELP)

Hey everyone! I've connected the input based on this link from the building blocks: http://openenergymonitor.org/emon/buildingblocks/measuring-voltage-with-an-acac-power-adapter

I'm able to read the value from the arduino and I know you have to compute the values based on this link: Arduino Playground - DirectMathVoltmeter

  1. The value from analog input on the arduino needs to divide 1023 then *5 and afterwards I have to divide back the Resistors 1 & 2 to get the values before the voltage dividing. What comes after that? What about the transformer AC-AC power adapter? Shouldn't I need another math to step back up the values to get the final measurement?

  2. I've looked at this link:http://openenergymonitor.org/emon/buildingblocks/arduino-sketch-voltage-and-current

and I want to ask if it is the measurement of the phase difference of the two inputs current & voltage?

Here is my code:

const int analogIn0 = A0;
const int analogIn1 = A1;
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawCurrentValue= 0;
int RawVoltageValue= 0;
int ACSoffset = 2500;
int Denominator = 0;
float AcVoltage = 0;
float Voltage0 = 0;
float Voltage1 = 0;
float Amps = 0;

void setup(){
Serial.begin(9600);
}

void loop(){

RawCurrentValue = analogRead(analogIn0);
RawVoltageValue = analogRead(analogIn1);

Denominator = (10000)/(130000);

Voltage0 = (RawCurrentValue / 1023.0) * 5000;
Voltage1 = (RawVoltageValue / 1023.0) * 5000;
AcVoltage = (Voltage1 / Denominator ) ;
Amps = ((Voltage0 - ACSoffset) / mVperAmp);

Serial.print(Amps);
Serial.print(", ");
Serial.println(AcVoltage);

}

If it is, how do I do the measurement on this code?

Why not use the library that is mentioned in your first link? Or, at least look at it.

Do you have a multimeter? You'll most-likely have to to do some calibration because output of a transformer is usually not exactly "as specified". The voltage is usually specified at the rated current load and there's tolerance on top of that. ("Calibration" is simply a matter of adding a calibration factor in your sketch.)

  1. The value from analog input on the arduino needs to divide 1023 then *5 and afterwards I have to divide back the Resistors 1 & 2 to get the values before the voltage dividing. What comes after that? What about the transformer AC-AC power adapter? Shouldn't I need another math to step back up the values to get the final measurement?

Yes, you'll have to multiply by the inverse of the step-down AC step-down transformer. i.e. If 240V in gives 12V out, that's 20:1 and you'll have to multiply by 20. Of course, all of that can be put-into one expression (equation).

And, don't forget to subtract the 2.5V (512 count) bias. It's usually best to subtract 512 from the raw ADC reading before converting to a voltage.

Worst case, you can compare your DMM AC voltage reading to the minimum & maximum ADC readings and determine the ratio experimentally.

Do you have a current transformer or something for measuring current?

There's no need to measure phase unless you have 3-phase power, or unless you're measuring the phase of the current relative to the voltage.

If you want to measure power consumption, you do need to measure current. And if there's a possibility of an inductive load, you need to know the voltage/current phase relationship. in order to accurately calculate power.

Thanks for your reply!
I have to both subtract 2.5V from the current & the voltage? In other words, I have to subtract 512 from both the raw readings from both A0 & A1?

Is the AC voltage the RMS value from Serial.println(AcVoltage); ?
Is the ACSoffset= 2500 to minus for the current sensor, so it is different from the 2.5V?

Sorry, just want to clarify. Thanks!

Addon: Currently I'm getting ~3.9-4.0 RMS for current but the correct value should be around 1? I don't think the calculation is wrong either? What do you think is the problem?

When you use an virtual ground (mid-point = 2.5V), this offset (1024/2) should be subtracted from the ADC readings, yielding values in the range -512 to +511, corresponding to +-2.5V (ARef/2). Note: the result must be stored in a signed variable. You can measure the mid-point voltage using another analog input, and subtract exactly that value from the other ADC readings.

When you multiply the instantaneous current and voltage readings, you have to integrate over a half or full AC period. Numeric integration can be performed by summing up all current*voltage products over a full AC period, weighted by the sample rate. The sample rate determines the time frame (duration, dt) that applies to every single product. More samples yield a higher sum, so that (simplified) the sum can be divided by the number of samples per AC period. RMS correction is not required then, this is done by the integration.

The products can become negative with inductive/capacitive loads, where current and voltage are not in phase. Then you can sum up the products, or you can multiply the voltage and current sums, depending on which kind of power (apparent/real/reactive) you want to measure. When you sum up the voltage or current readings separately, the result over a full AC period should always be zero. That's why then you have to sum up the squares of the values, and take the sqrt from the product.

DrDiettrich:
When you use an virtual ground (mid-point = 2.5V), this offset (1024/2) should be subtracted from the ADC readings, yielding values in the range -512 to +511, corresponding to +-2.5V (ARef/2). Note: the result must be stored in a signed variable. You can measure the mid-point voltage using another analog input, and subtract exactly that value from the other ADC readings.

When you multiply the instantaneous current and voltage readings, you have to integrate over a half or full AC period. Numeric integration can be performed by summing up all current*voltage products over a full AC period, weighted by the sample rate. The sample rate determines the time frame (duration, dt) that applies to every single product. More samples yield a higher sum, so that (simplified) the sum can be divided by the number of samples per AC period. RMS correction is not required then, this is done by the integration.

The products can become negative with inductive/capacitive loads, where current and voltage are not in phase. Then you can sum up the products, or you can multiply the voltage and current sums, depending on which kind of power (apparent/real/reactive) you want to measure. When you sum up the voltage or current readings separately, the result over a full AC period should always be zero. That's why then you have to sum up the squares of the values, and take the sqrt from the product.

Do you mean that my reading and calculations are correct? Sorry, I don't quite understand.. Basically I need to divide it by the correct sample to get the correct reading? I have a 230AC with load 229ohms so I should be getting 1A but instead I got 3.9A?

Hi,

Why not use the library that is mentioned in your first link? Or, at least look at it.

To quote post #1, it has a library that by the looks of it all the cals and measurements to give current, voltage and PHASE.

What are you using to measure the current?

Tom... :slight_smile:

lamela:
Do you mean that my reading and calculations are correct? Sorry, I don't quite understand.. Basically I need to divide it by the correct sample to get the correct reading? I have a 230AC with load 229ohms so I should be getting 1A but instead I got 3.9A?

The output of a single (instantaneous) value is meaningless, as shown in above code. It can be everything between the min and
max values. You have to consider all values over one AC phase for meaningful results.

It's okay if you want to do it your way, else a look into the library code could be inspiring.

Thanks for the replies!
I think from the link it requires an emonTx to work? I'm using ACS712 for the current sensor.
Ok. I'm getting values ranging from -6 to +5 from the AC signal after the calculations. But if I take the RMS, I will get 3.9 to 4 from the reading. What can be done to solve this problem?
I've already double checked the hardware connection and software(calculations) backward and forth and the math but I can't find anything that's wrong!
Would really appreciate if you guys can help me with this! Thanks!

An amplitude of 11 (peak-peak) is equivalent to a RMS of 3.93. Where's the problem?

DrDiettrich:
An amplitude of 11 (peak-peak) is equivalent to a RMS of 3.93. Where's the problem?

But the correct value should be around 1? If I take 230/229ohms= 1.004(to the current sensor), so the value I get should be near to 1. Are there anything wrong with my codes? I'm taking the reading from Analog input 0 to divide 1023 then * 5000. Then I minus the 2500 offset and divide 185.

I've serial printed the 3 values, the RawCurrentValue, the Millivolt and the Current reading. I verified that the Millivolt values are correct, so I assume the rawcurrentvalue shouldn't be the problem though.. I should be getting a current close to 1.
The values of the RawCurrentValue is ranging from 290-705. Millivolt is ranging from 1400-3450 and current is from -5.96 to 5.16.
I don't think the calculation to measure the current is wrong either? What should I do?

currentproblem.png

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom... :slight_smile:

Here is the connection! I still can't find a solution to it.. I'm also thinking there's something with the calculation. Since the ADC conversion is analog to digital so it actually converts the input to digital. Since for 230/229=1A and I read about 0.83VRMS, 0.83 into analog input and raw value is being read.
Then I divide it by 1023 and * 5 to convert to digital. I then minus the offset of 2.5V and divides the ACS712 current sensor sensitivity of 0.185V/A.
I realized if I divide it by 4, I can get the correct current value. What do you think is the problem?
Really need help from you guys! Thank you!

ACS-712-Basic-Hook-Up.png

Hi,

Please!!!!!
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

If your voltage circuit is like the one you linked in post #1 you are measuring a varying voltage.
The analog input measures instantaneous voltage, that is for a fraction of a second when you do an analogRead it samples and converts the voltage on the input to a number between 0 and 1023.

So your digital values from the analog inputs will be varying even for constant values of AC volts and current because you are sampling at random parts of the AC waveform.

You can modify your input circuits and measure peak volts and current, then knowing it is a sinewave calculate the RMS value.

This however will not give you phase difference.

To do that you need to measure when the volt AC waveform and the current AC waveform cross the zero volts/current level.

Tom.... :slight_smile:

The ACS712 also can be tested with DC current. Is your calculation okay then?

The division by 1023 for ADC conversion is a common error. The reading should be divided by 1024, the number or ADC steps.