ESPHome pulse_counter doesn't work

I’m trying to create a pulse counter sensor that monitor the GAS meter (that have a pulse output).
I tried many times and many different configurations, but I didn’t manage to make the “pulse_counter” works. And I spend many hours on it.
BUT if I use a binary_sensor and a global variable to take note of every state change, it works.

Example of non-working code:

sensor:
  - platform: pulse_counter
    pin:
      number: GPIO14
      mode: INPUT_PULLDOWN
    count_mode:
      rising_edge: INCREMENT
      falling_edge: INCREMENT
    name: "Pulse Counter"
    update_interval: 15s
    total: 
      name: "Total pulses"

This code above does not count a single pulse.

Instead, this one works:

globals:
  - id: motion_count
    type: int
    restore_value: yes
    initial_value: '0'
    
binary_sensor:
  - platform: gpio
    name: "Gas meter pulses"
    id: gas_sensor_floater
    pin:
      number: GPIO32
      mode: INPUT_PULLDOWN
    on_press:
      - lambda: |-
            id(motion_count) += 1;
            ESP_LOGD("Counter", "Impulsi: %d", id(motion_count));

sensor:
  - platform: template
    name: "Total counter Sensor"
    lambda: |-
      return id(motion_count);
    update_interval: 60s

That makes no sense to me. The only thing that came to my mind as possible reason of this behaviour is that the pulse’s uphill and downhill curve isn’t steep enough.

Anyone have suggestions?

Thank you

Gpio14 is not “clean” pin, try your counter code with 32.
Also, remove the falling edge from count.

1 Like

Yup, don’t count on both a rising and falling edge.

Also have a look at this: Pulse_counter vs pulse_meter.

Thank you for you answer, unfortunately I already tried different pins, also different modules (1 NodeMCU dev and 1 another ESP32-WROOM-32)… With no luck :frowning:

I added the falling edge for test, hoping that maybe it could be better than rising. But seems that it can’t detect rising nor falling edges

Thank you too, but at the moment it doesn’t count the rising nor the falling, so… :frowning:

I noticed the pulse_meter, but in my case I need to have a count of the pulses, not a correlation with time. And I don’t need to have it in real time, it’s not a problem to have a little delay. So the pulse_counter is what I need, I think

The fact that binary sensor detects the pulse, means that the pulse is very long.
Binary sensor is just polling on the loop every ~16ms.
What kind of circuit you have between Esp and meter?

Also, how are you detecting the pulse counter? You don’t get anything on your esphome log?

This one is the meter with his pulse sensor. I just plugged the wires to ESP32 (Ground to “G”, +3.3V to “A”, GPIO32 to one of the “I”). The meter has a magnet on the “0” of the least significant wheel, that trigger the reed switch when passing near the “black box”.

Are you sure?
To me it’s a miracle if you didn’t burn the sensor and Esp completely with that wiring.
A is bulgar alarm output and it’s connected to G. You see it on connection scheme printed on top of sensor.

Correct wiring is G to gnd and I to esp gpio. mode: INPUT_PULLUP

I will try this wiring again, but I already tried it (the first thing I tried) with no luck. But maybe I was trying with “pulse_counter” sensor and not with the “binary_sensor” workaround (really, I don’t remember, due to the countless tests done).

Maybe they have fuse inside, I don’t know how it’s possible that magic smoke didn’t show up.
Use one edge only.
Also, the pulses are super long, can be that pulse counter is not good for that. Try both pulse_counter and -meter.

I’ll try this evening as soon as I get home. I just noticed that if the last wheel stops with magnet near pulse (reed) sensor, it keep it “ON” until it moves away. It’s like a door contact sensor. So it can remains “ON” for hours or days, if it stops in that position.
Will the pulse_meter work with this kind of behavior? :thinking:

I don’t know, but for sure it’s not designed for that. Afaik pulse_counter uses RMT hardware peripheral which was designed for IR-remote pulses, usually few microseconds long.

I confirm that wiring G to GND and I with GPIO (set as INPUT_PULLUP) does not works both for pulse_counter that binary_sensor. The wiring I used before (giving the VCC to A) worsk in some manner. I could try to give the 3.3V to G and set the INPUT_PULLDOWN to the GPIO (without connect the A wire)

Sure it doesn’t. It would be always LOW.

Sorry, it was a typo… I set the INPUT_PULLUP :sweat_smile:

That’s incredible, but pulse_meter worked… pulse_counter does not read the pulses that pulse_meter can… I don’t knwo why, I’ll use the pulse_meter, keeping only the “total” sensor into consideration

Never tested with so long pulses, according to that scheme I posted above, your pulses can be seconds in length.
Bigger mystery is why you didn’t fry your board… :sweat_smile: