Issue with pulse counter component!

Hi

I try to use that tutorial to count my electric consumption at home: https://esphomelib.com/esphomeyaml/cookbook/power_meter.html#see-also but I get only crazy values reported in HA even some negative ones !
I use a nodemcuv3 board with a basic photoresistor like that for wiring (mine is wired on D1 instead of A1)


Right now sensor shows up in HA but keeps staying at 0 !
I’m confused as photoresistor is analog device and so should be wired on Analog input I guess but example linked on esphomelib website uses a digital input !!
Someone succeeded to get that working ?
Update: tried using A0 as input but still stuck at zero in counter :frowning:

Thanks

Vincèn

Hey Vincèn
the same here. Its sounds quite easy. It must be easy, because i found nothing else about errors :slight_smile: I us a WeMos D1 mini. Hooked up the sensor on D6 (GPIO12) as mention in the description. Wondering why not A0, tried this as well, same result.
Use a 10KOhm-Resistor and a GL5528-Photoresistor (10-20KOhm Light resistance), but don’t know the frequency of the LED on my built-in Powermeter. Probably i need a other LDR? Have GL5506, GL5516, GL5528, GL5537 and GL 5539. Or have i to try every LDR?

Regards
Daniel

Hey Daniel

i didn’t follow-up on this as I moved in a new place and I don’t have anymore any counter with such system ! My initial guess was that LDR are too slow to react to detect a pulse flash like that !

Vincèn

What colour is the LED on your meter? This article shows the wavelength of typical colours https://en.wikipedia.org/wiki/Color

Also this has a table which shows that most (if not all) of your LDRs peak at 540nm, which is green. https://electronics.stackexchange.com/questions/331985/light-dependent-resistor-photoresistor-ldr-gl5516-vs-gl5506?rq=1

Hope those help

1 Like

From the datasheet the response time is about 30ms worst case so it should be able to react fast enough but it still won’t work the way you have it. See this diagram?

Look at the 3.3V TTL logic level “in low” and “in high” voltages: VIH = 2V , VIL = 0.8V You need to be above and below these voltages respectively for a change in logic level to be registered.

Using the voltage divider equation to calculate your dark and light voltages assuming a 3.3V supply, 10K series resistor and LDR resistance of (from the datasheet) 15K light, 1M dark:

Light: 1.98V (indeterminate logic level)
Dark: 3.26V (logic 1)

So this wont work.

Use a 100K series resistor with the GL5528. It should work.

Vo Light (20K Ohm, worst case): 0.55V = Logic 0
Vo Dark (1M Ohm): 3.0V = Logic 1

Make sure you hook it up with your LDR in the R2 position as shown on that voltage divider calculator page.

You might still have an issue if the intensity of the light is not bright enough to bring the LDR down to at least 32K Ohms. Or if the spectral response is poor as pointed out by nickrout and again you can’t get the light on resistance this low.

2 Likes

You explain in one post how low/high voltage logic works, how LDR’s work, how to calculate the output voltage with voltage divides equation and thus how to choose the right resistances. This post is so valuable… I don’t understand why these guys didn’t thank you…

At least I will: Thank you!

2 Likes

Hi Tom! I now have a question:
I connected everything with an esp32 and a GL5516. Everything is working and the pulses are counted by the esp. The problem is that it is counting to many pulses I think. Comparing the energy consumption of the energy meter on the wall with my “esp-energy meter” its about the double consumption.
Is there a way to handle this? Or other question: What is causing this?
Sorry, I know you maybe need more information, but I don’t know which one :D…
Here is my Esprit config:

esphome:
  name: stromzaehler
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: "ssid"
  password: "pw"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Stromzaehler Fallback Hotspot"
    password: "quPTOCJWWpLm"

captive_portal:

# Enable logging
logger:
  level: DEBUG
# Enable Home Assistant API
api:

ota:

#esp32_ble_tracker:

sensor:
  - platform: pulse_counter
    pin: GPIO36
    update_interval: 10s
    unit_of_measurement: 'W'
    name: 'Power Meter'
    filters:
      - multiply: 120
    internal_filter: 13us
    id: powermeter
  - platform: total_daily_energy
    name: "Gesamter Stromverbrauch"
    filters:
      - multiply: 0.001 
    power_id: powermeter
    unit_of_measurement: kWh
time:
  - platform: homeassistant

What does one pulse from your meter mean (how many Watts)?

It might say on the meter or you may have to look up the meter data sheet.

500imp/1000Wh … I think the calculation of the multiplier is right.
Its weird that I see often a “jump” in the counter. For example when I have 3 pulses/min sometimes suddenly it says 6 pulses/min (just for one counting phase) and than continues with the correct 3pulses/min. It looks like these jumps occur more often when there a more pulses.

I’m really thinking about how precise this method of measurement can be. I think you have a lot of know-how in this field, since I saw many posts of you about energy-monitoring. Do you think that wiring up 3 ct clamps (got 3-phase ac in my house) is better than counting pulses? What would you recommend me?

Did you see the note about noise at the bottom of this page:

I find it weird that the cookbook example converts energy to power then back to energy again.

1 pulse = 2Wh of energy in your case, just counting the number of pulses (keeping a tally) and dividing by 2 gives you a total energy used meter. Rather than using the pulse counter sensor that outputs in pulses per min scaling it to get power then feeding that to the energy meter sensor.

Yes, I’ve seen that section with the noise , but didn’t get any different or positive result playing with it. The maximum filter for ESP32 is also just 13us.

I think the „recalculation“ is because people want to know the actual power consumption. Knowing just the consumed energy isn’t telling people what the actual consumption is.

Do you have experience with ct clamps?

Only with the Shelly EM.

Okay :slight_smile:

There are a couple of topics in the ESPHome category about CT clamps. People seem to have accuracy issues at the lower power end.

The Shelly EM is simple to interface with home assistant using mqtt. I’ve found it to work really well for power and energy monitoring. Not too expensive either.

Agreed with you @tom_l.
How to set up a pulses counter which will not count the number of pulses per min but simply increment a number?
My power meter does 1 flash for 1 Wh consumed.

And of course, thank you for your explanation here, it is pretty clear.
However, as we want to count the flashes, shouldn’t we put the LDR in the R1 position? I mean, don’t we want to have a logic 1 when there is light?
By the way, the diagram you posted, is it specific for the GL5528 or is it for any LDR?

Hi all,
It works fine for me now.
I placed the LDR GL5528 in R1 position and I used a 10kOhm resistor in R2.
Here is the code I uploaded on a Wemos D1:

# pulse counter for the house main electricity meter. One pulse = one Wh consumed
  - platform: pulse_counter
    pin: GPIO12
    update_interval: 1s
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    internal_filter: 29ms
    icon: "mdi:flash"
    unit_of_measurement: 'kW'
    name: 'Power Meter'
    filters:
      - multiply: 0.06
    id: power_meter
    accuracy_decimals: 0
      
  - platform: integration
    name: "Power consumption"
    sensor: power_meter
    id: power_consumption
    time_unit: h
    icon: "mdi:counter"
    unit_of_measurement: "kWh"
1 Like

The diagram is showing the high/low logic in general.
It doesn’t matter if its logic is 1 or 0. Pulses are counted if you jump between these phases.

How reliable is it working for you? I had differences between the real energy consumption and the counted consumption.

Which sounds like edge triggering. Which is why it is important to only trigger on one edge of the pulse (or you will get double the reading):


    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT

I tested for an hour and the precision is 100%. I will try for few weeks now. I think the accuracy_decimals is also important.