Search code examples
cesp32

ESP32 how to reset GPIO pins used with PCNT, so they can also be used for EXT1 wake up from deep sleep?


I configure 4 gpio pins as pulse counters to read Hall-effect liquid flow sensors, which works fine, but when I try to use those same pins to wake up from a deep sleep, sleep ends immediately. If I don't configure the pins for PCNT, deep sleep works as expected.

I even went so far as setting a flag in RTC memory so that it sleeps for as long as 5 seconds, wakes up and does not configure PCNT before trying to go back to sleep. It still wakes from the second sleep immediately, so whatever it is about those pins is retained across deep sleep.

Is there a way to restore the default state of those pins without resetting the entire device?


Solution

  • The answer is, set both *_gpio_num members of pcnt_config_t to PCNT_PIN_NOT_USED and call pcnt_unit_config again:

    pcnt_config_t pcnt_config = {
        .pulse_gpio_num = PCNT_PIN_NOT_USED,
        .ctrl_gpio_num = PCNT_PIN_NOT_USED,
        .channel = PCNT_CHANNEL_0;
    }
    pcnt_unit_config(&pcnt_config);