Build a 12 input pulse counter - multiple input pulse counting
Last Update 3 November - by Trystan Lea

Pulse counting shield: includes a dedicated barebones arduino and pull down resistors (detailed below)
How it works
We cant use the interrupt method to measure 12 pulse inputs as there are only 2 interrupts on the Arduino. Instead we need to sample really fast and as continuously as possible in order to catch every pulse and measure the pulse rate accurately. To get best performance we need to:
- Read the digital input pins using fast low level digital input register commands
- Dedicate the pulse counting arduino to pulse counting only - no ethernet sending of data for example..
- Stream-line the code as much as possible - for example: no floating point math and minimal serial prints
Low level digital input registers
In version one I used digitalRead to read the state of the digital inputs and I also used the pulse counting arduino to do other things like sending data via ethernet. I started to do some accuracy tests, it became quickly apparent that the more inputs I read the worse the measurement accuracy got and the accuracy deteriorated quite rapidly!
Luckily it is possible to read digital Inputs much faster using direct port manipulation, its possible to read a register of 8 digital inputs in one command, although it actually drops to 6 inputs per register due to use of 2 for serial communication ( 0 and 1) and for the clock crystal (14 and 15).
Digital pins 2 to 13 occupy 2 registers, register D and B and by calling commands PIND and PINB which return a binary value of the digital pin states its possible to read 12 useful digital inputs pretty much simultaneously and much faster than using digital read although not as easy to understand when you look at the code!
With low level digital register access accuracy stopped dropping when reading from more inputs pins, result!
Dedicated Pulse Counting Arduino
The arduino used for pulse counting needs to be dedicated for pulse counting. As I mentioned above I used the same arduino for ethernet data sending to start with, problem is that ethernet data sending can take between 26ms and 1700ms+ and any pulses that occur in this time are lost, leading to really poor results. So for accurate results its best to keep the pulse counting arduino dedicated.
This means that a 12 input pulse counting arduino needs to be seen as an external input module, where we need a second arduino for doing the other tasks like ethernet data sending etc.
I decided to go for a configuration where the pulse counter arduino spits out its pulse times in microseconds and pulse counts of all 12 inputs via fast 115200 baud serial once every second. This string is then recieved and decoded by the main arduino and then send via ethernet to a web energy display:

Hardware
The hardware for a pulse counter is mostly just the Arduino itself and then depending on your configuration:
Wired pulse counting
A pull down 10k resistor on each digital input to bring the digital signal to zero when the pulse output relay is in off/disconnected state.

LED Pulse counting
Create LED pulse sensors following this guide by AirSensor
No pull down resistor is required as the pulse / light sensor gives logic level 0 when the pulse is low. However if you build a pulse counting module with pull down resistors of around 10k up it still works with the light sensor, more info to come on this.
Software
Dedicated pulse counting module sketch
Measures pulses from up to 12 pulse output meters, spits out a string to serial every given time interval (1s default) with pulse times and pulse counts. String is in semi-JSON form to keep it lightweight.
Download: MultipleInputPulseCounter_24Oct.zip
Base Arduino sketch
The following sketch decodes the semi-JSON string sent from the dedicated pulse counting arduino and calculates power and energy values from the pulse times and pulse counts.
Download: PulseDecoder.zip
Pulse Decoder + Ethernet
The above sketch, plus sending these values via a JSON string to the web using the official ethernet shield.
Download: PulseDecoderEthernet.zip
To read more about the web side of things have a look here: Energy Monitor + Simple Web Based graphing
Bench Testing - Pulse Generator
With the pulse counter build you may want to test that all 12 inputs are working correctly, Glyn Hudson has written a good sketch and documentation to do this here: Bench testing with a 12 output pulse generator
Re: 12 input pulse counting
Hi,
I'm just about to dive into the Arduino waters and build a system to measure the house consumption and PV output. Both meters and led pulses.
As I've only got two inputs do I need to go down the route above or would one Arduino work with the inputs coming into the two interupts?
Or, am I asking a stupid question?
Thanks,
Richard
Re: 12 input pulse counting
Hi Ritchard,
It is possible to monitor the output of a utility meter by reading it's pulse output but especially when monitoring solar PV this results in a very poor instantenous powe reading since pulses come so infrequently. A much better way is to use clip-on CT current sensors see: http://openenergymonitor.org/emon/emontx/solarpv
Good luck.
Re: 12 input pulse counting
Hi Glyn,
Thanks for the reply. I'll have a go at the CT version and see how I get on.
Re: 12 input pulse counting
Done the hardware part of this setup.
But where do you set the numbers of puls pr. kw ? - is it done in emoncms
One of my S0 puls is 800 pr. kw and the other is 1000 pr. kw.
My plan is to use this for my PV, gas and water.
Lars Joergensen
Denmark
Re: 12 input pulse counting
Hi Larsjo,
Good work getting the hardware done. The ammount of pulses per kWh is set as the ammount of energy one pulse equates to. In our example 1 pulse =0.001kWh (i.e 1 Wh/pulse or 1000 pulses per kWh).
The is set in the Arduino Sketch software with the variables passed to the function getPower:
It's easy to assign different kwh/pulse values to different meter readings.
Good luck, please document your experiences on the site. We would love to hear what your using the system for. If you runinto further problems there are many people on the forums who will be happy to help.
If your monitoring PV you might find that CT based monitoring gives better results as since the power is generally quite low the time between pulses can be very long. See my documention on building a solar PV monitor using CT sensors: http://openenergymonitor.org/emon/emontx/solarpv
All the best,
Re: 12 input pulse counting
Thx
I can see the problem in using S0 outputs, everyting under 100w is a no go because of the time between the pulses.
Will look into you PV setup, can see that is maybe a better way to do it.
Re: 12 input pulse counting
Tristan,
That's great you share your sketches under GNU GPL but you don't specify which version and the copyright mission statement is missing:
http://www.gnu.org/licenses/gpl-howto.html
My goal isn't to annoy you just make sure the project starts on good basis!
Bye,
Stéphane
libmodbus.org
Re: 12 input pulse counting
Hello Stephane
I appreciate your question. Im afraid I have become a little complacent about inserting it in the source, although I certainly am fully commited to highest degree of freedom and openness so I would appreciate any advice you can give on making sure everything is on a good basis.
I created a license statement page when I started the project here http://openenergymonitor.org/emon/node/4 I could add a link to this on the footer of the website as it is a bit hidden away in the about page.
On that page I say OpenEnergyMonitor is free; you can redistribute it and/or modify it under the terms of the GNU General Public Licence as published by the Free Software Foundation; either Version 2 of the Licence, or (at your option) any later version.
Would you advise changing that to version 3? and how critical is it to insert a statement in every piece of code?
Thanks a lot
Trystan
Re: 12 input pulse counting
It's critical to insert the statement in every piece of code.
I choose the LGPLv3 for libmodbus to avoid tivoization (en.wikipedia.org/wiki/Tivoization). This risk is more important in embedded software so I think it's better choice for your project (GPLv3 in your case) but I'm not a lawyer and I'm recommend you to read a bit more on the subject before picking one.
Re: 12 input pulse counting
Well I better get down to it :) Thanks Stephane