interfacing keyboard

6,716

Circuit Image

A keyboard is a fundamental component of an embedded or microcontroller system. For small-scale and hobby projects, a 4x4 (hex) matrix keyboard is adequate. The keys in the matrix keyboard are arranged in a matrix configuration to efficiently utilize the port pins of the microcontrollers. This setup allows for the interfacing of 16 keys using only 8 pins of the microcontroller, as illustrated in the circuit diagram. The rows are connected to 4 pins of a port, while the columns are connected to another 4 pins. The rows are configured as inputs and the columns as outputs. By grounding one column and setting all other columns high, the status of the rows can be observed to determine which button is pressed. For instance, if the first column is grounded and the first row reads low, then the first button is activated. Microcontrollers operate at high speeds, enabling them to detect key presses in microseconds. However, if a switch is held down for an extended period (which is considered milliseconds for a microcontroller), the microcontroller may register multiple triggers. Additionally, switch debounce, caused by the mechanical action of the switch, can introduce further complications. To mitigate these issues, a delay should be implemented after each key press.

The 4x4 matrix keyboard circuit consists of 16 keys arranged in 4 rows and 4 columns, allowing for efficient use of microcontroller pins. The microcontroller's GPIO (General Purpose Input/Output) pins are divided into two groups: one for rows and another for columns. Each row pin is configured as an input, while each column pin is set as an output. This configuration enables the detection of button presses through a process known as scanning.

During operation, one column is driven low while the remaining columns are set high. The microcontroller then reads the state of the row pins. If a button connected to a grounded column and a specific row is pressed, the corresponding row pin will read low. This allows the microcontroller to identify which button has been pressed based on the combination of active column and row.

To address the issue of switch bounce, which can cause erroneous multiple readings when a button is pressed or released, a delay is implemented in the software after each key press detection. This delay allows the mechanical switch to settle, ensuring that subsequent readings are stable and accurate. The assembly source code provided in the accompanying documentation outlines the specific instructions required for key scanning and debounce management, facilitating the integration of the matrix keyboard into various embedded applications.Keyboard is a basic and essential element of an embedded or microcontroller system. For small and hobby projects a 4x4 (hex) matrix keyboard is sufficient. The keys in the matrix keyboard are arranged in a matrix arrangement in order to utilize the port pins of the microcontrollers efficiently. Here we can interface 16 keys by using just 8 pins of microcontroller. This is shown in the circuit diagram. As shown in the circuit diagram, the rows are connected to 4 pins of a port, the columns are connected to other four pins, we configure rows as input and columns as output. By grounding one column and setting high all other columns, now by observing the status of the rows, we can come to know which button is pressed.

For example, if we ground the first column and if the first row is low, then the first button is pressed. We know that microcontrollers are really fast, therefore they can detect the key press in microseconds, if we hold the switch for long time (for a microcontroller long time is in milliseconds), the microcontroller is triggered more than once, also there is a problem of switch debounce because of the spring action of the switch.

To eliminate these problems we should introduce some amount of delay after every key press. The assembly source code for the circuit is given in the table below. 🔗 External reference