Power quality monitor -- work in progress - Page 1
Forum section: electronics / projects

author: gxti
author_rank: Frequent Contributor
timestamp: December 25, 2011, 08:32:10 am
content: A co-worker recently found a great deal on 100A, pre-terminated, clamp-on current transformers so I bought 5, characterized them with some dummy loads in series with my Fluke 87-V, and then did what any self-respecting lunatic would do -- put them in my breaker panel:Homes in the U.S. are normally fed with 240VAC split-phase, e.g. a neutral and two anti-phase 120VAC hot lines. High-power appliances like ranges, ovens, furnace, etc. sit across both phases and take the full 240VAC while lights and receptacles are on one phase or the other. So I chose to put one transformer on each of the two feeders, and the remaining three across two branch circuits where my computers are connected and on the furnace. Here are some shots of various loads with an oscilloscope monitoring the two feeders, before with just computers (rather ugly looking, ain't it?) and after with the range on:And here's one with the microwave running:So that's all well and good, but there are two problems. One, I want to monitor these all 24/7 and record some chart porn and the oscilloscope isn't much use for that . Two, current is only half of the story. What I really need is to monitor voltage as well and then calculate real power, reactive power, power factor, etc. So I set out to design a device to monitor both the current transformers and the line voltage, do the number crunching to produce condensed data, and feed it over USB to a computer which can record it over time and make pretty graphs. Because I like making things as complicated as possible this device will be powered from the same line that it's monitoring, using a LNK305 buck converter.I limited each device to 3 current channels and 1 voltage channel, so two devices will be used to cover the whole house, one per phase. All four channels use a single picoamp op-amp in a low-pass, inverting configuration. This is followed by a small RC filter to provide a charge buffer for the ADC. The voltage channel is also preceded by a voltage divider and a coupling capacitor to recenter the waveform at 2.5V while the current inputs are already centered because they have one wire tied to 2.5V and are already scaled to within 5Vpp out of sheer luck. The voltage frontend was validated with LTspice but I'm still not 100% sure that it will work.These four channels are then sampled and processed by a dsPIC30F running at 120MHz (30 MIPS). The details here are still a bit fuzzy but I'm aiming for about 1-10 ksamples/second per channel, contingent on performance. I've never worked with dsPIC before or done any kind of high-performance microcontroller programming, so this should prove to be educational.Finally, the dsPIC's UART is connected through an isolator to a separate USB-powered section with a MCP2200 USB-UART bridge. Note also the reset line, which allows easy initiation of reprogramming of the dsPIC30 in-circuit using the excellent ds30loader bootloader without worrying about isolation.Layout was completed in an afternoon, somehow without going completely insane. I chose to use a two-layer board with laen's fantastic PCB service (http://pcb.laen.org). Four-layer would be used for a production model if such a thing came to fruition but it would have doubled my PCB cost for the prototype without reducing the board area at all. I'm sure this board is full of mixed-signal faux pas but I will be quite pleased if it doesn't simply burst into flames -- the rest is just nitpicking.The chosen enclosure is a DIN-rail box from Polycase:http://www.polycase.com/ek-series. I'll be the only kid on the block cool enough to have a DIN rail in his laundry room slash server closet. Hopefully I'll come up with some other cool gadgets to clip on there. The PCB only occupies the top half of the enclosure due to prototype PCBs being billed by area. The mains connector will be inside the enclosure and requires unscrewing the lid to access it. Unfortunately I don't have any proper drawings of the mechanical layout, so this pretty 3d view of the PCB will have to suffice.Initial bill of materials totals 33.19 USD per unit including PCB, enclosure, and terminal block plugs.At this point nothing has been fabbed, it'll be a few weeks before the PCBs arrive due to the holidays. I'll report back in once I've made more progress. Feedback is most assuredly welcome and I'd love to hear exactly how you think I'll manage to kill myself with mains voltages, or how much I suck at filter design. Merry Smissmass to all, and a pleasant belated Festivus as well.

author: PetrosA
author_rank: Frequent Contributor
timestamp: December 25, 2011, 06:30:29 pm
content: This should give you an idea of how much money you're saving by building this yourself.http://www.brultech.com/home/store/category.php?id_category=16

author: NiHaoMike
author_rank: Super Contributor
timestamp: December 27, 2011, 08:31:20 pm
content: If it helps, my senior design project has a power meter based on a dsPIC33FJ128GP802, although it used a LTC1864 for the current channel to get more dynamic range.It looks like your antialias filters are set at too high a frequency. The current and voltage channels should be separated as much as possible to avoid crosstalk. The current channels especially should use smaller resistors to keep down Johnson-Nyquist noise. Differential inputs (or pseudo differential inputs) are also preferred to increase the SNR. And the ground and power for the analog circuits should be separated from that of the digital part (joined with ferrite beads) in order to reduce noise.

author: gxti
author_rank: Frequent Contributor
timestamp: December 27, 2011, 09:50:14 pm
content: Thanks a bunch for replying!Quote from: NiHaoMike on December 27, 2011, 08:31:20 pmYou mean the second stage RC filter? I basically copied it from an article because I didn't know whether having too much capacitance would be worse than none at all:http://www.embeddedrelated.com/showarticle/110.php. Assuming 10ksamples/second/channel, should I am for < 5kHz here? And should I prefer higher resistance over higher capacitance, e.g. a 68k resistor there with the same 470p capacitance, in order to avoid stability problems?QuoteI did as best as I could with a 2-layer board, maybe I should just rip up the voltage trace and use a fly wire :-) This is the one part where I feel a differential input would have been substantially better, but then I would need a separate ADC. Perhaps in another revision.QuoteGood idea. I tuned the voltage channel, which has a rather delicate balance with the divider resistors, in LTspice then copied it for the current channels but there's no reason for the current channels to have such high input impedance. Empirical testing suggests that a 10K load across the transformers doesn't disturb readings measurably.QuoteI'll see what I can do about these. I'm more interested in keeping the parts count down (especially the number of unique parts, using the same resistor/capacitor values where a little slop is acceptable) than in squeezing out every last decibel. Thanks again for the suggestions.

author: NiHaoMike
author_rank: Super Contributor
timestamp: December 28, 2011, 12:38:00 am
content: The dsPIC's internal ADC is impressively fast. The one I'm using can do up to 500kHz sampling at 12 bits. You can oversample many times (my code oversamples 16 times for the voltage and 8 times for the current) in order to ease analog filter requirements and increase resolution.

author: Jimmy
author_rank: Regular Contributor
timestamp: December 28, 2011, 01:48:16 am
content: As I always say to people if you do your own house wiring don't bother with home insurance as they probably won't pay out on a fire even if it wasn't your wiring that started it.O and I hope that double insulated able coming out the front of you swichbard and I hope you dont have any pets

author: gxti
author_rank: Frequent Contributor
timestamp: December 28, 2011, 02:04:15 am
content: They're current transformers. They clamp onto the outside of the existing wiring, insulation intact, and are completely galvanically isolated. And although I should probably put some sleeves over those wires, even if they short out the worst that could happen is that they stop reading properly -- the most current that can flow is whatever the transformer is specified for, probably 10-100mA.But unfortunately you're probably right about the insurance. Itlookslike a bodgy wiring job, therefore itisa bodgy wiring job.

author: The_Penguin
author_rank: Regular Contributor
timestamp: December 28, 2011, 03:00:11 pm
content: Quote from: Jimmy on December 28, 2011, 01:48:16 amUp here in the frozen North we get a permit, and 2 inspections. One rough, one final. Insurance would pay without an issue.

author: gxti
author_rank: Frequent Contributor
timestamp: December 29, 2011, 05:25:54 am
content: Can anyone recommend a good (short) guide on ADC filter design? I've re-tuned for a 3KHz knee in both the active and passive stages but now I'm getting a substantial error (from the phase delay, I think). Here's the new current input circuit:Transient analysis in LTspice shows a 4.2% RMS error with this configuration, 90 degrees out of phase with the input. An AC sweep of the output gives 177.592 degree phase shift at 60Hz, which I honestly have no idea if that's good or bad. 180 would be ideal since the amplifier is inverting, right? At 1KHz it's up to 140.653 degrees. Halving both capacitors nets 2.1% RMS error, 178.795 degrees, and 159.72 degrees, respectively. I suppose this is where the oversampling comes in handy, since every frequency doubling on the filter reduces the phase shift at 60 Hz. Any tips?In unrelated news, I'm going to change from USB to Ethernet which complicates things quite a bit. I'll break the prototype into three separate boards for the ADC/DSP, the Ethernet bridge (PIC18F66J60 based, very nice), and the PSU. The latter will probably be completed later once I've measured real-world power consumption. I've also dicked with the filter gain (68% now) to deal with the lower supply voltage.

author: jahonen
author_rank: Super Contributor
timestamp: December 29, 2011, 08:20:00 pm
content: Quote from: gxti on December 29, 2011, 05:25:54 amI'd use 2nd order filter stages (like Sallen-Key, perhaps a state variable one etc.) with Butterworth filter frequency response and put the corner frequency to something like 5 kHz. This should not require additional components since you already have an opamp in the circuit. Due to steeper filter knee, lower frequencies are not so much affected. Also, if you produce same phase shifts in both voltage and current channels, then it should not affect the real power measurement, since both are delayed by the same amount.It would be probably best if one could use as much oversampling as possible and then perform post-filtering with software, optionally with decimation if possible.Regards,Janne

author: gxti
author_rank: Frequent Contributor
timestamp: December 29, 2011, 10:07:37 pm
content: I did a bit of research since the last post, it looks like phase shift isn't the direct concern but rather its companion "group delay". Definitely a case of "didn't go to school". A consistent delay across the pass-band is OK because as you say all the channels will be delayed by the same amount, but if it is skewed across frequency then higher-frequency components will be more/less delayed than the 60Hz voltage reference. So I've been playing with a multiple feedback topology with a Bessel filter, since Bessel has the lowest group delay skew, and results are promising. The only downside is that the number of passive parts nearly doubles, but I can live with that. I still need (or at least I've convinced myself that I need) a RC post-filter to act as a charge reservoir for the ADC, but I can tune it up to where it is nowhere near the pass band, e.g. 1K/2.2n (re-using values from elsewhere in the filter).The real question though is whether this is complete and total overkill. Sure the group delay skew is 2.2 nanoseconds at 1KHz, but is that necessary? I honestly have no idea.

author: gxti
author_rank: Frequent Contributor
timestamp: December 30, 2011, 05:31:36 am
content: New current channel performs much better. Nice, linear phase response (constant group delay) up to 1KHz where the cutoff begins. The bad news is that I've just shot the voltage channel in the face. There's no way that this filter can tolerate the high source impedance (> 100Kohm) of the voltage divider. Of course it was bad before, too, I just hadn't realized it then. Now I have to look for ways to lift and buffer the delicate voltage signal without destroying it, probably another op-amp. So much for the "low parts count"! I love this MFB filter but it adds 7 unique parts onto the pile, too.

author: gxti
author_rank: Frequent Contributor
timestamp: January 17, 2012, 12:26:16 am
content: The prototype boards were finalized and sent to the fab last week. Hopefully they should arrive in about a week. The present front-end has one voltage and one current channel, using a buffer amp and a MFB Bessel filter in identical configurations for each channel. Flat phase response and equal group delay means no phase shift between the channels. Here's the final schematic for the front-end:Today I discovered MCP3901http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en544175-- a dual converter ADC with built in filtering and oversampling and specifically targeted at power monitoring applications. This would net 16 to 24 bit resolution (vs 12 with the dsPIC, not including oversampling), possibly simpler filter design, and simultaneous sampling of current and voltage inputs. Differential inputs are a nice bonus, too. Since it only has the two inputs I would use an analog mux; this also means I probably only need one analog pipeline for all of the current channels. Depending on how much muscle I need perhaps I can even lose the dsPIC and have the ethernet chip (PIC18F66J60) do the calculations (unlikely) or at least downgrade it to something cheaper.

author: Rerouter
author_rank: Super Contributor
timestamp: January 17, 2012, 01:35:48 am
content: only point i am curious about is how you will calculate the power factor, e.g. phase offset, being how switch mode supplies like those in your microwave don't exactly produce a clean sine,other that that, you can get alot of line quality status out of that, you could even go as far as to calculate sine purity, or have it tell you whether its a capacitive, inductive or switch-mode type supply degrading it

author: gxti
author_rank: Frequent Contributor
timestamp: January 17, 2012, 02:01:38 am
content: Using phase offset to find power factor doesn't work for non-sinusoidal loads, as you point out. The true method is from the definition: "power factor = true power / apparent power", where "true power = RMS(voltage * current)" and "apparent power = RMS(voltage) * RMS(current)". This is why I'm sampling both voltage and current with high resolution, rather than just looking for zero crossings with a transformer or optocoupler.As an addendum to my previous post, I found the MCP3909 which does 90% of my application for me. Hilarious. I could put 3 of those down (one for each current channel) and still come out ahead on cost, but I think I'll try for the analog mux anyway. Either way I definitely don't need a dsPIC just to periodically calculate RMS and divide. And as fun as it was to design that Bessel filter, it's totally unnecessary at a 900khz sample rate.

author: amspire
author_rank: Super Contributor
timestamp: January 17, 2012, 02:08:48 am
content: That is a nice job picking the right filter. Since you have the simulation going, it is worth running several passes with the components near their tolerance limits to see how the filter holds up. Anyway it looks like you will be replacing the circuit now anyway. A pity after the good work.The circuit seems to assume that earth and neutral are at a similar potential, and you cannot assume that. When there are lightening strikes in the area, you can get hundreds of volts between earth and neutral, even if they are connected at the switchboard.Either the whole circuit uses Neutral as the common connection, and you do not use earth at all, or you could use a transformer to isolate the voltage input, and nothing else in the circuit connects to the mains directly. The current sense is already transformer isolated.Richard

author: gxti
author_rank: Frequent Contributor
timestamp: January 17, 2012, 03:17:29 am
content: Earth is not presently used anywhere in the circuit. The terminal is there but if I don't have anything to connect it to (shielding or EMI caps, I guess) by the time the design is finished I'll just remove it. All of the low-voltage lines leaving the box are galvanically isolated and are not referenced to anything external, except for maybe an unisolated I2C bus between neighboring units so that they can share an Ethernet line. As for neutral, it is pinned to Vcc/2 and acts as a reference voltage for the amplifier chain. This way I don't need a capacitor to couple the mains divider in, which would totally kill my phase response. In other words: circuit ground (not earth) = 0 volts, neutral = 1.65 volts, Vcc = 3.3 volts.MCP390x on the other hand is designed to be ground-referenced and tolerates +/- 6V on the differential inputs, very nice! If I can get my power requirements down I might be able to go back to a non-isolated supply with half-wave rectifier for the final product, since it can have circuit ground = neutral without needing isolation.And as for the original filter design, I'll still be assembling it and playing with it just to see how reality stacks up to LTspice. I need to get or make a signal generator to test it out properly. Luckily there are quite a few reviews and recommendations lurking around on this forum.

author: jahonen
author_rank: Super Contributor
timestamp: January 17, 2012, 06:29:25 am
content: Quote from: gxti on January 17, 2012, 02:01:38 amActually, true power is just an average of product of instantaneous voltage and current, so true power = mean(voltage(t) * current(t)).Regards,Janne

author: gxti
author_rank: Frequent Contributor
timestamp: January 18, 2012, 01:39:17 am
content: You're right of course. I always get those mixed up. Power is already "squared" so no need for RMS, just a simple mean average.It looks like I have two choices: use two 3901 to cover 4 channels and replicate the required LCR network for each channel (at least it's not a two op-amp MFB Bessel filter...), or use an analog mux to feed 3 or 4 current inputs through a single network to a single 3901 channel and the voltage into the other channel full-time. The mux is appealing because the total parts cost and perhaps board area is less, and I don't need to process 2x as much data or to synchronize and service the two frontends. I don't mind only getting partial data, I'm not building a revenue meter here.The first mux problem is that it requires a dual supply if I want to keep the signal ground-referenced, and the 3901 requires that it is. So I'll need a very small negative rail supply. Maybe there's a clever trick with 34063 switchers to get two rails, I'll have to look into it.The second problem is that I don't know how the mux is going to mess with my filter network, especially since I need to attenuate the signal precisely. The source impedance of the current transformers is not very good (a downside to getting pre-terminated ones) so I will need < 50 ohms resistance in the mux in order to keep the attenuator precise. It doesn't look like I can get a dual-supply, < 50 ohm, 4:1 mux for a reasonable price (compared to using two converters) so another option is using the high-resistance ones with a buffer amplifier afterwards. I've never used muxes before so I could use some advice :-)

author: amspire
author_rank: Super Contributor
timestamp: January 18, 2012, 01:50:49 am
content: Wouldn't it be easier to get one of the power monitoring IC's that include the Volts x Current multiplication on the IC? Eliminates the need for a DSP processor.Perhaps something like this:http://www.analog.com/en/analog-to-digital-converters/energy-measurement/ade7953/products/product.html

author: gxti
author_rank: Frequent Contributor
timestamp: January 18, 2012, 02:34:46 am
content: That part costs more than my deliberately oversized DSP :-) But Microchip has similar reasonably-priced parts in the MCP3905, 6, and 9. The 3905 and 6 have only a frequency output (proportional to real power) and the 3909 is basically a 3901 with the multiplier and frequency output. The digital output on the 3909 can access the voltage, current, and multiplier outputs but not all at the same time. Also all three are tapped before the built-in filtering which means I have to filter it in software anyway. According to a MCP3901 appnote that takes the software route like I am, a typical PIC18 can just barely handle one continuous power calculation if you cheat a little. Since my other uC also has to handle networking duties in a timely fashion, I'd rather not risk it. Using the chip's multiplier also means I have to either use a mux or use one chip per channel since each chip has to see the voltage, whereas with the 3901 I could have a chip with two current channels. Having the DSP nets me a lot of flexibility for not a lot of money; killing it is something to look into for revision 2.Continuing from my last post, I was just about to flip-flop back to having two 3901s when, while researching the previous paragraph, I discovered there is one more 390x part -- the 3903, which is 3901's fat cousin with SIX FREAKING CHANNELS. It's designed for three-phase power but I can use it for three one-phase measurements, or up to 5 if I give up the built-in phase compensation. Now I don't need two 3901s, just one 3903. Case closed.

