Page 1 of 1

PCNT module for esp32

Posted: Thu Jun 24, 2021 5:30 am
by argsngh18
Hi,

For my frequency calculation application, ranging from 0.1Hz to 50KHz, running on ESP32, I am using a PCNT(Pulse counter) module to accumulate pulse and a 1-millisecond software timer to monitor the sampling period.

Complication : I am capturing the time stamp and start sampling period at the start of first-falling edge of pulse-train and upon elapse of sampling-period, flagged by s/w timer of 1ms, and then calculating frequency. However the pcnt_get_counter st some instances give +/-x counts where x -ranges from 5-50ish. To which there is error in calculate frequency.

P.S. : For frequency below 5KHz the mentioned complication is not observed

Till now I have confirmed that the s/w timer 95% of times is accurate but most of the time pcnt counts retrieved from API are not correct

I am planning to use high resolution esp_timer in place of software timer

Welcome your help !

Re: PCNT module for esp32

Posted: Thu Jun 24, 2021 6:00 pm
by biterror
I would do the pulse "gating" completely in hardware to avoid the unpredictable delays and jitter of interrupt code. I haven't used the PCNT peripheral, but isn't it possible to enable/disable the counting using the CTRL input - maybe you could generate a 1 ms long pulse using a timer / PWM / SPI / whatever and loop it back to the CTRL input and then read the counter value after the pulse has occurred?