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,
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?
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?
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
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