author: gxti
author_rank: Frequent Contributor
timestamp: January 31, 2012, 06:24:39 am
content: Been spending lots of time learning Solidworks. Don't know how far I would have gotten without it! As you can see there are three PCBs. One handles Ethernet (and I plan on reusing this board in other projects), one handles ADC and DSP duties, and the middle one is the power supply. At this stage I plan on getting some cheap 5V plug packs, sawing them open and hot gluing the PCB in, because it's very cheap and very effective. A finished product would have a single PCB with all three functions. Batch PCB orders are great but because they are priced by the square inch I have incentive to make the boards as small as possible.The first round of PCBs including the Ethernet module should arrive tomorrow, hooray! I am probably the world's slowest electronic designer.Some Solidworks porn:

author: Unknown
author_rank: Unknown
timestamp: January 31, 2012, 09:30:25 am
content: Good job.As a regular user of both Solid Works and Sketchup I would have chosen the latter for this one, its less fiddly.

author: gxti
author_rank: Frequent Contributor
timestamp: February 04, 2012, 11:05:45 pm
content: First round of boards are here and assembled. I got the ethernet widget, the DSP board, and an isolated USB UART. I haven't put together the DSP yet, I may try to write the code for it first just to see how much muscle I need, like I should have done before ordering the board in the first place. Either way I will definitely have a discrete microcontroller handling DSP duties, even if it isn't a dsPIC.Ethernet widget:Bigger pictures:http://www.flickr.com/photos/suigetsu/6819358033/#sizes/o/in/photostream/Gerber and source materials:http://hg.partiallystapled.com/circuits/ctservice/file/6bbe0b193318This is more or less final. The next revision will have EEPROM for writing a MAC address to and uses JST PH connector for serial so that it locks firmly, but is otherwise identical. I might switch to a more expensive ethernet jack.USB jumper:Bigger pictures:http://www.flickr.com/photos/suigetsu/6819357919/#sizes/o/in/photostream/Gerbers and source materials:http://hg.partiallystapled.com/circuits/usbjumper/file/03a5b0f173f0Not much to say here, it's a MCP2200 with an isolator (RF type because it happened to have the right channels) and a N-MOSFET to pull down the reset line on a chip for programming. Obviously handy for programming a device that might be attached to the mains.

