Use 4×4 keypad with STM32

Description

100%
100%

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

Actually It does not matter which keypad you use or how many KEYs it have. We will use 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

The following are some important lines used in 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.

100%
100%

Connection

100%
100%
100%
100%

Result

100%
100%
Check out the VIDEO Below
100%
100%

DOWNLOAD

You can buy me a coffee sensor ūüôā

download the CODE below

100%
100%
, , , , , , ,

Leave a Reply

avatar
  Subscribe  
Notify of
Menu