Use 4×4 keypad with STM32
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.
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
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.