author: gxti
author_rank: Frequent Contributor
timestamp: February 10, 2012, 03:50:50 am
content: The Ethernet widget is working flawlessly. It takes packets off the UART and transmits it as a UDP multicast datagram. I'll be adding an EEPROM to the design for storing a MAC address, but I can also just write a script to munge one into the .hex file of the bootloader. The bootloader won't erase itself, so the firmware can still be updated "in the field" without losing its identity. But enough about that, let's talk about DSPs.I'm going nuts with all the possibilities for the RMS and power calculations in the DSP firmware. I'd initially assumed I would do some sort of continuous conversion scheme, where new values are pushed into a ring buffer and periodically compiled to produce outputs. The amount of RAM required for this is downright silly -- with 2KB of RAM and 5 channels times 32 bits (16 each for power and current) I can only store 80 samples per channel, which even at maximum oversample is only one cycle at 60Hz! I'd have to prescale the sample rate just to grab a few cycles so that the "windowing effect" (variations in the calculation dependent on how much of the waveform was sampled and at what phase) would be acceptably small. And scaling back means lower resolution, which reduces accuracy, especially with jaggy current waveforms. I could probably produce acceptable results this way, and there are dsPICs with more RAM, but maybe there's a better way.So then I went looking for app notes and ran across Analog Devices AN-578. While it is targeted at low-power micros while my app is using a dsPIC30, the approach is still interesting. It uses a batch processing technique where thousands of samples are collected over a period of 2.3 seconds, then it stops collecting data for a second while it computes the RMS outputs. The problem of course is that I don't want to stop collecting. So another possible spin on batch processing that I came up with is to use two accumulators instead of big sample buffers. Each incoming sample is squared and divided down by some fixed factor, then added to one of the accumulators. After a hard-coded number of samples have been collected, the accumulator is square-rooted and spit out to the UART, then reset back to zero. Meanwhile, new samples are directed to the other accumulator. This means only two registers per channel, much better than a sample buffer! The only downside I can see is that I can't apply any post filtering. The ADC is already oversampling by a factor of 256 and has a built-in SINC filter, but I really have no idea what kind of filtering I might need beyond that. But at least I am confident enough that I can push forward with the hardware I've laid out around the dsPIC30, and play with the firmware all I want.Just to give an idea of what data I've got coming in, the ADC is simultaneously converting 5 current channels and 1 voltage channel at 1.25Msamples/sec. I am using the max oversample of 256 times, reducing the data rate to 4882 samples per second. There is also a pre-scaler that can reduce the sample rate, if necessary. The DSP can do up to 20MIPS @ 80MHz, or 30 MIPS if run at 5 volts which I was not planning on since the ADC's digital interface has a max voltage of 3.6V. I do, however, have a 5 volt supply because the analog side of the ADC requires it. Wacky.

