Page 1 of 1

Help with Rotary Encoder and ESP32 Thing

Posted: Sat Dec 15, 2018 11:01 am
by davidqvist
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

Re: Help with Rotary Encoder and ESP32 Thing

Posted: Sat Dec 15, 2018 9:23 pm
by boarchuz
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:

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);
  }
}
Encoder A to 13, Encoder B to 14, Encoder GND to GND.

Re: Help with Rotary Encoder and ESP32 Thing

Posted: Wed Dec 19, 2018 2:37 pm
by NewPrasertKitt
Encoder A /CLK wiring to GPIO13
Encoder B /DT wiring to GPIO14

Regards
NewPrasertKitt

Re: Help with Rotary Encoder and ESP32 Thing

Posted: Thu Dec 20, 2018 10:33 am
by DrDoom
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.