# Keyboards

#### Key Arrangement

There is a lot of material to cover so I will jump straight to the point.

Here are some key points on working with keyboards:

• Keys on a keyboard are organized in a matrix of rows and columns
• The microcontroller accesses both rows and columns through GPIO ports.
• With  two  8-bits ports,  64 keys can be accessed. This is because 64 keys can be arrange into a 8 x 8 matrix.
• When a key is pressed, a row and a column make contact in order to detect the particular key.

#### Key Identification

Remember, as mentioned above, the keys are not simply connected to the microcontroller as input pins, rather they are arranged in columns and rows in order  to save the number of GPIO pins required to read from a keyboard. I f we were to connect each of a 64-keys keyboard to the microcontroller as input pins, we will require 64 pins to do this. With the matrix arrangement, we require just 16 pins.  Lets see how the microcontroller scans and identifies the keys. The images below show the circuitry of the matrix arrangement of a 4×4  (16 keys) keyboard and its pinout.

Circuitry of a 4×4 keyboard

4×4 keyboards

From the image above, we can see that the rows are connected as output pins and the columns are connected as input pins.

When a key is pressed, the microcontroller drives all the rows low, then reads the columns.

Lets say the data read from the column is D7 – D4  = 1111, this implies that no key has been pressed.The process continues until a key press is detected.

However if one of the column bit is zero E.g. D7 – D4 = 1101, this will mean that D5 column has been pressed.

The next task will be to find out which particular key has been pressed. This will be quite easy since the columns are connected to separate pins as inputs.

We could store the various keys in a 4×4 array and access them using their indices. It will look something like this :

```
{
const unsigned char keymap[4][4] = {
{ '1', '2', '3', 'A'},
{ '4', '5', '6', 'B'},
{ '7', '8', '9', 'C'},
{ '*', '0', '#', 'D'},
};
.
.
if (col == 0xE0) return keymap[row][0]; // key in column 0
if (col == 0xD0) return keymap[row][1]; // key in column 1
if (col == 0xB0) return keymap[row][2]; // key in column 2
if (col == 0x70) return keymap[row][3]; // key in column 3
.
.
.

```

Please  follow the link below to see full example code of the keypad .

London, U.K
© 2019 BOHOBIOM LTD