Concurrent CT and Pulse inputs on EmonTX (v3)

I've got an AC-AC powered EmonTx v3 that's got a single CT input at the moment. I'd like to add a hardwired pulse signal from the Gas meter but I'm not sure this is a simple as it seems.

Apologies in advance if this makes no sense, but here goes...

The pulse counting examples seem to work by attaching an interrupt to the digital input, whereas the CT calculation (with AC-AC) samples the AC waveform to calculate the RMS voltage. So what would happen if there's an interrupt from a pulse during a CT calculation. I think the ISR would be called to increment the pulse count variable (and whatever else it needs to do), but it would take a (very short?) time to run and return control to the CT or AC-AC sampling. While the ISR is running millis() doesn't increment, but I don't know if this is corrected at the next increment or if it causes millis() to lose time.

Maybe this isn't actually a problem at all, but if it is would it be better to poll the digital input and count the state changes rather than using an interrupt? The pulses, even with the meter running at full speed are fairly slow (about 2 secs per rev is the fastest I managed to get the meter going) so I think the chance of missing a transition are fairly low.

Robert Wall's picture

Re: Concurrent CT and Pulse inputs on EmonTX (v3)

I have not tested this, but provided your ISR does very little - as it ought, then, and especially as you say it will run only once every 2 sec at worst, I would not expect it to make a material difference. Realistically, it's adding a few microseconds drift to a measurement period of 200 ms.

dBC's picture

Re: Concurrent CT and Pulse inputs on EmonTX (v3)

The timer interrupt used to keep that time stuff working only fires every 16384 cycles.   If the timer interrupt fires during your ISR it will be left pending and will get executed once you exit your ISR.   So the only way you could lose a timer tick is if your ISR were to run for longer than 16384 cycles,  thereby allowing two timer ticks to occur while inside your ISR.   

Comment viewing options

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