ESPHome and fast ADC readings

Hello there.

I am trying to to a research if my goal would be possible via ESPHome and ESP8266 MCU.

I would like to attach photoresistor to ESP8266 and measure ADC values (basically I need to measure accurately, how long the light source is ON). I would like to measure the duration, how long the value stays above some sort of threshold, i.e. like… if say we have 1024 max ADC value, I would like to measure, how long it stays above 800 before going down below 800. After the duration is determined, MQTT message will be published.

The problem is that I need relatively high accuracy (at least 0.1s and below). I need to measure durations starting from ~0.5s to roughly 10-15s. I would say that I need around 50ms sampling rate to measure durations accurately enough. I learned somewhere, that lambdas are executed at ~60Hz, but not sure, how that will work together with ADC readings.

Do you think it is plausible to implement this kind of logic?

It sounds like it might be pushing things but I don’t know.

I think you’re right that a loop is 60hz or ~16ms.

I would ask this Q on ESPHome Discord where the devs hang out.

They might for example suggest a custom component.

There’s this kind of thing but it’s not a direct nice fit.

And I recall the template binary sensor runs every loop but again not such a nice fit.

And I got curious and spotted this too.

I guess you might be about to chain the above three together. Make a Binary sensor from an ADC sensor, and then feed the pulse width sensor with that?

Thanks for you suggestions. Actually I was not aware of Pulse Width Sensor and it seems interesting enough. But I do not understand how it actually works.

If it does have “update interval” of, let’s say, 60s, so what will it return if a given pin had multiple ups and downs within that timeframe? There is another sensor, Pulse Width Counter and I would think that it can, for example, return count of rises etc. within a specific timeframe, but how should this pulse width work, when it actually measures duration and can return a single duration as a result?

Do you have any ideas on that?

Thanks

Good Q. I have no idea;)

Not a solution, but, I’m testing something right now and with the ESP8266 connected to an AP I cannot go faster than 4ms between samples without some magic (250Hz). If it itself an AP, it can go a bit faster but I did not benchmark it yet. Only tested at 80MHz and without ESPHome.