MCP23017 using encoder

Status
Not open for further replies.

rfresh737

Well-known member
I've got an MCP23017 wired up to my bread board and it is working fine with push buttons.

A lot is on the internet about getting encoders to work with an MCP23017. Most have not had any luck in getting an encoder to work with the MCP.

While I don't have get my encoder to work with an MCP either as I have enough pins on my Teensy 3.6 to wire it up that way and make it work, I thought I'd try a little bit more just for the education of it and see if I'm close to getting it to work.

What I see if I turn my encoder knob cw or ccw is unreliable click results. I'm printing out 'left' and 'right' and I see mixed results. Pretty much what others have reported when they have tried to do this.

However, if I slowly try to turn the knob to the right and hold it there and don't complete the click, I can see 'right' being printed out in the output window repeatedly...thus showing (initially) correct sensing...as long as I don't compete the click. Same for turning to the left...I can just start to turn the knob ccw and hold it there and watch the stream of 'left' display on the output window.

I'm wondering if I can detect that first initial turn in my code below and thus obtain correct directional sensing?

Code:
  // Loop block
  // MCP23017 Chip #2 -----------------------------------------------
  Wire.beginTransmission(mcp2_address);
  Wire.write(GPIOA);                 // set pointer to bank A pins
  Wire.endTransmission();
  Wire.requestFrom((int)mcp2_address, 1); // request one byte
  while(Wire.available())            // slave may send less than requested
  { 
    c =~( Wire.read());              // receive a byte as character
  }
  delay(25);
  checkKeyPress_MCP2_BankA(c);
  Wire.beginTransmission(mcp2_address);
  Wire.write(GPIOB);                 // set pointer to bank B pins
  Wire.endTransmission();
  Wire.requestFrom((int)mcp2_address, 1); // request one byte
  while(Wire.available())            // slave may send less than requested
  { 
    c =~( Wire.read());              // receive a byte as character
  }
  delay(25);
  checkKeyPress_MCP2_BankB(c);
  // MCP23017 Chip #2 -----------------------------------------------


Code:
void checkKeyPress_MCP2_BankA(byte c)
{
  switch(c)
  {
   case 2:
	   Serial.println("left");
      break;
  }
}

void checkKeyPress_MCP2_BankB(byte c)
{
  switch(c)
  {
   case 128:
	   Serial.println("right");
      break;
  }
}
 
If you really need to you can run multiple encoders through some multiplexers with really no problems as long as your sketch repeats fast enough.
 
mcu pins and interrupts are definately faster than the MCP23017, if you want performance, go direct, or use an MCP23S17 (SPI), be sure to use it’s interrupt line for fast responses as well :)
 
Status
Not open for further replies.
Back
Top