How to interface Keyboard with STM32

Today in this tutorial I am going to interface a 4x4 keypad with STM32. I am using STM32F103C8 microcontroller and the keypad is a membrane switch
4x4 matrix keypad which looks like shown below


Actually It does not matter which keypad you use or how many KEYs it have. We will be using a method which can be used universally with any type of keypad or microcontroller out there.


HOW TO

So let's start this discussion on How to actually interface a keypad with any microcontroller. The concept is pretty simple. The Keys in the keypad are divided into ROWs and COLUMNs and we are going to use this to find out which key has been pressed.

As shown in the picture above the Keypad have 16 keys and they are divided in 4 ROWs and 4 COLUMNs. All the keys in a single ROW are interconnected and all keys in a single COL are interconnected as shown in the picture below



Whenever the key is pressed, the connection between ROW and COL get completed and the COL pin will read 0. This will tell us that the ROW and COL are both 0 (low) and we will know what key was pressed.

For eg- Let's say I pressed 5. To identify this, I need to pull the second ROW to LOW and than check for the Columns. Whenever the '5' is pressed, the second column will become LOW as the connection between second ROW and second COL will be completed. And this combination of ROW and COLUMN being LOW, will be assigned value '5'.

The method I am using here is the POLLING as the microcontroller keep on polling for the key pressed. An interrupt on the key press would obviously be better which I will try to implement in coming days.


Some Insight into the CODE


 /* Make ROW 1 LOW and all other ROWs HIGH */
 HAL_GPIO_WritePin (R1_GPIO_Port, R1_Pin, GPIO_PIN_RESET);  //Pull the R1 low
 HAL_GPIO_WritePin (R2_GPIO_Port, R2_Pin, GPIO_PIN_SET);  // Pull the R2 High
 HAL_GPIO_WritePin (R3_GPIO_Port, R3_Pin, GPIO_PIN_SET);  // Pull the R3 High
 HAL_GPIO_WritePin (R4_GPIO_Port, R4_Pin, GPIO_PIN_SET);  // Pull the R4 High
 
 if (!(HAL_GPIO_ReadPin (C1_GPIO_Port, C1_Pin)))   // if the Col 1 is low
 {
  while (!(HAL_GPIO_ReadPin (C1_GPIO_Port, C1_Pin)));   // wait till the button is pressed
  return '1';
 }
 
 if (!(HAL_GPIO_ReadPin (C2_GPIO_Port, C2_Pin)))   // if the Col 2 is low
 {
  while (!(HAL_GPIO_ReadPin (C2_GPIO_Port, C2_Pin)));   // wait till the button is pressed
  return '2';
 }
 
 if (!(HAL_GPIO_ReadPin (C3_GPIO_Port, C3_Pin)))   // if the Col 3 is low
 {
  while (!(HAL_GPIO_ReadPin (C3_GPIO_Port, C3_Pin)));   // wait till the button is pressed
  return '3';
 }
 
 if (!(HAL_GPIO_ReadPin (C4_GPIO_Port, C4_Pin)))   // if the Col 4 is low
 {
  while (!(HAL_GPIO_ReadPin (C4_GPIO_Port, C4_Pin)));   // wait till the button is pressed
  return 'A';
 }

Here first we are making ROW1 LOW and all other ROWs as HIGH. Than we keep monitoring for the columns. If COL1 is low, that means the key pressed is '1' or else if the COL2 is low, that means the key pressed is '2' and so on..
Likewise we do this for other ROWs as well until all ROWs are covered.


You can Download code at the end of the post


RESULT




VIDEO




To DOWNLOAD THE FULL CODE, VISIT https://controllerstech.com/use-4x4-keypad-with-stm32/

How to interface Keyboard with STM32 How to interface Keyboard with STM32 Reviewed by Controllerstech on October 01, 2019 Rating: 5

1 comment:

  1. Hey, did you write a Code for Interrupts aswell

    ReplyDelete

Powered by Blogger.