Multiple pulse counting with Arduino Mega?

Hey

Has anyone tried using an Arduino Mega to count multiple pulses? The Mega seems to have 6 inputs with interrupt capability.

I read on http://openenergymonitor.org/emon/buildingblocks/12-input-pulse-counting that Trystan had problems using the Ethernet shield because it blocked for up to 1700 ms, but would that be a problem when using interrupt based pulse detection?

Since running pulse counting with exact micro second timing makes it impossible to use sleep modes for the CPU, it's not possible to run off batteries anyway, so using a full blown Arduino doesn't matter I think.

o_cee's picture

Re: Multiple pulse counting with Arduino Mega?

No one who has any input?

Since we get three phases here in Sweden, and I'd like to monitor three different (to start with) fuse boxes (sub centrals?), I think it would be easier and cheaper to go with three DIN mounted three phase meters, and one Arduino. I would prefer to use the RFM12b to communicate with the Raspberry base, but it seems easier to get a WLAN shield.

MartinR's picture

Re: Multiple pulse counting with Arduino Mega?

You could just use an emonTx for that. Any pin can be used as an interrupt using interrupt-on-change. I'm currently using digital pin 7 for pulse counting. You just need to write the code to see which pin caused the interrupt.

o_cee's picture

Re: Multiple pulse counting with Arduino Mega?

Uhm yes but there's only one interrupt, I need three. The cost of three emontx is more than one Arduino Mega.

MartinR's picture

Re: Multiple pulse counting with Arduino Mega?

That was my point. I said any pin can be used as interrupt. You should read the section on pin change interrupts in the ATmega328 data sheet. The pins are in 2 groups of 8 and 1 group of 7 with 3 separate interrupts so if you pick your pins carefully you could use the 3 interrupts or you could just use one interrupt and just read the pins in the interrupt handler to see which one changed.

o_cee's picture

Re: Multiple pulse counting with Arduino Mega?

Ah thanks Martin, I didn't know about pin change interrupts. You are right, that could work, just need to figure out how much re-work the emontx would need...

Quite nice article about interrupts:
http://www.gammon.com.au/forum/?id=11488

MartinR's picture

Re: Multiple pulse counting with Arduino Mega?

Yes, that's a useful article - thanks.

If it's any help, this is what I did to use digital pin 7....

In setup replace

attachInterrupt(1, onPulse, FALLING);

with

bitSet(PCMSK2,7); // unmask pin change interrupt for selected pin
bitSet(PCICR,PCIE2); // enable pin change interrupt

then in the interrupt routine replace

void onPulse()                 
{

with

ISR(PCINT2_vect)
{
  if(digitalRead(7)==HIGH) return; // we only want the falling edge

and that's it!

o_cee's picture

Re: Multiple pulse counting with Arduino Mega?

Nice, thanks! And where did you connect it physically? DIO7 to the right of the crystal?

MartinR's picture

Re: Multiple pulse counting with Arduino Mega?

Yes, that's right. I wired the detector to a 6-pin plug which can go on any of the headers P1-P4. Only P1, DIO4 is actually used by emonTx and that's only if you have a temperature sensor.

o_cee's picture

Re: Multiple pulse counting with Arduino Mega?

I think they are wired to the CT inputs, no? (Which I won't be using). Would I need to remove the CT components, or they won't interfere? 

MartinR's picture

Re: Multiple pulse counting with Arduino Mega?

No, each 6-pin header has an analogue and a digital pin. The CTs are connected to the analogue inputs, the digital inputs aren't connected to anything (apart from the temperature sensor). You can pick up 3.3V power and ground from the adjacent pins. I'm using the CTs and the pulse detector at he same time.

Comment viewing options

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