Interfacing Encoder Switch with ESP 32

Pallav Aggarwal
Posts: 5
Joined: Wed Mar 18, 2020 5:32 pm

Interfacing Encoder Switch with ESP 32

Postby Pallav Aggarwal » Sun Mar 29, 2020 8:59 am

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?
Best Regards
Pallav Aggarwal
https://pallavaggarwal.in/

username
Posts: 538
Joined: Thu May 03, 2018 1:18 pm

Re: Interfacing Encoder Switch with ESP 32

Postby username » Sun Mar 29, 2020 6:48 pm

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.

Pallav Aggarwal
Posts: 5
Joined: Wed Mar 18, 2020 5:32 pm

Re: Interfacing Encoder Switch with ESP 32

Postby Pallav Aggarwal » Mon Mar 30, 2020 4:31 pm

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.
Best Regards
Pallav Aggarwal
https://pallavaggarwal.in/

Who is online

Users browsing this forum: No registered users and 246 guests