Need to write small prog to measure RMS Current, RMS Voltage and power factor for 50HZ 8 plug power strip, any sample prog?.
37 13369 Banfa 9,064
Expert Mod 8TB
To my knowledge most power strips don't usually have a data interface.
How are you planning on physically taking the measurements? Should surely be the first question to ask before you ask the question of how you write the code to obtain and process that data.
Thanks for question. I have designed small hardware with small microcontroller to do these measurements.
Banfa 9,064
Expert Mod 8TB
OK what data interface exists on the micro control, RS232 for example?
What protocol are you using to transfer data across this interface?
This is 24 plug power strip (8 plugs are one group  3 power boards in strip), with 3 input power cards for strip, each power card controls one power board on that 24 port strip, only one microcontroller to read all 24.
Basically I am using GPIO to select plugs on 24 plug power strip, to read Current and voltage form each plug, I have ADC on microcontroller, I select one plug at a time (840 samples per second, I have to read 24 currents form 24 plugs and 3 voltages from each group (8 plugs are one group)), select one and read ADC input, which gives value of that either I[1].....I[24] or V[1], V[2], V[3].
I am using Five GPIOs as input to a mux to fanout 32 separate input sources like below (since all other GPIO used for something else),
Current 1 00000
Current 2 00001
Current 3 00010
Current 4 00011
Current 5 00100
Current 6 00101
Current 7 00110
Current 8 00111
Current 9 01000
Current 10 01001
Current 11 01010
Current 12 01011
Current 13 01100
Current 14 01101
Current 15 01110
Current 16 01111
Current 17 10000
Current 18 10001
Current 19 10010
Current 20 10011
Current 21 10100
Current 22 10101
Current 23 10110
Current 24 10111
Current 25 11000
Current 26 11001
Current 27 11010
Current 28 11011
Current 29 11100
Current 30 11101
Current 31 11110
Current 32 11111
There must be some interface circuit between the plugs and the ADC. What sort of signal is presented to the ADC: instantaneous voltage, peak voltage, RMS voltage? Same question for current.
That is, are you obtaining RMS via hardware or via software?
By the way ... be careful. You're working with potentially lethal currents.
(That's lethal to you, I'm much less worried the microcontroller.)
Thanks for your valuable mail.
There is an interface circuit, all AC outlets are controlled through relays, relay can turn ON/OFF each plug, that is separate logic, but there is a mux between microcontroller (ADC) and relay, that is an analog MUX which gets feed current and voltage back to ADC for measurement, which gives Instaneous current and Instaneous voltage. I have to read 24 currents and 3 voltages 840 times per second (840 samples per second). I have to store those 27 readings in separate buffers, then I have to calculate average and then RMS of each 24 current and voltage.
for (count=0; count < 840; count++)
{
for(inst_curr_cnt=0; inst_curr_cnt < 24; inst_curr_cnt++)
{
set GPIO of one out of 32 and read ADC value and store
I_buff_loc[inst_curr_cnt] = ADC
}
for(inst_volt_cnt=0; inst_curr_cnt < 3; inst_curr_cnt++)
{
set GPIO of one out of 32 only Three times and read ADC value and store
V_buff_loc[inst_curr_cnt] = ADC
}
square those accumulation of each instaneous curent and voltage, then calculate rms for all 24 currents and voltages
rms_curr = accumulation of each current / 420
}
I need some logic how to calculate power factor for reach based on instaneous current and voltag, thanks in advance.
@tvnaidu
The only way I can think of to do this involves measuring the phase angle between the voltage and the current.
Hardware provides an input to ADC to measure instaneous current and voltage only, by measuring those two 840 samples per second and then store those values, accumulate, then average and get RMS of each and calculate real power, then apparent power, using those Two calculate power factor.
The Hardware doesnot provide anything besides inst. Currewnt and volatge?
Banfa 9,064
Expert Mod 8TB
You can not use instantaneous voltage and current to calculate the power factor and you need the power factor to calculate real power.
You can calculate instantaneous apparent power from instantaneous voltage and instantaneous current.
You can calculate root mean squared (RMS) values relatively easily. As the name suggests this is the square root of the mean of the squares of the value (voltage or current or power) taken of 1 period of the wave.
However for a sinusoidal wave centred on y=0 (true for instantaneous voltage and current but not power) this simplifies to the peek value divided by the square root of 2. 
peek value