author: amspire
author_rank: Super Contributor
timestamp: February 10, 2012, 05:07:58 am
content: You can keep a running average, just like any analog meter does.I described a simple method in this post that hardly needs any memory:https://www.eevblog.com/forum/projects-designs-and-technical-stuff/design-of-a-digital-energy-meter-using-a-pic/msg88309/#msg88309https://www.eevblog.com/forum/projects-designs-and-technical-stuff/design-of-a-digital-energy-meter-using-a-pic/msg88528/#msg88528You can adjust the damping of the RMS output by varying the division number for the RMS sample accumulator. In my example, I used a multiplication factor of 256, but with a DSP chip, you will want to increase it a lot. I think 4882 samples a second is a bit low. A 16 times oversample and around 50K samples/second would give excellent accuracy with difficult waveforms.With about 50K samples/sec, a multiplication factor for the RMS accumulator of 212would work well for a smooth output. Each second 70% of the reading would be from the data in the previous second, and 30% would be from the data before the previous second.If you also want to keep track of a very accurate total power, you just need a second 64 bit RMS accumulator register, plus a counter for the total number of samples. When you want to get the total power out, divide the number in the 64 bit accumulator by the sample count. With a 10 bit A/D, you could fit several years worth of power with zero dropped samples.Richard

author: gxti
author_rank: Frequent Contributor
timestamp: February 11, 2012, 05:44:24 pm
content: Ah, an exponential moving average. This is why I post -- someone always points out the obvious thing that I should have done, and would have figured out eventually if I weren't so busy plunging headfirst into the first thing to pop into my head. So thanks for that. I wrote a Python script to tease out the constants I should use to get minimum error. It looks like a value where 86% of the average (it's easily calculable from the "alpha" constant) comes from the last ~1 second gets error below 0.1%. 1 second is relatively slow but perfectly acceptable to me and this is an easy thing to implement even without a DSP, so I'm starting here first. Later I'll look into more advanced filtering that might let me reduce that time constant while still effectively suppressing 60/120Hz noise.The naive C implementation I came up with first gets about 4800 loops per second with just the math and no SPI. Since the compiler seems not to know how to use any of the DSP and seemingly some of the ALU operations at my disposal and is calling library routines to do multiplication and division it seems that assembly will get me very far here. If I don't post for a few days I've probably gotten trapped under a pile of datasheets and programming references.On a side note, apparently PICkit3 has trouble talking to a dsPIC30 that's fused to start at 120MHz. I had to undervolt the chip until it cried uncle to get it erased. If I do that again, I think I'll configure it to start up without the PLL and wait a bit first, just to make sure I can program it again. At 80MHz (which is the max speed for 3.3v operation anyway, I'm just messing around) there's no issue.

author: gxti
author_rank: Frequent Contributor
timestamp: March 27, 2012, 01:52:22 am
content: The prototype is assembled. I bought the wrong size oscillator but heck, it's only 10MHz:All of the hardware seems to be operational, and I already have some code written from when I was proving out the speed of operation, but it's completely untested so it'll be a few days before I have something functional. I also need to look into buying a signal generator to test this properly.

author: gxti
author_rank: Frequent Contributor
timestamp: April 09, 2012, 04:21:59 am
content: Having problems talking to the ADC via SPI, it's just not responding. But I poked it with my oscope and saw something funny:Note the scale: there are 20-80mV pulses coming back from the chip. The timing is about what I would expect if it were responding to SPI; that yellow trace is the chip select line. I've triple-checked that the reset line isn't asserted, and I'm fresh out of ideas. Anyone recognize this problem?

author: amspire
author_rank: Super Contributor
timestamp: April 09, 2012, 07:09:14 am
content: I would guess that something is dragging down the data line. Either the input on the micro is configured as an output, or something else on the board is dragging it down. Connect a 10K resistor to the 5V rail from the data line and see what voltage it rises to after the A/D chip completes sending data, and also when the chip select goes high. I have a suspicion the line never goes tri-state at all.Richard.

author: amspire
author_rank: Super Contributor
timestamp: April 09, 2012, 08:02:23 am
content: Edit: Deleting post to wrong thread .........I was wondering where that post on FPGA's had got to.Thanks gtxi and hans.Richard

author: gxti
author_rank: Frequent Contributor
timestamp: April 09, 2012, 05:26:45 pm
content: Good advice, but I think you posted in the wrong thread, mate :-)

