Design of a digital energy meter using a PIC - Page 1
Forum section: electronics / projects

author: mikeroprocess
author_rank: Newbie
timestamp: February 03, 2012, 03:16:40 am
content: Hi everyone, I am in my final semester of my senior year in an EET program and I am required a 2 semester course in design. My group has decided to design a digital energy meter to display energy in kWh on an LED. We've decided to use a PIC18F and plan on measuring our current using a shunt resistor with the ADC and designing a ladder network to measure the voltage also with the ADC of the PIC. I have never programmed a PIC but I have taken 4 semesters of C++ programming so I hope to start programming the PIC quickly. Any advice at all is welcome, I am a beginner, thanks in advance.-Mike

author: amspire
author_rank: Super Contributor
timestamp: February 03, 2012, 03:38:55 am
content: Mike, to measure true power in a micro, you need to do real time multiplication of instantaneous volts and current. This is more the realm of a faster DSP micro instead of a PIC. The problems with mains monitoring is that quite often current waveforms can be quite spikey, and that is why you need high speed sampling for a fully digital solution. You possibly could get something to work reasonably as long as the current is nice and sinusoidal.You could use sampling techniques (measuring a few samples per cycle over a lot of cycles) to get a PIC to match a high speed DSP in terms of total number of samples, but the trouble with sampling is that it can be fooled by certain types of waveforms, whereas if you have a combination of a lowpass filter and a real time sampling DSP at over twice the low pass filters cutoff frequency, you cannot be fooled.You would be much better off getting one of the ICs that do the volts x current using analog multiplication, and then you use the pic to read the output of this chip.Something like this:http://www.analog.com/en/analog-to-digital-converters/energy-measurement/ade7953/products/product.htmlThere is a similar thread on this topic:https://www.eevblog.com/forum/projects-designs-and-technical-stuff/power-quality-monitor-work-in-progress/Richard

author: mikeroprocess
author_rank: Newbie
timestamp: February 03, 2012, 03:59:12 am
content: Thanks for your reply Richard. My professor recommends we use 128 samples per period. I am not exactly sure how to implement that in code though. I will consider your suggestion, it seems like I would have to feed the current and voltage into that IC you showed me and then wire the output of that IC to the input of the PIC and use that value to calculate my energy. Is that correct?-Mike

author: Gall
author_rank: Frequent Contributor
timestamp: February 03, 2012, 07:17:11 am
content: It is generally a better idea to do part of the work in analog circuit.The obvious technique is to multiply I*U by using either a couple of opamps or a readily available IC. Analog Devices makes such ICs. The less obvious one is to use carefully designed low-pass filters to "flatten" the ADC input signal.

author: erupter
author_rank: Contributor
timestamp: February 03, 2012, 08:22:47 am
content: Roam a bit on the microchip site.Check this outhttp://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en520376They also have a full dev-board for this.No 16F though, too small/slow, at least a 18F with external sampling.http://www.microchipdirect.com/ProductSearch.aspx?keywords=MCP3905EV

author: amspire
author_rank: Super Contributor
timestamp: February 03, 2012, 12:34:33 pm
content: Quote from: mikeroprocess on February 03, 2012, 03:59:12 amOk, this is not a commercial design - it is an exercise, and the professor has told you to use 128 samples per period. At 60Hz, that is one sample per 130uS.What you need to do is to set a timer to generate an interrupt every 130 uS. During the interrupt, you need to read the instantaneous voltage and current as signed integers, and you have to do a signed integer multiplication of the two numbers, and then add it to an accumulator.Now if you keep doing this, the accumulator keeps growing for even, so what you want to do instead is to maintain a kind of running average.One way to do it is this. Divide the number in the accumulator by 255 and then add the latest result of the multiplication. To do this, divide a copy of the accumulator by 256 and subtract it from the accumulator. To divide by 256, it is just a 8 bit shift, or a whole byte shift to the left.The result in the accumulator will end up being 256 x V x I. The important thing is it is proportional to the RMS power. It can be positive or negative depending on the direction of the power. Make sure you are careful to maintain sign in all operations, and that none of the calculations overflow. To fit all this in 130uS, you will probably have to do careful assembler code, and look for the fastest way to do each step.This would be a low accuracy real life power meter with arbitrary current waveforms, but will product good results with sine wave currents which is probably what the professor will use to test the result.Richard.

