# Prototyping – Part II

There is not much to this. A protoshield, the arduino, and a breadboard. Note the current transformer (donut). I have two of those to use in the panel. The first test was to plugging in a 60 W lamp to see what the measurement came too. I expected around 0.5 Amps and 60 watts. I was not disappointed.  I proceeded to plug in a toaster and put the ammeter in the circuit to see if my RMS current matched its RMS measurement. The photo below shows a .4% error. Not bad. Note the drop in the line voltage.

In Canada, the nominal line voltage is 120Vrms. I do measure the voltage as part of my power calculations and when I saw the 113 V I checked with the multi-meter and it read the same.  Assuming a 120 V reference would lead to errors in the power calculations. I should not be running a toaster outside my 20Amp line in the kitchen. I created a suicide cord that threads through the current transformer and is basically an extension cord. It plugs into a 15Amp line with other loads. 120 down to 113 is just over a 5% difference from the nominal line voltage. I am trying to rationalize why such a large dip. Anyway, the power measurement works. ## Crest Factor

The crest factor is the the ratio between peak and RMS signals. I do compute that and it gives me an idea on the shape of the waveform. A sinewave should have a crest factor of $\sqrt{2}$. The 60 W lightbulb had a crest factor of 1.40 for the voltage 1.40 for the current. Close enough.

I plugged in a variable speed drill and ran it a low RPM. As expected, the power factor went down to .27 with most of the power becoming reactive at 104 vars. The real power was just a mere 29.5 watts. The crest factor for the voltage was 1.39 and for the current, 3.96. That is expected as the duty cycle is changed to control the speed. For us home owners, we get charged for the real power consumed.  In industrial environments, the power company would penalize you for running with such an awful power factor.

I can’t wait to plug all this in the main panel see what the overall power consumption profile is. I expect the power factor to be closer to one.

## Next Steps

Computing C02 emissions is trivial as well as projecting cost of power usage.  I would like to have that wired next to the power panel and displayed on the LCD sooner than later.  On the other hand, I need figure out the zigbee side of things as well as how to best do the data logging.  I can easily purchase another arduino later and focus on getting this prototype soldered on something more permanent.

# Prototyping – Part I

Well I finally got around to get some code and test things out. Without known test loads and an oscilloscope, testing shall require some creativity.  Armed with a multimeter I began testing things out.

Software

The longer term plans include creating spot measurement devices  using the arduino and rather than slapping the code and copying and pasting stuff around, I opted to create a couple of classes to facilitate reuse. It took some trial and error to understand how to add classes to the arduino dev environment and could never get multiple inheritence to compile in the environment. I assumed the problem was between the keyboard and the chair.

I ended creating two classes: ACSignal to help capture and compute some basic stuff like average, RMS values. The PowerSignal class takes two ACSginal references and computes apparent power, real power, etc.

The code to use the classes is rather simple. Converting the float to alpha–since the LCD4Bit library does not handle spitting out floats–took more time that I wanted.

As explained earlier, the math looks more complicated than it is. The two main formulas are shown below along with the corresponding code to implement it. Anti-climatic if you ask me, but nice to see the stuff working.  As long as the sampling rate is much greater than the highest frequency component, things should be good to go. I take 2000 samples display stuff and resample.  Since this is all this arduino does I don’t need interrupt driven sampling periods. The maximum sampling rate for the ATMEGA chip on the arduino is around 9600 hz.  More than enough for this power line sampling. $P_{avg} = \frac{1}{N}\sum_{n=1}^{N}v_ni_n \approx \frac {1}{T}\int^{to+T}_{to}p(t)dt \qquad(1)$ $X_{rms}= \sqrt{\frac{1}{N}\sum_{n=1}^{N}x^2_n} \approx \sqrt{\frac {1}{T}\int^{to+T}_{to}x^2(t)dt} \qquad(2)$

@@

void PowerSignal::updateAccumulator()
{
mSumVoltageTimesCurrent += mVoltageSignal.getInstantaneousValue() * mCurrentSignal.getInstantaneousValue();
}

float PowerSignal::computeAveragePower()
{
return( mSumVoltageTimesCurrent / mVoltageSignal.getSampleCount() );
}

float ACSignal::acquireSignalSample()
{
float lVal;

mCurSample = analogRead( mPin )  ;
mCurVoltage = mCurSample * PS_VOLTS_PER_STEP;
mSampleCount++;
mSumVoltageRaw += mCurVoltage;
mRealVoltage = mCurVoltage - mDCOffset;  // remove DC offset from circuit voltage divider
lVal = getInstantaneousValue();
mSumVoltageSquared +=  lVal * lVal;

if( lVal > mMax )
mMax = lVal;

if( lVal < mMin )
mMin = lVal;

return( lVal );

}
float ACSignal::getRMSValue()
{
float lRMSValue = PS_PWR_CALC_ERR;
if( mSampleCount > 0 )
lRMSValue = sqrt( mSumVoltageSquared / mSampleCount );
return( lRMSValue );

}

Time to plan to interface to the  Arduino. The constraint is that the inputs can only be 0-5 volts.  So no negative voltages.

Measuring the Line Voltage
I am using a 9VAC 1.5A wall wart to step down the voltage. To shift voltage into a 0-5 range, I used the following circuit. All I have is a digital multi-meter that measures true RMS. Open circuit measured came in at 10.5 Vrms.  I assume it is  sinusoidal so the peak-to-peak voltage is $10.5(\sqrt{2})=14.8 Vp-p$

The plan was to shift that into a 2.5Vp-p centered around 2.5V which will give me the required 0-5V. A 2.5 voltage reference zener diode was used to shift the voltage up 2.5 volts. The potentiometer was used to tweak the RMS voltage to $\frac{2.5}{\sqrt{2}}=1.77 Vrms$

Measuring Current
Current measurement followed  similar approach. The current transformer with a resistor in placed across the terminals produces a voltage proportional to the current. The spec sheet for the CR8459-2000-N states that the voltage is defined as $V = \frac{IR}{T_e}$ where $T_e$ for this CT is 2011.

It would be difficult to test this in the panel installation so max current, I,  at 7.2 amps for the bench. I will need to debug stuff and will change the current to 100A when I stuff it in the panel. Note I need to do this circuit twice since I have 2 100 Amp measuring points. So solving the formula for R and setting I =7.2 and V=7.2 and using the given $T_e$ we get $R=\frac{VT_e}{I} = \frac{(7.2)(2011)}{7.2}=2011 \Omega$

The diagram below shows how the current measurement was interfaced. Again the potentiometer was used to tweak the  RMS voltage to $\frac{2.5}{\sqrt{2}}=1.77 Vrms$ The R or potentiometer value will change to value of $145 \Omega$ when I permanently mount the current transformer to reflect a higher current range.