Help with 2 pulse_counter sensors

I am trying to mount a small project, in theory very simple, but something is wrong and I am getting crazy. I hope you can help me to understand what is wrong.

I have an ESP8266 and 2 flow sensors.

The flow sensors are like this (ignore the picture in the scheme)
flow sensor
:

The connection scheme is very simple, like this:

And my the definition of the sensors is like this:

sensor:
  - platform: pulse_counter
    pin: GPIO5
    name: "Flujo agua (1)"
    update_interval: 10s # 60s is default value
    unit_of_measurement: 'L/min'
    id: flujo_agua_1
    filters:
      - lambda: return (x / 486.0);   # Pulso de flujo: F (Hz) = 8,1 * Q – 3 (+/- 10%) Q = L / min  ->  # 486 = 8,1 * 60

  - platform: pulse_counter
    pin: GPIO4
    name: "Flujo agua (2)"
    update_interval: 10s # 60s is default value
    unit_of_measurement: 'L/min'
    id: flujo_agua_2
    filters:
      - lambda: return (x / 486.0);   # Pulso de flujo: F (Hz) = 8,1 * Q – 3 (+/- 10%) Q = L / min  ->  # 486 = 8,1 * 60
  • If I disconnect GPIO4 wire: I can read the flow in “Flujo agua (1)” (comming from GPIO5). The other sensor reads 0.
  • If I disconnect GPIO5 wire: I can read the flow in “Flujo agua (2)” (comming from GPIO4). The other sensor reads 0.
  • BUT if I connect both pins at the same time. When water is flowing for 1 sensor, I get the same value in both of them. If for example I get water flowing throw sensor in GPIO5, I get the value read in “Flujo agua (1)” but also in “Flujo agua (2)”…Same result the other way.

What am I missing?

I am not sure, but could it be that the “x” variable is global in the code, so is always the same for both sensors?
What happens when you temporarily take out the filters: are the returned values still the same for both?

It should be local to each sensor.

Some things to try:

Separate the grounds (You have 4 GND pins)?
Try another power supply?
Try using different GPIO pins?

Thanks Tom,
It seemed to be a logical explanation for this problem. Are you talking from own experience?
I have been searching the documentation and the code on GitHub for this, but have not been successful.
Do you know where this is specified?

Have you soldered to the NodeMCU ?

Yep. But come to think of it, with different components. Not two instances of the same component. I smell a bug…

Perhaps a pull-up or pull-down resistance is needed. If the wire runs are bundled, there could be signal crosstalk.

https://esphome.io/components/sensor/pulse_counter.html

Refer to the “wiring” section.

1 Like

To test it I modified the code of one of my ESP devices like this:

# DS18B20 Temperatuur sensors
  - platform: dallas
    dallas_id: dallas_hub_1
    address: 0x2206201221529C28
    name: $friendly_name Warmtepomp Uitgaande lucht Temperatuur
    id: d1v40_01_ds18b20_wit
    filters:
      # - offset: 0.3
      - lambda: return x + 0.3;

  - platform: dallas
    dallas_id: dallas_hub_1
    address: 0x9F00000D31EF4928
    name: $friendly_name Warmtepomp Sensor behuizing Temperatuur
    id: d1v40_01_ds18b20_to92
    filters:
      # - offset: 0.3
      - lambda: return x + 0.3;

This are two identical DS18B20 sensors, but in a different environment.
If the “x” variable would not be local to each sensor the values should be the same now, and they are not.
So (at least with DS18B20 dallas sensors) it looks like the “x” variable indeed is local to each sensor.
Could it still be that this is a specific issue with two pulse counters?

X is local.

Try enabling pullup or pulldown on the pins, see if that helps. Look up “floating pins” on YT to understand why.

I have been doing some tests with your suggestions.

  • Separate the grounds (You have 4 GND pins)? → No, I don’t
  • Try another power supply? → Same result
  • Have you soldered to the NodeMCU ? → No
  • pull-up or pull-down resistance → pull-up:no changes, pull-down: compile error, only available for GPIO16
  • What happens when you temporarily take out the filters: same “bug”
  • Try using different GPIO pins? → This was my first attempt, changing one of the pins (changing GPIO4 by another one), with no result, but now I have tried again, changing both of them and it looks like it’s working fine now.

I did a couple of changes:

  • Change board definition setting “nodemcuv2” (I had esp01_1m)
  • Change GPIO4 & GPIO5 with D3 & D4

I guess that the issue is happening when I use the GPIO5 (D1) pin.

Anyway, it looks like now is working fine so, thank you very much.

1 Like

Well done :+1:
Just for reference: which board are you using exactly: a NodeMCU or an ESP01 or some other board?

I am using a NodeMCU board

I just wanted to document that I had this same experience:

  • Two pulse_sensors would return identical values for both sensors regardless of which input IF particular pairs of GPIOs were used.
  • When I used GPIO 22 & 23, I saw this behavior.
  • When I switched to GPIO 22 & 27, the behavior went away and each sensor returned correct values.
  • Changing pullup/pulldown values did not affect the behavior.
  • Both sensors same a common power and ground bus.

This was on a LILYGO T-RELAY-8 board (ESP32)