author: hans
author_rank: Super Contributor
timestamp: April 09, 2012, 06:47:40 pm
content: I also think your data lines may be shorted out. Maybe you could try and fix your online repository? I'm kinda interested in this kind of project, but when I download the archive files, it says it has an unexpected end of file. The files itself aren't downloading fine neither, Altium can't open them.

author: gxti
author_rank: Frequent Contributor
timestamp: April 09, 2012, 07:37:30 pm
content: Oops, there were multiple things wrong with the hg repo. Obviously I haven't tried pulling over HTTP before. All good now!Note that if you're using the archive download (zip file) you'll have to grab the contents of the "alib" repo too (http://hg.partiallystapled.com/circuits/alib) and put it at lib/ . I use TortoiseHG (http://tortoisehg.bitbucket.org/) to keep everything in sync.

author: gxti
author_rank: Frequent Contributor
timestamp: April 10, 2012, 05:10:39 am
content: Figured it out: the clock polarity was wrong. My fault for assuming the default SPI settings on a Microchip uC would match the required SPI settings for a Microchip part, and then forgetting that I made that assumption. This particular part (MCP3903) uses two address bits at the start of each communication and will not take control of the bus until it receives the entire first byte and confirms that the address bits match. I've been having so many problems with the microcontroller that I thought the same thing as everyone else, that there was a short on the line or that the TRIS register was set wrong or the buffer was fried or any number of other problems. But it turns out that the chip was just waiting to see its own address before responding, and I guess there's enough leakage that 20mV of signal gets through even when it's tri-stated.But now it's getting late, so getting the first bits out will have to wait until tomorrow. Small victories.

author: hans
author_rank: Super Contributor
timestamp: April 10, 2012, 05:15:10 pm
content: Yes this could be ground bounce, cross talk or whatever. I happened to have prototyped the MCP3903 in the weekend (I finally got some 28-pin SSOP/SOIC protoboards), but didn't have a suitable crystal yet (only 25MHz around). If you get any results of the outputs noise etc. I'm very interested in them!I may post a project topic here about the development of my own DC PSU monitor I've been planning to make. I hope to get 10ksps output maximum and be able to see microamps and below. I may be using 2 shunts of 10 ohm and 0.1ohm and autorange them at the sample frequency. (theoratically measuring resolution of <0.1uA up to 1.25A tops).I've done an internship once that did the same thing.. except it measured across 1 ohm and only sampled slower. To measure the microamps there were some ridiciously high gains (up to 5000x) involved and a very expensive bipolar 16-bit ADC ($40 each, ouch! One-off, luckily). The results were reasonable but felt it could be done more novel. The board ended up being larger than eurocard(!!) full with amplifiers, linear PSU's and connectors.This ADC looks much more promising because the high resolution effectively adds more gain (well it means I don't have to add more gain), as well as the PGA.Anyway, that's a bit offtopic.. I am very interested to see progress on your (AC) power monitorGlad to see the HG repository works, I will have a little look on your project.

