Pulse_width sensor sticking

I have a an optical sensor with the LED emitting 50% duty, and I’m reading the received phototransistor “value” using the pulse_width sensor.

Ive got two problems,

  1. When I physically block the optosensor, a lot of the time the previous value gets stuck, and so its possible to get an incorrect reading. This looks like it’s because theres no “timeout” in the pulse_width sensor implementation like Arduino’s pulseIn() function has. Am I missing something or do I need to write my own custom PWM reading sensor?

  2. When just left alone running in static environmental conditions, most of the time I’m getting the correct reading, but occasionally (once a second?) I’m reading 0s from the sensor. I can force this if i set the PWM frequency “too high”, but this is running at 40Hz and I’d have expected an ESP8266 to be able to handle this. Is this unreasonable?

Is there a simple solution to this before I start writing my own custom sensor?

Thanks

If your LED is emitting fixed 50% duty, what are you actually trying to read with your sensor? I immagine that pulse width sensor is made to measure duty cycle.

Its a reflective opto for door open/closed detection which needs to not be sensitive to daylight, the usual solution to this is to use an oscillating emitter

So what you are interested on is just the open/closed but you need this sensor to verify that it’s not constant (from daylight)?

The documentation sucks. But it mentions that it’s measuring length of high signal. Did you wire your phototransistor as common collector or -emitter?

Its an emitter follower but it could be the opposite.

Reading the code closely for the pulse_width sensor its obvious what its doing and also answers my question. If a PWM signal ends then the last pulse-width value is retained. I’m going to fork it and add a timeout value so that it resets to 0

Actually, maybe the pulse_counter or pulse_meter will do what I need with some filters in place…

Worth a try. At least those components are made for continuous counting (pulse_width sensor might be for occasional probing of duty cycle).