Idle question regarding interrupt based sampling of sensor data

On a previous project I worked on, we had a small micro sampling data in an interrupt routine, and then processing and sending the data once a certain condition was met in the main program loop, all the while still sampling data via that interrupt.  We were fairly short on processor power, so the interrupt had to be fairly optimized, as did the calculation code. 

Has anyone considered that sort of setup here?  Are there any obvious reasons not to do it that way, such as for instance the Arduino not having the processing power to handle it?

I don't think the added accuracy is that terribly important, but I did notice that when I changed my sampling delay from 1 s to 2 s I started missing the startup current surge from my fridge more frequently.  Not a big deal, but seeing that surge makes it easier to identify the fridge as the culprit, as opposed to a light left on somewhere in the house.

Just wondering if anyone has ruled this out already.  

Wade

arvidb's picture

Re: Idle question regarding interrupt based sampling of sensor data

I've been looking into this a bit. Looking at the hardware spec, it should be possible to do full three-phase current and voltage measurements using a single emonTX. One would need to sample first and calculate later, i.e.

1) sample a couple of wavelengths
2) do the calculations over the complete data set
3) send the calculated data
4) sleep
5) repeat

in contrast to today's:

1) take one sample and calculate
2) repeat for 20 half-waves
3) send the calculated data
4) sleep
5) repeat

To sample 3-phase, I & U, two whole 50 Hz wavelengths at 1.5 kHz:

* total sampling frequency = 6 channels * 1500 Hz = 9 kHz
* total storage = 2/50 (sampling time) * 1500 (freq) * 6 (channels) * 2 (bytes/sample) = 720 bytes

9 kHz sampling rate is possible while retaining the full 10 bit resolution. The chip's got 2 kB of RAM, so depending on other memory usage, it should be enough (I haven't investigated this yet).

Also, it should be possible to compensate for voltage phase shift by simply shifting the sampling of the voltage in time instead of the PHASECAL calculation done in EmonLib today.

Robert Wall's picture

Re: Idle question regarding interrupt based sampling of sensor data

Has it not been done? Here: http://openenergymonitor.org/emon/node/467

The reason for measuring a short period then sleeping is to extend the operating period when battery powered. If mains powered, that is obviously not a concern.

arvidb's picture

Re: Idle question regarding interrupt based sampling of sensor data

Great! Thanks for the link!

I did some thinking and realised that even at 4.5 kHz sample rate, one sample period is 4 degrees phase shift at 50 Hz. So using that kind of shift instead of the PHASECAL calculation won't do.

The reason I looked at interrupt driven sampling (or rather free-running sampling) was that I saw that the HW should be able to handle three-phase current+voltage measurements, and it seemed a bit excessive to use three emonTXs for that then. I thought it was simply not possible to sample often enough when doing calculations in-between each sample.

But since the emonTX is sleeping most of the time anyway, one could just sample current+voltage for one phase at a time. It should just be a matter of adding the needed voltage input circuitry.

It looks like you manage about 5 kHz sample rate in Robert's 3-phase code: sample delay for L3 is 17 samples = two phase-to-phase periods; 17*3/2 would be # of samples for a complete cycle, and x4 since you sample three currents and one voltage = 102 samples/cycle, or 5100 per second. Free-running sampling could raise this to some 9 kHz, which perhaps would increase the accuracy (?), but it doesn't make a difference as to how many phases one can sample. More phases just mean less sleep (and more power draw).

MartinR's picture

Re: Idle question regarding interrupt based sampling of sensor data

The main problem with using a single voltage measurement for a 3-phase system is that the voltages can vary quite significantly from phase to phase. I regularly see 10V between phases in my house. This will be an unknown error if you only monitor a single phase.

I don't think there is an issue time wise

arvidb's picture

Re: Idle question regarding interrupt based sampling of sensor data

Yes, just like you did in your (beautiful!) setup, I'd like to measure the voltage of each phase too. And what I'm trying to say (in my own convoluted way ;) is that this can be done with a single emonTX, if one just adds the circuitry for two additional voltage inputs.

Using interrupt-driven sampling one could additionally increase the number of samples per phase and 50 Hz cycle to ~90, from today's ~50. Which may not matter much...

I'm just waiting for the USB-to-serial programmer to be in stock again so that I can order my emonTX and start coding. :)

MartinR's picture

Re: Idle question regarding interrupt based sampling of sensor data

You're right of course, it's straight forward to add the extra circuitry for more inputs on a breadboard but with emonTx PCBs only costing £5 it would make sense to use those just for the time it would save wiring the breadboard. When you get to that point you have to consider that an ATMega chip is also only £5, and you get 3 times the processing power - that was my train of thought anyway. The stackability of emonTx boards makes things so easy. I don't know if this was a design feature or pure luck but either way it was a stroke of genius by Glyn and Trystan.

I'm running interrupt based code of my system. It's a 3-phase version of the code I posted here: http://openenergymonitor.org/emon/node/1535

A normal ADC conversion takes about 120us so if you're doing 4 per sample that's a minimum of 480us for each sample, or 41 per mains cycle. You can speed up the conversion time by changing the ADC clock. I double it in my code above so each conversion takes about 60us.

Thanks for the compliment on my system BTW, it's grown a bit now with 3 power controllers. I'll post some more pictures soon.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.