Page 1 of 1

Interfacing Encoder Switch with ESP 32

Posted: Sun Mar 29, 2020 8:59 am
by Pallav Aggarwal
Hi,

I am interfacing ESP32 with an encoder switch and not able to read it properly in both directions. If I limit it to one direction, it works almost fine.

Here is the configuration:

Code: Select all

#define ENC_DATA					25
#define ENC_CLK						33
#define ENTER						26

inside main before while(1):

	gpio_set_direction(ENTER, GPIO_MODE_INPUT);
	gpio_pullup_en(ENTER);
	
	gpio_set_direction(ENC_CLK, GPIO_MODE_INPUT);
	gpio_set_intr_type(ENC_CLK, GPIO_PIN_INTR_NEGEDGE);
	gpio_intr_enable(ENC_CLK); // Enable Interrupt
	gpio_pullup_en(ENC_CLK);
	
	gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
	
	//remove isr handler for gpio number.
    gpio_isr_handler_remove(ENC_CLK);
    //hook isr handler for specific gpio pin again
    gpio_isr_handler_add(ENC_CLK, gpio_isr_handler, (void*) ENC_CLK);
	
	gpio_set_direction(ENC_DATA, GPIO_MODE_INPUT);
	gpio_pullup_en(ENC_DATA);

Interrupt Routine is line this:

Code: Select all

static void IRAM_ATTR gpio_isr_handler(void* arg)
{
	uint8_t tmp = 0;

	tmp = gpio_get_level(ENC_DATA);
	if(tmp) // if pin is high
	{
		if(cct < 200) cct++;
		else cct = 200; 
	}
	else   // If pin is low
	{
		if(cct > 0) cct--;
		else cct = 0;
	}

}

In the main loop I am displaying the cut variable on a display 4 digits to see whats going on.
Could you please suggestion what could be the issue?

Re: Interfacing Encoder Switch with ESP 32

Posted: Sun Mar 29, 2020 6:48 pm
by username
It could be a debouncing issue. I.E. when it transitions from high to low or visa-versa you will get several interrupts at once. You could try fixing that with a cap on that pin, or do it in software.

Re: Interfacing Encoder Switch with ESP 32

Posted: Mon Mar 30, 2020 4:31 pm
by Pallav Aggarwal
I had a debouncing circuit already in place, I see some curved rising and falling edge. Let me make it straight by tweaking the R in the RC circuit.