author: gxti
author_rank: Frequent Contributor
timestamp: August 25, 2012, 06:49:58 am
content: It works! Readings from my desktop PCs:Volts: +121.61Amps: +2.96 +0.00 +0.00 +0.00 +0.00Watts: +341.57 +0.00 +0.00 -0.01 -0.43Volt-Amps: +359.68 +0.00 +0.00 +0.00 +0.00Power Factor: +0.95 +1.00 +1.00 +1.00 +1.00Right now the computer is doing the square root part of the RMS, scaling to real-world units, and calculating the VAR/power factor, but the "hot loop" is done and working great. Still have to deal with those pesky "negative watts" that creep into idle channels, probably by making use of all 24 bits coming from the ADC instead of 16 and carrying the extra precision on to the averaging accumulators. I passed over that initially because I will probably have to hand-write the assembly code. Will be upgrading to 24 bit precision and adding the square root and scaling code in the very near future.And there's always room for another hardware revision...

author: hans
author_rank: Super Contributor
timestamp: August 26, 2012, 07:00:20 am
content: Looks awesome. Why do you think they have a habit of getting negative values? Is it just an offset error in the MCP3903 front-end, possibly enlarged if you enable the gain of the device.Oh and by the way, the 24-bit precision isn't *really* 24-bit. You get 20-bits+ if you sample at a few Hz (I believe there is a table confirming that). The resolution drops fairly quickly. At full sample speed it's only a 14 bit converter or something. I believe I saw like 2kHz somewhere in your code, so I guess the converter is 16-17 bits. For e.g. look at table 4-2 in the datasheet of the MCP3911;I experimented with this deviceand has similar specs (in fact, they claim a bit higher SINAD)I doubt the 24-bit will gain much extra, except a lot more LSB noise (yeah like 6-bits wide).

author: gxti
author_rank: Frequent Contributor
timestamp: August 26, 2012, 09:14:24 pm
content: I'm certain that it is due to a lack of precision in my accumulator, which is currently 32 bits. The EMA factor "alpha" is 2^-10, which means that up to 10 LSbits of the accumulator can get "stuck" if the input isn't changing, and with a LSB of 0.83mW that the output can be up to 0.83*1024 = 0.85W off. Adding 8 or more bits of precision to the accumulator should solve the problem. I'd use the full 24 bits from the ADC because otherwise I'd just be filling that space with zeroes anyway.I recompiled with 64 bit integers and things do indeed look better, problem is it's twice as slow as before so I had to reduce the sample rate. Additionally, the amps now don't go quite to zero and when multiplied by 120V I get on the order of 1 volt-amp. For some reason I don't have that problem when using 32 bit accumulators. I will check whether the 40 bit accumulators in the dsPIC30 are going to be of any use, and either way rewrite the averaging loop in assembler in the hopes of not taking that full 64-bit penalty. I don't need 64 bits, just 48 at the most, and only that if I want to do multiplies with the 24 bit input.Regarding ADC precision, you're right of course about effective number of bits. It isn't mentioned in the MCP3903 datasheet but from the SINAD figures given it looks like 14.3 bits in my configuration. That's ok though, that translates to 3.3mW precision which is still great. But I still want to mix that extra 8 bits of noise in because it will smooth out truncation errors in the averaging process, and it's essentially free. I'm using it anyway when "calibrating" for the zero offset error.

author: gxti
author_rank: Frequent Contributor
timestamp: August 28, 2012, 04:48:25 am
content: Looks like the volt-amp noise is systematic and is unfixable. For example, the current transformers I'm using have a factor of 15.9mV/A, and there's an extra 5x attenuation in the ADC input stage. That means that 20uV of noise at the ADC pins multiplies up to 6.3mA of "measured" current, times 120 volts yields 0.75VAR. This really isn't that bad, it just means that below 10 watts the VAR and power factor measurements are lost in the noise. I'll take this over the negative half a watt when there isn't enough precision in the accumulator.

