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 can't use the interrupt method to measure 12 pulse inputs as there are only 2 interrupts on the Arduino. Instead, we need to sample very fast,ff and as continuously as possible in order to catch every pulse and measure the pulse rate accurately. To get best performance we need to:

  1. Read the digital input pins using fast low level digital input register commands
  2. Dedicate the Aduino to pulse counting. e.g. no Ethernet data sending.
  3. Streamline the code as much as possible - e.g. no floating point math and minimal serial printing.

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 quickly became apparent the more inputs I read, the worse the accuracy became. The accuracy deteriorated quite rapidly! 

Luckily, we can read digital Inputs much faster using direct port manipulation. It's possible to read a register of 8 digital inputs in one command, although it actually drops to 6 inputs per register due to the 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. By calling commands PIND and PINB which return a binary value of the digital pin states, it's possible to read 12 useful digital inputs virtually simultaneously. Although it's much faster than using the digital read command, it's not as easy to understand when you look at the code!

With low level digital register access, accuracy stopped dropping when reading from more than 2 or 3 inputs.

Dedicated Pulse Counting Arduino

The Arduino used for pulse counting needs to be dedicated to the task. As I mentioned above, I used the same arduino for Ethernet data sending to start with. The problem is Ethernet data sending can take from 26 to 1700+ ms, and any pulses occurring during this time, are lost. For accurate results, keep the pulse counting Arduino dedicated.

This means that a 12 input pulse counting Arduino needs to be seen as an external input module. We need a second Arduino for doing other tasks like Ethernet data sending, etc.

I decided to opt for a configuration where the pulse counting Arduino displays its pulse times in microseconds and pulse counts of all 12 inputs via 115200 bps serial, once per second. This string is sent to, and decoded by, the main Arduino, then sent via Ethernet, to a web-conected energy display:


The hardware for a pulse counter is mostly just the Arduino itself, 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 its 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 output is logic level 0 when the pulse is low. However, if you build a pulse counting module with pull down resistors of ~10k it still works with the light sensor, more info to come on this.


Dedicated pulse counting module sketch

Measures pulses from up to 12 pulse output meters, sends a string to the serial port at a specified time interval (1s default) with pulse times and pulse counts. The string is in semi-JSON format to keep it lightweight. 


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. 


Pulse Decoder + Ethernet

The above sketch, plus sending these values via a JSON string to the web using the official Ethernet shield.


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 all 12 inputs for proper operation. Glyn Hudson has written documentation and a sketch to do that: Bench testing with a 12 output pulse generator

More useful pages:


Accuracy and Precision

Revision log and further development questions

Richard Owen's picture

Re: 12 input pulse counting


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?




glyn.hudson's picture

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:

Good luck. 

Richard Owen's picture

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.

Larsjo's picture

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




glyn.hudson's picture

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:

getPower(digital inputt pin, kwh per pulse)

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:

All the best, 


Larsjo's picture

Re: 12 input pulse counting


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.


stephane's picture

Re: 12 input pulse counting


That's great you share your sketches under GNU GPL but you don't specify which version and the copyright mission statement is missing:

My goal isn't to annoy you just make sure the project starts on good basis!



TrystanLea's picture

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 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


stephane's picture

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 ( 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.

TrystanLea's picture

Re: 12 input pulse counting

Well I better get down to it :) Thanks Stephane