What is the maximum reading of the pulse width sesnor?

I’ve got an ESP32 running the pulse width sensor on a light detector module attached to the blinking light on my electricity meter. I’m using it to detect the time between pulses so I can accurately calculate my power consumption.

However when the pulse become very spaced out (low power consumption), the ESP reports a width of zero. I’m assuming it does this because it reaches its maximum value and resets before a new pulse is detected. So my question is: what’s the maximum value it can report? And can I increase it some way, by say by changing the resolution from microseconds to milliseconds etc?

Thanks

According to the source it is contained in an unsigned 32 bit integer. So 4294967295 usec max. This should allow for 1 hour 11 minutes and 35 seconds.

However I don’t profess to understand the code and there may be some sort of time-out before that.

Hi Dave,
Mind sharing details on the sensor you use and how you set it up? I hadn’t thought of it, but its a great idea that I might want to explore myself.
Thanks!

I used one of these light sensor modules https://www.ebay.co.uk/itm/2PCS-LM393-Light-Sensor-Module-3-3-5V-Input-Sensor-for-Arduino-Raspberry-pi-UK/254229791627?hash=item3b3146c38b:g:Hr4AAOSwu6xc2Swy Note that it’s one that uses an LED to sense the light and not an LDR. LDR’s can be slow to react so they might miss some quick pulses.

The digital output from that module goes to an input pin of the ESP32, and then I use the pulse width sensor on ESPHome.

  - platform: pulse_width
    pin: 
      number: GPIO23
      inverted: False
    name: Pulse Width Sensor yard
    update_interval: 5s
    filters:
      - lambda: return 3600 / x;
    unit_of_measurement: "W"

The Pulse Width reports the time in seconds, and my meter flashes 1000 times per kWh (once per watt hour). There are 3600 seconds in one hour. So if you take 3600 and then divide it by the number of seconds, it tells you the average amount of watts that were flowing through the meter between those two pulses.

However, until I find out how to increase the maximum value of pulse width, when consumption drops below a certain amount, the sensor reads zero…and 3600 / 0 = infinite…so my Home Assistant dashboard is currently displaying a consumption of 470 megawatts…

1 Like

Hmmm, it appears the readings go mental once it takes about 10 minutes between pulses, so I’m guessing it’s less than 1 hour and 11 minutes.

How about doing an if else statement. If x = 0, return 0, else return 3600 / x.

That will eliminate the crazy readings, but I was actually after a solution to count the longer gaps between pulse widths, and this measure power consumption when very low.

I’m not understanding the issue. If you have 0 pulses over your interval, usage is 0 and should be reported as 0. The lower the interval, the more detailed a graph you can make for when you do have a lot of usage being reported.

I monitor pulses from my power meter in the same manner (optical sensor over emitter on meter) but I use a Raspberry Pi and a python script I created. I measure pulses over 10 seconds, convert to watts (watts = curr_pulsecount * 360) then send the value to my mqtt broker. I then have an mqtt sensor in home assistant that gets updated with this value.

If I have 0 pulses over 10 seconds, total watts used is 0 and 0 gets reported to the mqtt broker. it’s entirely possible that watts could get reported as 0 over several intervals and that’s perfectly valid.

Why not use the pulse_counter sensor instead? I’m doing the same as you, with the sensor magnetically attached to the meter watching the LED which flashes once for every Wh consumed.

image

Here’s my code:

sensor:
  - platform: pulse_counter
    pin: D5
    unit_of_measurement: 'kW'
    name: 'Power Meter House'
    filters:
      - multiply: 0.06
    id: house_power

  - platform: total_daily_energy
    name: "Total Daily Energy"
    power_id: house_power
    accuracy_decimals: 1

That gives me two sensors in HA, which I show like this:

image

(ignore yesterday’s low reading: I had the device apart to add a temperature sensor)

The power reading has a resolution of 60W, which is one pulse per minute. Long-term average is “perfect” down to zero power as it counts every flash from the meter, but if you’re only drawing 6W (one flash every ten minutes) it’ll return 0W for nine of the minutes and 60W for the minute that the flash happened in. You can see the 60W resolution as the “steps” in the low-power areas, but that resolution is fine for me.

image

1 Like

That may be why I wasn’t understanding the issue. I was thinking dave_sausages was counting pulses. I see now, pulse width was being used.

I was using pulse counter, but didn’t like the poor accuracy of it due to rounding errors, so switched to pulse width.

I’m still trying to work out how to increase the maximum pulse width I can measure in case anyone can help?