author: gxti
author_rank: Frequent Contributor
timestamp: October 29, 2012, 02:38:33 pm
content: Almost done with revision 2. Redesigned from scratch to be a complete unit, just hook up mains and up to 11 current transformers and plug it into any Ethernet network. The micro-controller is now a STM32F107 instead of dsPIC30, both because I'm standardizing on ARM and because it's faster and more capable. I expect that even though I've tripled its duties it will keep up without breaking a sweat. The STM32 should be much nicer at 72MHz especially since it doesn't have to execute-in-place from flash for critical sections.The product also has USB (since the STM32 supports it, why not?) and a header for I2C expansion. That will mainly be intended for driving a separate relay pack for switching the circuits that are being measured. There's a 1mbyte SPI flash for storing content for a web interface as well as for user-supplied plugins to interact with the data. And of course everything has 3kV isolation between the mains side and the processor/comms side.The power supply is definitely the thing most likely to burst into flames. I'm using a Linkswitch-TN based non-isolated buck converter to run the mains-side stuff, and then a little dc-dc module to bring it across to the safe side. There isn't a lot of headroom for running relays or other accessories so those will have to bring their own supply. Bring-up will definitely be a challenge, I don't want to solder all the expensive stuff down if the PSU is just going to blow up, but I don't really want to solder all that stuff down after the big electro caps are on, either. Final BOM and price still to be determined but from earlier estimates it should be < $75 per unit in small quantities.

author: gxti
author_rank: Frequent Contributor
timestamp: November 21, 2012, 05:25:23 pm
content: Revision 2 is assembled and has feature parity with revision 1! Now to decide what, if any, RTOS to use and see how hard it is to kludge lwIP in so I don't have to write a full IP stack myself.Before reflow:https://secure.flickr.com/photos/suigetsu/8198487928/sizes/o/in/photostream/Fully assembled, other than the connectors and the switching power supply:I bought the wrong switcher IC footprint, hence why that's not populated. A shame too, that's the part that was going to burst into flames and cause much excitement!I also threw together a little board with 8 MOSFETs for driving relays, the idea here is to use the power monitor to find where the load is and shut unnecessary loads down during peak hours. It fits the same size box with one at each end and communicates via I2C with the main board:

author: hans
author_rank: Super Contributor
timestamp: November 21, 2012, 05:42:59 pm
content: Looks really decent a nice lay-out. Those ARM proccesors are a treat, so much more capabilities than a simple PIC. Also the discovery boards are neat with their built in ST-LINKMaybe there is a component you can remove (the isolated power converter?) to protect the expensive parts of your design in case the regulator fails or something. I think I see a solderjumper there, so that shouldn't be a problem I thinkThere are many RTOS out there, but freeRTOS seems very popular and widely used (also many platforms supported). As all Cortex m0 share the same ARM core, it's likely there is a compatible driver for your core.Otherwise it isn't *That* hard to write your own. It's a matter of getting a timer to fire interrupts at 1kHz and implement SwitchContext and RestoreContext ASM routines (which is not much more than pushing proccesor registers onto stack, change stack pointer, and popping registers back and continue running). I figured one out running on a STM F4 Discovery, and only took like an afternoon or so.I think it's an added value for systems that run computations, serial terminals, ethernet simultanously. The STM32F107 has relatively a lot of RAM so you can easily buffer data between tasks so they can do their thing (for instance, buffer data to be sent for ethernet which will be handled separately).What kind of chip are you using for ethernet?

author: gxti
author_rank: Frequent Contributor
timestamp: November 21, 2012, 06:14:42 pm
content: The uC is a STM32F107 which has a built-in MAC, so the ethernet chip is just a generic Micrel 100Mbit PHY. The only ethernet programming I've ever done before was for a PIC18F66J60 by copy-pasting Microchip's (apparently low quality) stack, so this should be exciting. My main interest in having a RTOS, aside from being able to easily push work to a lower-priority thread, is to let users upload their own plugins to make use of the ethernet interface. There are lots of "internet of things" type websites around and having a plugin system would enable easy upload of data right from the meter, as well as a pretty web interface etc. There's an 8mbit SPI flash onboard so lots of room for user programs and data.And yes, I included a jumper between the power supply and the analog electronics. The ADC is on the "unsafe" side of the board and I wanted to be able to use it even if the supply failed, so not fitting the isolated converter isn't really useful. In the next revision I will probably downsize and also lose the high-voltage stuff so I can sell it to friends without losing sleep. But the basic framework and software won't change much, just the system design.

author: T4P
author_rank: Super Contributor
timestamp: November 21, 2012, 07:12:53 pm
content: FreeRTOS is good but the best RTOS for STM32 is the ChibiOS since it's written by somebody actually in ST

author: HackedFridgeMagnet
author_rank: Super Contributor
timestamp: March 14, 2013, 01:59:17 am
content: Interesting project gxti, thanks for sharing.Do you have any more figures for the metering accuracy your are getting? I am interested in 230V for low power measurement, basically as low power as I can go with 1% accuracy, for a similar price BOM. Any tips recommendations?