Hi guys,
I am a total newbie with ESP32 Thing, sorry. I am looking for a way to read a rotary encoder with a push button (EC11). I have found many examples that work well with Arduino Uno, but I am struggling to find a library, or code, that reads the rotary encoder reliably with the ESP32. If you can help, or know of examples, please let me know.
Kind regards
David
Help with Rotary Encoder and ESP32 Thing
Re: Help with Rotary Encoder and ESP32 Thing
I was playing with the pulse counter module a little while ago for something different. Upload this and see if it's useful for you:
Encoder A to 13, Encoder B to 14, Encoder GND to GND.
Code: Select all
#include "driver/pcnt.h"
#define ENCODER_PIN_A GPIO_NUM_13
#define ENCODER_PIN_B GPIO_NUM_14
#define PULSE_COUNT_UNIT PCNT_UNIT_0
int16_t theCounter = 0;
static void initPulseCounter(void)
{
pcnt_config_t pcnt_config = {
ENCODER_PIN_A,
ENCODER_PIN_B,
PCNT_MODE_KEEP,
PCNT_MODE_REVERSE,
PCNT_COUNT_INC,
PCNT_COUNT_DIS,
PULSE_COUNT_UNIT,
PCNT_CHANNEL_0
};
/* Initialize PCNT unit */
pcnt_unit_config(&pcnt_config);
/* Configure and enable the input filter */
pcnt_set_filter_value(PULSE_COUNT_UNIT, 1023);
pcnt_filter_enable(PULSE_COUNT_UNIT);
/* Initialize PCNT's counter */
pcnt_counter_pause(PULSE_COUNT_UNIT);
pcnt_counter_clear(PULSE_COUNT_UNIT);
/* Everything is set up, now go to counting */
pcnt_counter_resume(PULSE_COUNT_UNIT);
}
void setup()
{
Serial.begin(115200);
Serial.printf("Counter: %d\n", theCounter);
initPulseCounter();
}
void loop()
{
int16_t thisCount;
pcnt_get_counter_value(PULSE_COUNT_UNIT, &thisCount);
if(thisCount!=theCounter)
{
theCounter=thisCount;
Serial.printf("Counter: %d\n", theCounter);
}
}
-
- Posts: 9
- Joined: Thu Dec 06, 2018 4:20 am
Re: Help with Rotary Encoder and ESP32 Thing
Encoder A /CLK wiring to GPIO13
Encoder B /DT wiring to GPIO14
Regards
NewPrasertKitt
Encoder B /DT wiring to GPIO14
Regards
NewPrasertKitt
Re: Help with Rotary Encoder and ESP32 Thing
If you have problems with bouncing (the signal jumps on many devices), you should install a de-bouncing. This can be electrical or in the software.
I have already gained experience and the best way is an "encoder table". Since you have two data lines (even if one is marked as a clock), you get an expected pattern with HIGH / LOW on line 1 and 2. This pattern you can compare with the valid pattern and thus you get perfect data.
Search Google for "encoder table" and write your own class.
I have already gained experience and the best way is an "encoder table". Since you have two data lines (even if one is marked as a clock), you get an expected pattern with HIGH / LOW on line 1 and 2. This pattern you can compare with the valid pattern and thus you get perfect data.
Search Google for "encoder table" and write your own class.
Who is online
Users browsing this forum: No registered users and 60 guests