author: Gall
author_rank: Frequent Contributor
timestamp: February 03, 2012, 03:59:46 pm
content: Be sure to have low-pass filter. Preferably a surge protector, capacitor and a choke at mains input, a choke and cap at output. The idea is to make the mains voltage waveform as close to sine wave as possible. Switching loads like SMPS or dimmers may result in waveform distortions. They may cause wrong readings. Addition of usual AC input and output mains filters would fix it.

author: Zero999
author_rank: Super Contributor
timestamp: February 03, 2012, 06:12:00 pm
content: For safety reasons it's probably a good idea to isolate the PIC's DC power from the mains but fortunately this is easy to do with transformers, although you need to make sure you use properly calibrated measuring transformers, an ordinary power transformer may distort the waveform too much.

author: mikeroprocess
author_rank: Newbie
timestamp: February 04, 2012, 07:05:53 am
content: Thanks everyone for your replies!Quote from: erupter on February 03, 2012, 08:22:47 amThis energy meter IC looks interesting, and could be added to my design, but at a 130uS sampling rate for the ADC conversions should be done sufficiently by the PIC itself. This IC would improve my accuracy though.Quote from: amspire on February 03, 2012, 12:34:33 pmI realize this is a dull question but, how much code would what you explained call for? I intended on writing the code in C not assembler. All of my courses in C++ programming called for several pages of code for some programs including several different header and source files, how can I use my knowledge from those courses for programming the PIC?-Mike

author: amspire
author_rank: Super Contributor
timestamp: February 04, 2012, 07:21:18 am
content: It may be possible to write the interrupt in C, but you have to try it to find out if it is fast enough. Sometimes the C compilers actually write some great assembler.Two things you have to prevent:1. Each interrupt must always be finished before the next interrupt 130uS later comes. The thing about writing a fairly small bit of assembler is you can count exactly what the worse case number of cycles the code needs.If you write in C, you will have to still actually look at the assembler written by C. Otherwise, you just do not know what the C compiler has put in - like conditional calls - that in some cases can use up too many cycles. If you try it in C and it looks like it is taking 80uS maximum, then you are laughing. Toggle a spare pin high when you enter the interrupt and low when you exit. That way you can use a scope to see exactly how long it takes to run.2. The main program running outside the interrupts needs to be able to read the current V x I average. It must not read all the bytes without an interrupt changing the bytes half way through the read.There are solutions to this problem.Richard

author: IanB
author_rank: Super Contributor
timestamp: February 04, 2012, 08:02:53 am
content: Quote from: mikeroprocess on February 04, 2012, 07:05:53 amTo use the knowledge gained from C++ programming, you should focus on the core elements of the language, like functions, variable declarations and basic control structures like if, for, switch, etc. These will map over fairly readily into C. One thing you will find if you are used to C++ is that C is more restrictive in what it allows and you will get a whole bunch of compiler errors until you learn how C differs from C++. Features like classes, function overloading, reference arguments, templates and a whole lot of other stuff are not found in C, making it a very lean programming language. The standard C reference by Kernighan and Ritchie is highly recommended. It is one of the most perfectly concise and thorough programming texts ever written.

author: Gall
author_rank: Frequent Contributor
timestamp: February 04, 2012, 03:11:03 pm
content: Depending on the compiler, you may be able to use templates and classes quite efficiently. I did it successfully using AVR and gcc, not sure about PICs. To do so, you should NOT use new and delete operators and STL. You may use namespaces, classes with static members only, overloaded functions, templates (like #define replacement only) and, very limited, ordinary classes constructed on stack in local scope. Do not try to use virtual or dynamic_cast, too.If you know C++ good enough, you'll be able to write even more efficient code than Assembler, provided that your compiler is smart enough while expanding inline and compile-time const expressions.

author: mikeroprocess
author_rank: Newbie
timestamp: February 06, 2012, 05:41:49 pm
content: Does anyone have any recommendations for a programmer to pop the PIC to and from the circuit?

author: erupter
author_rank: Contributor
timestamp: February 06, 2012, 08:48:45 pm
content: Quote from: mikeroprocess on February 06, 2012, 05:41:49 pmPickit3 is discounted now if you enter MPLABX ad a coupon code.Leave the pic on the board, and use an ICSP connector.

author: mikeroprocess
author_rank: Newbie
timestamp: February 07, 2012, 04:22:25 am
content: Quote from: erupter on February 06, 2012, 08:48:45 pmI ordered both of these in this link:http://www.microchipdirect.com/productsearch.aspx?Keywords=DV164120I was going to use the PIC18F from the demo board.