Tuesday, March 19, 2013

Energy Monitor Kit: Behind the Scenes

The energy monitor kit can be used with any application circuit requiring energy measurement functions.In fact, just adding a e-Gizmo serial LCD Display would allow you to build a stand-alone energy measuring circuit.
An upcoming kit, Energy Monitor Kit, a scaleable energy monitoring front end module, will be available soon. This kit is a true and accurate AC line energy measuring circuit. It can analyze energy flowing through it, and output through a serial port important measurement details, such as true rms voltage and current as measured, real power, apparent power, reactive power, and power factor. This module is designed for easy integration to electronic systems that requires accurate energy monitoring circuits. This blog narrates how this kit was developed, discussing some experiences and difficulties encountered during the design process.


The Energy Monitor Kit


The Energy Monitor circuit is based on CS5463 energy monitoring chip from cypress. This chip is impressive; it got everything needed to complete a precision energy measuring circuit. But with the application documentations of the IC serving as an indication, I know the development tasks won’t be an easy one. 

I designed the module with the hobbyists and experimenter in mind. To make it appealing to them, it has to be easy to use. It should allow the user to add the functions with the addition of hardware and circuits. It should give accurate and reliable readings. Last but not the least, it should be low cost. 

Energy Monitor kit will display all at once real power Watts, apparent power VA, reactive power VAR, power factor PF, rms Volt and Amp. Here, power data for a 5uF pure capacitance test load are displayed.
Reading the specifications sheet and trying to make out the details alone took a great deal of my time. The conversion scheme used by the IC does not resonate with my intuition, and that made programming at times a bit frustrating. As an example, I wanted to make the module capable of auto-calibrating itself using just a stable 220VAC source and a test load. The IC has calibration functions that may do this. But using it led me to confusing results, and I cannot get the calibration procedure work the way I want it. After perhaps a couple of days of unsuccessful tries, I finally decided not to use the IC gain calibration functions, and have this pass instead to the microcontroller chip. 








The energy monitor kit can also transfer data to a PC with the addition of RS-232 to TTL converter kit. A Visual Basic source code application that will allow data to be displayed and saved through your PC will be available soon for download.
Writing the Codes

I picked a zilog z8F042A microcontroller to take the job of hiding the complex CS5463 operation details and present only to the user what probably matters most for his energy monitoring applications in ready-to- use form: true rms voltage and current readings, power (real W ,apparent VA, and reactive VAR), and power factor PF . I originally had assembly program language in mind when I made the choice. This microcontroller has 4K flash program space, plenty of program memory for assembly programming, but somewhat limiting* if C programming language is used instead. Nevertheless, as the design work moved on, I decided to use C language and face the challenge of fitting this within the 4K program space.

These are the difficulties I encountered and workaround I did to have the codes stay within the 4K space: 

The CS5463 spew out results in ratio of 2^23. To get meaningful results, I have to convert these numbers using, hmmm, floating point calculations. Wanting to conserve precious program space, I instead treated each numbers as 32 bit integers, and had all calculations performed using integer operations. This enabled me to get the desired results with much less program memory.

To display the results in ascii formats, I wrote my own simple formatting and conversion routines, avoiding altogether the use of the memory glutton printf function.

I forced the C compiler in the program to use ‘near’ variables and activated the ‘small but debuggable’ code generation options.





Screen snapshot of the VB Energy Monitor sample program. In this screen, the power consumed by a LCD monitor is measured and displayed.

Doing these from the start did not spared me nonetheless from ‘out of program memory’ problems, and several times I had to rewrite already good codes to make them fit inside the 4K memory. When the whole code still would not fit, I was forced to use my weapon of last resort: program some parts of the codes in assembly. Finally, I was able to compile the codes, ending up with a mere 50 bytes of free program space that I may be able to use latter for the finishing touches. 

*Of course, using Zilog’s C, 4K space will be big enough for applications that do not require floating points and preformatted (printf function) output functions. The printf function is code heavy and could easily eat up a large chunk of program memory space. Using this function may leave you little space available for the rest of your program.





The handmade PCB prototype. Note the yellow wire that directly connects the current input to the current shunt terminal.

Reckless PCB copper pours = Calibration Woes 

During the time I was programming the calibration routines, it was a natural thing for me to do some performance verification while the calibration is performed. The voltage channel worked flawlessly. It checked with accuracy better than 1% referenced to the readings displayed by my stable AC source within the 180V-250VAC input voltage range. The same cannot be said with the current channel though. I had the current channel auto calibrate at 2.2A test current. True enough, it gave a 2.2A readings at 2.2A load, but error increases as current load changes in either direction. The current channel is not linear! (I should mention this early it is not the fault of CS5643). Furthermore, the energy meter gave near correct power readings with highly reactive loads, but gave erroneous results when tested with pure resistive loads. Specifically, the Volt-Ampere displays some results that does not to follow the V*I relationship.





When the PCB was sent for outside fabrication, a copper pour covered the isolated current sensing path, causing problems as discussed in the text.

It is this erroneous power readings that got the best of me. I suspected my codes, rewriting and changing my computation and IC communication algorithms, hoping that it make the problem disappear. After more than a week of groping without any measurable success, I finally came back to my senses and did what I should have done in the first place- characterized the current channel input with controlled stimuli.

So I turned my attention back to the observed nonlinearity of the current input, and had it characterized using a function generator, taking results at different input levels. It showed the extent of linearity error, running as high as 20% at the low input range. But it gave no obvious indications what causes the error. The IC specification sheet specifies linearity to within 0.1% at 1:1000 dynamic range, and I have no reason to suspect this figure. I have to face the fact that something is awfully wrong with my circuit.







Our stabilized AC source used to test and calibrate the Energy Monitor Kit.

The first light finally arrived after I characterized the current channel input once again, this time not performing the zero offset calibration function built-in within the IC. A rather large offset appears whenever I connect the ground side of the current channel differential input! The computational engine of the CS5463, as it turn out, will display the current as simply zero whenever the offset causes a negative current net result. That provided a smokescreen that made it difficult for me to figure out the cause of the problem. The funny thing is, this problem did not showed up when I was doing my initial tests using my handmade PCB prototype, because I correctly anticipated this problem early on, and had the current input laid out correctly with the use of jumper wire (see photo) even before I started my initial tests.

Everything quickly fall into place. It is the PCB layout that is causing the problem. I realized the folly I had committed with my PCB layout- before I sent it for fabrication, I filled the empty area of the current measuring region with ground copper pour, in the process removing the isolation provided by the lone copper trace of the current input that should have been left alone as it wades its way toward the current shunt terminal.









250W test resistors used to test and calibrate the energy monitoring kit. These and the stabilized AC source pictured above are required to calibrate the energy monitor kit. We do not expect hobbyists to have these equipment at their disposal to do their own calibration, hence, the Energy Monitor kit will be available only packaged as completely assembled and calibrated.
After banging my head a few times on the wall (don’t worry, the wall is OK), I proceeded to fix the problem by adding a wire that once again connects the ground side of the current input the way it is supposed to be. Now I am getting the readings that I expect. A few more tests are in order. By and large, the energy analyzer kit is ready to go and will be available at e-gizmo in a few days time.



 

My fingers burned by repeated electrocution, concluding this blog for now.


No comments:

Post a Comment