I want to count pulses on GPIO 4 input with my ESP32. Luckily there is a driver by espressif:
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/pcnt.html
Unluckily it does not work for me. I am using platformIO with arduino framework. I have connected pin 22 and pin 4 and I can see pulses on my oscilloscope. This is my code:
#include "Arduino.h"
#include <driver/pcnt.h>
#define ARDUINO_RUNNING_CORE 1
void TaskReadPulses(void *pvParameters) // This is a task.
{
(void)pvParameters;
pcnt_config_t pcntCh1 = {
.pulse_gpio_num = 4,
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
.lctrl_mode = PCNT_MODE_KEEP,
.hctrl_mode = PCNT_MODE_KEEP,
.pos_mode = PCNT_COUNT_DIS,
.neg_mode = PCNT_COUNT_INC,
.counter_h_lim = 32767,
.counter_l_lim = -32768,
.unit = PCNT_UNIT_0,
.channel = PCNT_CHANNEL_0,
};
pinMode(22, OUTPUT);
Serial.begin(115200);
Serial.print("Calling unit_config: ");
Serial.println(pcnt_unit_config(&pcntCh1));
while (true) {
for (int i = 0; i < 50; i++) {
digitalWrite(22, HIGH);
vTaskDelay(1 / portTICK_PERIOD_MS);
digitalWrite(22, LOW);
vTaskDelay(1 / portTICK_PERIOD_MS);
}
int16_t counterVal;
Serial.print("Calling get_counter_value: ");
Serial.println(pcnt_get_counter_value(PCNT_UNIT_0, &counterVal));
Serial.print("Counter Value: ");
Serial.println(counterVal);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void setup() {
xTaskCreatePinnedToCore(
TaskReadPulses,
"AnalogReadA3",
1024,
NULL,
1,
NULL,
ARDUINO_RUNNING_CORE);
}
void loop() {}
and this is what it returns:
Calling unit_config: 0
Calling get_counter_value: 0
Counter Value: 0
Calling get_counter_value: 0
Counter Value: 0
...
Error code for 0 is success, so I have no idea why it does not count.