Pulse Counting with Photon and DIN Energy Meter

Hi folks,

my name is Samir Leal, and I am from Brazil. I am trying to make an Energy Monitor for my solar system with a chinese DIN energy meter (DDS238-1 - 3200p/kWh), the Particle Photon and the emoncms.org, but I am having a really bad time with the results.

Below is the code, I kept it as simple as possible to be able to test my setup:

volatile bool flagState = false;
volatile int Meter_1 = 0;

unsigned long timer = 0;


void setup() {
    pinMode(D7, OUTPUT);
  
    pinMode(D1,INPUT_PULLDOWN);
    attachInterrupt(D1,blinkLED,FALLING);
}

void loop() {
    
    if (((millis()-timer)>60000) || (millis() < timer)) {
        timer = millis();
        
        Particle.publish("Pulses per 60s",String(Meter_1).c_str(), PRIVATE);
        
        Meter_1 = 0;
    }
        
        
    if (flagState == true){
        digitalWrite(D7, HIGH);
        delay(30);
        digitalWrite(D7, LOW);
        
        flagState = false;
    }

}

void blinkLED() {
  Meter_1++;
  flagState = true;
}

And I am connecting like this:

[21] --------- Orange Cable ---------- [3V3]
[22] --------- Red Cable --------------- [D1]

as the result, I am getting around 6.000 pulses per 60s (the D7 LED blinks all the time).

When I put a 0,5k resistor between GND and D1 the pulses are around 1.500 per 60s (the D7 LED blinks only when the energy meter’s LED blinks).
When I put a 10k resistor between GND and D1 the pulses are around 1.500 per 60s (the D7 LED blinks all the time).

I am with a load of 100W, and I was expecting to count 5 or 6 pulses per 60s.

1kWh - 3200p ----> 100W = 320p/h —> 5,33p/min or 1p/11,25s

Any help would be great!

Thanks!
Samir Leal

You may need to debounce the pulses. I have a water flow meter that works similarlly and it needed to be debounced.

void handleFlow() {
  if(millis() - flowCountLastTimestamp > 50) {
    flowCountLastTimestamp= millis();
    flowCount++;
  }
}

Also, remove (millis() < timer) from your if statement.

@brandongoode Thanks a lot! I think this solved my problem.

I will keep doing some tests, but for now, I consider the problem solved!