Reading pulses from meters with pulse outputs


I want to count pulses on electricity meter and from that to estimate power consumption of a house.
I tried to use the arduino sketch Interrupt method given on the page Reading pulses from meters with pulse outputs of openenergy monitor website. But I did not understand the whole code.

//Number of pulses, used to measure energy.
long pulseCount = 0;   
//Used to measure power.
unsigned long pulseTime,lastTime;

//power and energy
double power, elapsedkWh;

//Number of pulses per wh - found or set on the meter.
int ppwh = 1; //1000 pulses/kwh = 1 pulse per wh

void setup()

// KWH interrupt attached to IRQ 1 = pin3
attachInterrupt(1, onPulse, FALLING);

void loop()


// The interrupt routine
void onPulse()

//used to measure time between pulses.
lastTime = pulseTime;
pulseTime = micros();


//Calculate power
power = (3600000000.0 / (pulseTime - lastTime))/ppwh;

//Find kwh elapsed
elapsedkWh = (1.0*pulseCount/(ppwh*1000)); //multiply by 1000 to convert pulses per wh to kwh

//Print the values.
Serial.print(" ");

Q1: what does that means, lastTime = pulseTime;
pulseTime = micros();?

Q2: why we calculate the power like this? power = (3600000000.0 / (pulseTime - lastTime))/ppwh; and why we used 3600000000.0 ?? and what is ppwh?

Q3: what we put number 4 here? Serial.print(power,4); and why we put number 3 here?Serial.println(elapsedkWh,3);

Finally, what i basically did is uploaded this code in my Arduino and i got this results in the attachments and i compare it with my smart meter readings. In addition, what i understand from the results that i got from the serial monitors is the first value is for the Power and the second values is for the Energy.

My smart meter values are:

Watts = 2014 W
Total KWH = 31.84 KWH

Best regards,

Q1. This stores the time of the last pulse, which was recorded as pulseTime, then resets pulseTime to the time of this pulse.
Q2. How many microseconds are there in an hour? Pulses represent energy, “ppwh” is pulses per Watt hour, and what it is doing there is applying the correct scale factor. So how does power relate to energy? (The clue is in the units - if your meter is 2 pulses per Wh, and you see 6 pulses per hour, what is the power? How many microseconds between pulses, and does the equation give you the same power?)
Q3. Look in the Arduino documentation for Serial.print( );

Have you set ppwh to be the correct number for your meter? If that is not correct (and you did not know what ppwh meant, so I suspect you have not set it), then you will not get the same result as your meter.

Thank you for your response,

Now i see things more clear than before, my meter is produces 800 pulses per KWH so what should i change in this code in order to get the correct values?ppwh only?

*ppwh should= 0.8?

Correct. 1622 / 2014 ≈ 0.8, so that confirms 0.8 is correct, alternatively that your counter is working correctly.

Sorry for a really stupid question, but what do I need to learn to implement this to my emonpi? Is it possible to make use of this data in the emoncms?

I took for granted that his already was implemented so I didnt bother with the amp-sensors (+ it would be a hassle to fit them there).

How to set up your emonPi?

I take it you mean pulses?
What is your emonPi receiving, it is a cumulative count from the beginning of time, or is it the number of pulses from the last batch of data? How you handle and process the pulse count depends quite a lot on what’s coming in, because the two possibilities I mention above mean quite different quantities are being measured, which require quite different processing. You’ll find a lot in these forums about pulse counting, and some of the pitfalls.

Thanks for replying! I think I got lost pretty much directly there :wink: And today I noticed that the emonPi seems like it is not able to handle the amount of pulses that my meter is blinking in (10.000 pulses per kWh) I started a new thread about that:

I’ll come back here when that gets solved (or not solved :wink:

I’ve deleted your new thread because it is silly to have the same question in two places.

Do you have the optical pulse sensor from the OEM Shop? That same question was asked a short time ago. The solution is, in the sketch that runs on the Atmel 328P inside the emonPi, you need to reduce or remove the timer that is there to remove contact bounce in mechanical reed switches. It is not necessary because those optical switches do not have contacts, therefore no contact bounce.

It is in the file emonPi_RFM69CW_RF12Demo_DiscreteSampling.ino
and it is line 86:
const byte min_pulsewidth= 110; // minimum width of interrupt pulse (default pulse output meters = 100ms)

You can make that min_pulsewidth= 1;

You will then need to compile and reload the sketch, I believe instructions are in the Wiki.

1 Like

Wow, great! Thanks a lot and sorry about the double post. I’ll check the wiki! (Edit: Yes its the pulse detector from the shop)

If setting the minimum pulse width to 1 (ms) does not work (what is the maximum pulse rate that your meter will reach?), you can try removing that time limit completely. The interrupt routine is in the file emonPi_Interrupt_Pulse.ino

The file will need to look like this:

void onPulse()
    pulseCount++; //calculate wh elapsed from time between pulses

1 Like

Got it working with the editing, compiling and uploading. Finally :wink: Tried it succesfully for 30 Hz and it will be enough for 11kw or something.

So, back to the sketch above. Would it be possible to make a feed of this calculated energy (measuring time between pulses that is) in emoncms?

1 Like

I’m not an emoncms expert, but I think the tools exist. Hopefully someone who knows emoncms will be able to help you.

1 Like

Seems like one can use the “rate of change” process on the pulseCount feed. There should have been an argument to set number of decimals. I just wanted to get back to this, since its a very fast way to get the current power usage only from the pulse detector. In sweden we have 3-phase, so I cant use the emonpi to measure current power consumption in any other way.

I was reasonably sure that it was possible, but I did not know the details.

Yes, it is a shame that the emonPi could not be made capable of 3-phase, 4-wire use, but there was space only for two jack sockets in the box, and to use any other connector would have introduced major compatibility problems.