# Charlieplexing

Note:  If you are seeing this note, then you are looking at an incomplete draft.  Keep checking back as I complete these series of blog posts over the next week or two.

You might be familiar with Charlieplexing with LEDS, but the same technique can be adapted for switches.   Let’s start by replacing LEDs and resistors with diodes and switches in the familiar Charlieplexing schematic layout. For greater clarity, we are assuming the microprocessor has weak pullups enabled and can omit the external pullup resisors on the lines. This still looks insane and it gets even worse with more switches.   Let’s find a more systematic way to represent the connections. All we’ve done here is swap the position of some diodes and switches, but the overall circuit is electrically identical.  Now we can begin to make the relation to a conventional scanning matrix more clear. In this case, the common connection at the cathode for a group of switches would have been a column in a typical matrix configuration.  Let’s make the similarity even more clear by rearranging like this: Basically, the columns are tied into the row lines.  Since we’re using a given row to select a “column”, we can’t use it to read that row at the same time. Thus, there is one less switch per row.  So with n lines, you can read n(n-1) switches.

When a “column” is selected, the other lines are tri-stated with pullups enabled to serve as inputs to read the rows.  For instance, when column 1 is selected, lines 2 & 3 function as input rows.  When column 2 is selected, lines 1 and 3 become input rows.

```#define NUM_COLS (n)
#define NUM_ROWS (NUM_COLS - 1)

uint8_t keyMatrix[NUM_ROWS][NUM_COLS];

uint8_t buttonPressed(uint8_t row, uint8_t col)
{
return keyMatrix[row][col];
}

void deselectCols(void)
{
TRIS_GPIO0 = 1;
TRIS_GPIO1 = 1;
...
TRIS_GPIO(n-1) = 1;
}

void selectCol(uint8_t col)
{
deselectCols();

// Col LAT should already be set to LOW
// so just enable the driver out of
// tri-state mode so it can pull the
// column low.
switch (col)
{
case 0:
TRIS_GPIO0 = 0;
break;

case 1:
TRIS_GPIO1 = 0;
break;
...
case (n):
TRIS_GPIO(n-1) = 0;
break;
}
}

{
// Read Rows.  Switches will be active low,

// Here is concrete example, using 3 GPIO
// lines in a 3 x 2 matrix.  The key to
// remember is there's n-1 rows and you
// cannot read a row from selected col.
switch(col)
{
case 0:
keyMatrix[col].input = !GPIO1;
keyMatrix[col].input = !GPIO2;
break;
case 1:
keyMatrix[col].input = !GPIO0;
keyMatrix[col].input = !GPIO2;
break;

case 2:
keyMatrix[col].input = !GPIO0;
keyMatrix[col].input = !GPIO1;
break;
}
}

//call every millisecond
void scanMatrix(void)
{
for (uint8_t i =0; i< NUM_COLS; i++)
{
selectCol(i);
// Give time for voltage levels to stabilize
__delay_us(50);
deselectCols();

}
}

void initMatrixIO(void)
{
//configure GPIO pins

// All digital mode
ANSEL_GPIO(0..n-1) = 0;

// Set all latches low. The latches needs to
// remain low for the rest of the code
//  to work. This way they can pull columns
// low when no longer in tri-state.
LAT_GPIO1(0..n-1) = 0;

deselectCols();

// Enable all pullups.  This is necessary
// when pin is serving as input.
// Should do no harm when it's an output.
WPU_GPIO(0..n-1) = 1;

}

void initKeyMatrix(void)
{
initMatrixIO();

for (uint8_t i = 0; i < NUM_ROWS; i++)
for (uint8_t j = 0; j < NUM_COLS; j++)
keyMatrix[i][j] = 0;
}
```

The main drawback to Charlieplexing is that it’s no longer possible to resolve the ghosting problem when multiple keys are pressed at once.  The other is that you can’t use a keypad wired in a standard matrix.

I know you’ll stop being my friend if I leave you without a working example, so I hacked together a 5 x 4 Charlieplexed key array from five 1 x 4 key matrix strips. <USB HID Keyboard example firmware using 5 x 4 matrix>