Help with Rotary Encoder and ESP32 Thing

davidqvist
Posts: 2
Joined: Sat Dec 15, 2018 10:31 am

Help with Rotary Encoder and ESP32 Thing

Postby davidqvist » Sat Dec 15, 2018 11:01 am

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

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: Help with Rotary Encoder and ESP32 Thing

Postby boarchuz » Sat Dec 15, 2018 9:23 pm

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.

NewPrasertKitt
Posts: 9
Joined: Thu Dec 06, 2018 4:20 am

Re: Help with Rotary Encoder and ESP32 Thing

Postby NewPrasertKitt » Wed Dec 19, 2018 2:37 pm

Encoder A /CLK wiring to GPIO13
Encoder B /DT wiring to GPIO14

Regards
NewPrasertKitt

DrDoom
Posts: 5
Joined: Thu Dec 20, 2018 10:16 am

Re: Help with Rotary Encoder and ESP32 Thing

Postby DrDoom » Thu Dec 20, 2018 10:33 am

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.

Who is online

Users browsing this forum: Google [Bot] and 74 guests