# Calculating I_rms from Current Sensor

Hi, nice to meet you all here.

Working on a smart energy meter project using the Arduino Mega 2560. Currently able to sense the input current from a normal wall socket outlet using ACS712 current sensor.

I was scratching my head through the calculation of the I_rms from each cycle of a input power supply. Problems come in when:

1. As far as I concerned, calculation of I_rms= (I_peak)/(sqrt2) for half a cycle but I noticed that the result I am getting are discrete signals. Is the formula appropriate for this situation?

2. .txt file attached bellow is the source code use in Arduino IDE to read the current and calculate the I_rms. I am pretty sure there are errors in writing the code between the “if (amps > 0)…till… Serial.print(I_rms);” Feel free to point out my error and correct my mistakes.

Workspace.txt (912 Bytes)

at first glance, I see 2 weird things in your code :

amps = positiveAmps*;*
_ for (i=0; ; i++)_

• {*
if ( positiveAmps > positiveAmps**[i-1]** )
* {*
I_rms = ( positiveAmps )/ (sqrt (2));
* …*
* …*
[/quote]
- what is the value of positiveAmps[i-1] when i=0 ?
- for(i=0; ; i++) : no limit for i ??

To calculate the true RMS current, you need to take a large number of readings at regular intervals over a whole number of mains cycles. Last time I did this on an Arduino, I took a reading every 500us and accumulated 200 readings over 0.1 sec (which works for both 50Hz and 60Hz mains). Square each reading and add it to a running total that was initialized to zero. When you have accumulated the required number of readings, divide the total by the number of readings, then take the square root, and that is the RMS current.

RMS is defined in two ways depending on your measuring needs:

• If your load is linear (resistive or reactive) voltage and current will sinusidal. Then you can use the realation Irms = î/sqrt(2). î beeing the peak current.
• If your load is non linear, that is electronic (Computers, home modern home electronics, VFD, leds or energy saving lamps) you have to calculate a true RMS value by first squaring each current sample, then integrating (summing) all samples over one half period, then calculating the mean value of the squared samples and finally taking the square root of the calculated mean. This is what RMS actually means

And please observe: In order to measure power or energy you have to multiply voltage and current sample by sample and integrating/summing/averaging the result.
Simply multiplying Urms and Irms will work only for purely resistive loads like light bulbs and heaters.
If the displacement power factor (dpf or cos fi) is known beforehand you can multiply with this factor to compensate for reactive loads (motors and fluorecent lights). None of these two latter methods will work for non-linear loads.

nilton61:
RMS is defined in two ways depending on your measuring needs:

• If your load is linear (resistive or reactive) voltage and current will sinusidal. Then you can use the realation Irms = î/sqrt(2). î beeing the peak current.
• If your load is non linear, that is electronic (Computers, home modern home electronics, VFD, leds or energy saving lamps) you have to calculate a true RMS value by first squaring each current sample, then integrating (summing) all samples over one half period, then calculating the mean value of the squared samples and finally taking the square root of the calculated mean. This is what RMS actually means

And please observe: In order to measure power or energy you have to multiply voltage and current sample by sample and integrating/summing/averaging the result.
Simply multiplying Urms and Irms will work only for purely resistive loads like light bulbs and heaters.
If the displacement power factor (dpf or cos fi) is known beforehand you can multiply with this factor to compensate for reactive loads (motors and fluorecent lights). None of these two latter methods will work for non-linear loads.

All exactly correct. It is a piece of cake to make a power meter using an analog multipler, as I have done before, but would be a fun project with a micro as well.

As nilton6 says, you cannot in general measure power or energy just by measuring the RMS current and/or RMS voltage, even with sinewaves. For example, imagine the load is a pure capacitor or inductor, in which case the current waveform will be a beautiful sinewave. You can use an RMS responding DVM to measure the current and voltage, and you could naively multiply Vrms by Irms to get the power, and be totally wrong, because for a pure capacitor or inductor the avarage power is precisely zero.

The correct procedure is to simultaneously take samples of V and I, many times over the period of the waveform. Calculate instantaneous power as VxI, and take the average of these intantaneous powers to get the average power. For 'nasty' harmonic-rich current waveforms with that are non-sinosoidal and 'spikey', you will need many VI samples over the period of the waveform, or you'll get the wrong answer. For example, to account up to the 6th harmonic, which would be at 300 Hz, you will need a bare minimum of 600 Hz VI sample rate, which is 1200 Hz total sample rate because you need V and I. It's pretty easy to get the right answer with a nice resistive load like an incandescent bulb, but not so easy to get the right answer with a low-cost CFL, for example.

For a non-resistive load the instantaneous power can be positive or negative BTW, which is why you need to take the average.