Hello. First, this is my first time posting in a forum like this, so there may be something rude or incomplete. I apologize in advance.
I am currently trying to create a program to read the pulses of a fast rotating rotary encoder using the ESP32 pulse counter. My problem is that as I increase the speed of the encoder, I am missing counts. My encoder is a high resolution encoder that produces 3600 pulses per revolution and I want to get an accurate count with it spinning at 1200 rpm.
The ESP32 pulse counter should be able to count accurately up to 40 MHz, so it should be possible to achieve this, but it does not work. I would like to know what I should do.
Here is my code.
#include "driver/pcnt.h"
#define PULSE_INPUT_PIN 32
#define PULSE_CTRL_PIN 33
#define PCNT_H_LIM_VAL 32767
#define PCNT_L_LIM_VAL -32767
int16_t count = 0;
void setup() {
pcnt_config_t pcnt_config;
pcnt_config.pulse_gpio_num = PULSE_INPUT_PIN;
pcnt_config.ctrl_gpio_num = PULSE_CTRL_PIN;
pcnt_config.lctrl_mode = PCNT_MODE_REVERSE;
pcnt_config.hctrl_mode = PCNT_MODE_KEEP;
pcnt_config.channel = PCNT_CHANNEL_0;
pcnt_config.unit = PCNT_UNIT_0;
pcnt_config.pos_mode = PCNT_COUNT_INC;
pcnt_config.neg_mode = PCNT_COUNT_DIS;
pcnt_config.counter_h_lim = PCNT_H_LIM_VAL;
pcnt_config.counter_l_lim = PCNT_L_LIM_VAL;
pcnt_unit_config(&pcnt_config);
pcnt_counter_pause(PCNT_UNIT_0);
pcnt_counter_clear(PCNT_UNIT_0);
Serial.begin(115200);
pcnt_counter_resume(PCNT_UNIT_0);
}
void loop() {
pcnt_get_counter_value(PCNT_UNIT_0, &count);
if(count > 3600) pcnt_counter_clear(PCNT_UNIT_0);
if(count < -3600) pcnt_counter_clear(PCNT_UNIT_0);
Serial.print("Counter value: ");
Serial.println(count);
}