sqrt(2)

You will have to examine the captured data to determine the peek value numerically.
You will need to calculate the power factor because
real power = apparent power * power factor
where apparent power = Vrms * Irms
You should be able to calculate the phase different between the voltage and current by looking for the peaks in those values and determining the time difference between those 2 peaks compared to the total period for the wave.
Time the time difference in the range wave period/2 to +wave period/2 this corresponds linearly to a phase difference of 180 degrees to +180 degrees.
Once you have the phase difference the power factor is
power factor = cos(phase difference)
Look up these pages, the explain a lot of it and have some useful graphs and equations power factor root mean square Banfa 9,064
Expert Mod 8TB
On and a final point on your code, your loop 0  840 will not produce 840 samples per second, it will produce a burst of 840 samples.
To get 840 samples per second you need some sort timer triggering the sample mechanism. Your microcontroller probably has 1 or more timer interrupts available so you can trigger it off an interrupt.
However I would tend not to perform 27 ADC conversions and readings in an interrupt routine as it would block any other interrupt routines and ADC readings can be slow (in comparison to processor clocks). So use the timer interrupt to set a flag that the main code uses to trigger 1 set of ADC readings.
Also you can just take the read, every reading will need to be time stamped if you want to calculate the phase difference.
actually this microC had inbuilt 4/8 channel ADC, I am not using any timer interrupt for that, it had 8 ADC pins can be used as GPIO or ADC inputs, I am using 5 pins to select one of plug and one ADC input to select read current/voltage.
My question is: I have only instantaneous current and instantaneous voltage readiungs only, using those Two can I calculate real power and apparent power, then using those power readings can I calculate power factor?. My goal is to calculate power factor with instantaneous current and instantaneous voltage.
anyway below is my routine to calculate RMS current, RMS voltage from instantaneous current and voltage, also I did actual power and apparent power calculations, also I did power factor calculations. 840 times I took ADC value for inst. current and inst, voltage, the others readings (RMS CURR, RMS VOLT, ACTUAL POWER, APPARENT POWER, POWERFACT) I did for every halfsecond, please take a look my routine below (sorry for syntax), please see logic and let me know. appreciated.
please point me if I am wrong. appreciated you all.
this will not compile  no space between '+' and '=': insta_curr_read [curr_count]+ = read_adc_val
And why insta_volt is only one sample per channel in power calculation?
Banfa 9,064
Expert Mod 8TB
Your calculations for all rms values, actual power are all wrong. Note your calculated apparent power and power factor will be wrong too but only because theyare based on the incorrectly calculated other values not because the calculation logic is wrong.
Starting with rms values you are
Accumulating the square of the instantaneous values
Taking the square root of: the accumulation divided by 1/2 the number of samples
This is neither the square root of the mean of the squares of the values over one period; because you do make any attempt to ensure that you are taking a set of readings that last for a single period of the sine wave. Also the accumulator is never zeroed, that means its value is always increasing so your rms values for voltage and current will always be increasing which is clearly wrong.
To calculate the RMS value you must either
capture a set of data points that last for 1 period of the sine wave and have a regular period, then use the to manually calculate the RMS value by calculating the sum of the mean of their squares.
or
detect the peak value as you take measurements and calculate the rms value using
rms = peak / sqrt(2)
Your calculation for actual power is
actual_power[count] = insta_curr_read[count] * insta_volt_read[3];
This calculates the instantaneous actual power. You then use this along with the rms apparent power to calculate the power factor. You can not mix instantaneous and rms values in an equation like this the result is meaningless.
You need to calculate the rms actual power and then you can use you equation above to get the power factor or calculate the power factor and then you can use that and the rms apparent power to calculate the rms actual power.
I didn't say you were using the timer interrupt. I said you should use the timer interrupt to ensure that you accurately spread you 840 samples across the second rather than doing them in a burst which is what happens if you just use a for loop.
Some of the formulas suggested above are only true for sinuisoidal wave forms, for example:
rms = peak / sqrt(2)
power factor = abs(cos(phase angle))
Do you have any big reactive loads on the power strip that might significantly distort the waveform?
Does it matter if your computations are inaccurate while that load is kicking?
Hi Donbock:
The plugs are used for equipemnt like computers, some servers, some switches, mainly this power strip to use in lab. that is why I need to calculate RMS values for current and voltage, later power factor.
I don't think I get peak value, but with the above reply from "Banfa", I still need to understand more how to accumulate all those instaneous values.
Let me figureout to use timer interrupt which should generate for every One second to measure 840 samples per second. instead of separate task's for loop.
I don't get peak value, I onnly gets instantaneous current and instantaneous voltages, can I use those two with Timer interrupt to calculate power factor?. thanks.
If you can trust that the waveform is sinusoidal and that the frequency is 50Hz, then ...  Draw a sine wave on a piece of paper.
 Now draw some vertical lines connecting the X axis to your sine curve. Make these lines equally spaced in the X direction.
 The vertical lines represent your ADC samples, their length is the value you read from the ADC.
 You will begin sampling at some arbitrary part of the sine curve because your program is not synchronized to the power input; but that's ok.
 The sample period should be N * 20mS, where N is an integer and 20mS is the period of 50Hz. (It is easiest to visualize what's happening if N=1.)
 You can compute RMS directly from your array of samples; or you can find the maximum (ie, peak) value and divide it by sqrt(2); or you could fit a sinuisoid to your sample data and find the peak value from that.
 You can find the phase angle by measuring the time skew between corresponding points in the voltage and current waveforms. From the phase angle you can compute the power factor.
I am still a little nervous about whether this project is safe.  How did you come up with the idea of doing this?
 Are you making the hardware yourself or did you purchase a power strip with all this stuff in it?
 What are the consequences if your software makes a mistake? Will anyone be hurt; will any equipment be damaged?
Actually I am not making Hardware, designed by hardware, I need to implement software, mainly an extra function to set GPIO and read ADC value, 5 inputs which is 2 to the power of 5 is 32, using 5 GPIO input pins select one of the current/voltage, read that and calculate those readings, Hardware provides Instantaneous current and Instantaneous voltage, I am figuringout with those can I really calculate RMS curent and RMS voltage, then those Three powers.
Sofar I never measured readings like this, that is why I am trying to understand.
I draw sinewave on paper starting positive half one side and the other half in negative side, if I draw lines parallel to Yaxis, I have 420 lines on positive side and 420 lines on negative Yaxis side, those are samples, each one is different height, if I add all of them, I gets zero because of (+)ve and (ve).
But when I read instaneous voltage or current, not sure whether ADC gives value whether it is ()ve or (+ve)?. I am just cumulating, may be not be correct?.
Banfa 9,064
Expert Mod 8TB @tvnaidu
No but you could use those values to calculate rms voltage and rms current to give apparent power and then use the same values to calculate instantaneous power and that to calculate rms actual power and then use apparent power and actual power to calculate power factor.
You say you only get to measure instantaneous values not peak values. I understand that, if you record enough instantaneous values then you can calculate the peak value (crudely it'll be the largest one).
About your hall effect chip.
That data sheet is actually the data sheet for 3 different parts (all part of the same range just with different performance characteristics). You need to know which part you are using.
Every part takes a current between N and +N amps and converts it to a positive voltage. For example the +5 amp part outputs roughly +1.5  +3.5 volts.
That is connected to your microcontrollers ADC which then converts the value to a 16 (?) bit value. You need to look atthe ADC (microcontroller) data sheet to work out how the value is related to the input voltage and then you can work out from the posted data sheet how the value is related to the line current.
Your microcontroller ADC almost certainly doesn't measure power line voltages (240v?) so there must be some circuitry in there too to aid measuring the voltage.
And finally Don you seem to suggest in post 17 that the sample period should be greater the the mains period. That's not right whatever you are sampling your sample period wants to be less than the period of the thing you are measuring or conversely you sampling rate wants to be higher than the frequency of the of the thing you are sampling.
Slightly strangely at a sample rate of 840Hz you get 16.8 samples per mains frequency period.
With 24 lines to measure current on them, 840 samples per second will not be enough. You still need to measure current some 10 times per halfperiod, ( ~20000 samples/sec ) If the controller is 'generic' 8bitter, forget about float  it will take forever, use 8*8 =16 or 16*16 = 32 multiply.
Banfa 9,064
Expert Mod 8TB
I believe the OP is talking about 850 samples per second per measured line.
It is 24 plug power strip, 3 input cards, 24 plugs divided into 3 boards inside power strip, each board had 8 plugs, means one power card for one set of 8 plugs, and second power card for another set of 8 plugs, last power card for last set of 8 plugs, total on power strip 24 plugs, if all 3 cards plugged to 3 inputs, then all 24 plug ports works. just like one big power strip with 24 plugs, 24 lamps can be connected, if all 3 input powerd connected, if only one input, then only one board woreks, each board had 8 plug ports. whether it is one board (8 plugs), or two boards (16 plugs) or 3 boards (24 plugs), I have to do measure 840 times inst. current and inst. voltage in a second. am I wrong?

_1_ 2__3__..........24_____
Hi Banfa: Thanks for the mail about halleffect IC.
I am trying to read all these reviews and try ti understand where I did wrong in my above 100 lines program. Bascially I am going to use a timer interrupt to set the flag, then in my task, I am looking for whether flag is set or not, if flag is set, then I want to take samples and store those values in buffers and then calculate those powers, I need to calculate powers for every half a second, that is why I put a if condition at 419 to do that calculation, can you point me where I did wrong in those 100 lines?. appreciated.
I have instantaneous values stores in initial bugffers, later accumulations, later did average and RMS, then powers.
@Banfa
My mistake  I used terms that maximized confusion. I meant to say that the overall interval within which all samples are taken should be an integral number of cycles. Ideally, the individual samples would be spread out evenly within that overall interval.
Suppose the overall sampling interval is one 50Hz cycle; and suppose you collect N samples during that overall interval (the sampling frequency is (N1)*50Hz); and suppose you decide to consider the maximum sample value to be the peak value (ie, no curve fitting). Then, ignoring noise, the worstcase is when the actual peak happens to occur exactly halfway between two samples. The percenterror for that case is:
(sin(90deg)  sin(90deg  360deg/(N1))) / sin(90deg) * 100%
You can use this formula to compute the minimum number of samples/cycle to achieve any particular accuracy. As a general rule of thumb, I would prefer for the actual number of samples/cycle to be at least twice the computed minimum value, but that may be unnecessarily excessive.
I recommend that while you're collecting samples that you do nothing else. Perform all your calculations afterwards. Such an approach means that you are not tracking voltage, current, or power factor in real time.
How much latency is acceptable?
@tvnaidu
How did you come up with 840/second?
Basically this strip used to connect some servers and some computers, need to measure actual power, apparent power and PF.
I would like to setup timer for one second, each timer interrupt sets a flag, I have another task running separately looks if flag sets, then it does sampling and read ADC values and compute those powers and PF.
Banfa 9,064
Expert Mod 8TB
A 1 second timer will not work, you will still end up taking you samples in bursts of 840 samples every second with a big gap.
Imagine if I ask you to count to 10 every 10 seconds as fast as you can, you would
Spend about 2 seconds counting to 10
Spend about 8 seconds waiting for the next 10 second interval.
Your samples have to be taken on a very regular period to properly sample the input voltage and current waveforms. So what you need is a timer that goes off every 1/840 = 1.19 milliseconds and to take 1 sample of everything every time it goes off. You need to count time or the number of timer ticks and reset all your accumulators every time the mains frequency period is up i.e. every 1/50 = 20 milliseconds.
That is why I think 840 is a strange sample rate, because it is hard to synchronise sample time and input frequency time. (1/50) / (1/840) = 16.8 samples per mains cycle.
If you took 850 samples per second you would need a 1/850 = 1.176 millisecond timer but you have exactly 17 samples per mains cycle.
If you went further to 1000 samples per second you would have to have a 1 millisecond timer and have 20 samples per mains cycle which gives nice numbers for everything. You would get similarly nice numbers for 500 samples per second but I do not know if that gives you a high enough sampling rate to accurately take your measurements.
A also do not know if your microcontroller has the grunt to be able to sample at any of these frequencies.
@tvnaidu
You made it clear that you are doing the software, not any of the hardware. I am still unclear about where the 3 input cards and 3 power strip cards come from. Were they designed and built by one of your peers or were they purchased from some company? @tvnaidu
Why is it so important to calculate RMS values for current and voltage, later power factor? The reason I ask is to discover what the implications are of inaccurate calculations. The severity of these implications drives the accuracy and latency requirements.
For example, if all you do is put the calculated values on a humanreadable display then that tells me there is no reason to calculate new values any faster than about 5 times a second. Much faster and the numbers would change too quickly for a person to read.
For another example, the required accuracy of the power factor calculation determines the necessary accuracy and resolution of the timestamps you use to compute the phase angle.
Minimum it should nbe 840 times per Second, reason is I got 840 like this: at 7th harmonic, 7 * 60 = 420, then for two sides multiply with 2, 420 * 2 = 840. Minimum it should be 840, it can beyond 840. may be if I take 1000 samples per Second, 1/1000 = 1 ms, I want to make measurements and calculations twice for every One milli Second, these readings should give some indications like, for example, if one bulb and one fan connected, then it should howmuch power it draws total. also what it tells what is the power factor is.
I have to write software piece, Hardware manufactured somebody. Now a days these power strips are common with web interface to control each plug and shows all these power ratings.
@tvnaidu
It sounds like you expect power line harmonics. If so, then the sinuisoidalonly equations may not work well enough for you. Take a look at this article to see if you need to include Distortion Factor in your power factor calculation. (I don't know how to compute distortion factor.)
By the way, you probably want the 7th harmonic of 50Hz, not 60Hz.
Thanks for the article, I will take a look.
I rewrote the program to measure inst. currents and voltages, then calculate other power values. Here I have timer running for 10ms (1000 samples per second, 1/1000 = 10ms, I have to take sampling not less than 840, I rounded to 1000 for easy timer), timer ISR sets a flag, in my main task, I will check if the flag is set, if the flag is set means it is one instant, I am taking only 50 instances (500 ms), then I calculate all powers, I maintain that counter locally whether it reaches 50 or not to measure powers, once power readings calculated, I reset counter to zero. for every Half second means halfof sine wave, I want to take insta. readings and store them.
below program buffers to hold vinstantaneous value only, gets overwritten for every instantaneous, where as accumulators keep accumulates all 50 instantaneous, it resets to zero after 50 instant, after measure power readings.
here below total SAMPLES defines as 100, since interrupt for every 10ms, total is 1 sec (100 * 10ms), but I use "SAMPLES/2" for RMS calculation since I am measuring only 50 readings. Please let me know whether I can use this prog. Banfa 9,064
Expert Mod 8TB @tvnaidu
That all sounds good except 1000 samples per second = 1/1000 = 0.001 = 1ms timer not 10ms timer which is 100 samples per second.
Also please try to remember to put [code] ... [/code] tags round your code, it makes it more readable on the forum.
Thanks Banfa Sir, appreciated for your valuable time.
Actually I though instead of doing 1000 samples, I can do 100 samples, may be 100 samples may not accurate?. also I have change my timer ISR for 10ms instead 1ms, that is my mistake typing 1ms. I don't know whether I should take 1000 samples instead 100 samples. In that case all RMS calculations averate should divide by 500 instead 50. May be if I take 1000 samples, may be accurate?. Please suggest me.
I din't get that tage, please suggest me how to keep those tags?. Thanks again.
Banfa 9,064
Expert Mod 8TB
You can either type the tags or if you are using the advanced editor highlight your code and press the # button at the top right of the editor.
Starting with a sample rate of 100 per second may be a first good step. Get everything working, if less accurately before trying to get the software running at full speed.
Thanks for help, appreciated.
banfa i didn't get the actual power with this method?have you tried this method??
Banfa 9,064
Expert Mod 8TB
No I have and have never had any need to perform power factor calculations in a microcontroller (or on any other computer for that matter).
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by Fabian 
last post: by

18 posts
views
Thread by Peter Mount 
last post: by

1 post
views
Thread by WinstonSmith 
last post: by

1 post
views
Thread by Prakash Wadhwani 
last post: by

29 posts
views
Thread by John Devereux 
last post: by

41 posts
views
Thread by Mountain Bikn' Guy 
last post: by

74 posts
views
Thread by Dominik Wallner 
last post: by

9 posts
views
Thread by mathon 
last post: by

9 posts
views
Thread by Ross 
last post: by
           