{"id":3428,"date":"2011-08-25T23:22:48","date_gmt":"2011-08-26T03:22:48","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=3428"},"modified":"2012-07-30T09:29:16","modified_gmt":"2012-07-30T13:29:16","slug":"lab-18-matrix-keypad-interfacing","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/lab-18-matrix-keypad-interfacing\/","title":{"rendered":"Lab 18: Matrix keypad interfacing"},"content":{"rendered":"<p>Matrix keypads are very common input devices in embedded systems. They have simple architecture and are easy to interface. One good thing about them is that they allow you to interface a large number of input keys to a microcontroller with minimum usage of \u00a0I\/O resources. This tutorial describes two different approaches of reading input data from a 4&#215;4 (16 keys) matrix keypad interfaced to a PIC microcontroller. The pressed key information is displayed on a character LCD. The microcontroller used in this experiment is PIC16F1827.<\/p>\n<div id=\"attachment_3444\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadTitle.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3444\" class=\"size-full wp-image-3444\" title=\"KeyPadTitle\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadTitle.jpg\" alt=\"\" width=\"560\" height=\"373\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadTitle.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadTitle-550x366.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3444\" class=\"wp-caption-text\">4x4 matrix keypad interfaced to PICMicro<\/p><\/div>\n<p><!--more--><\/p>\n<p><strong>Theory<\/strong><\/p>\n<p>Matrix keypads are simply an extension to the simple tact switch inputs. They consists of keys interconnected in the shape of a matrix. Each key is a simple mechanical switch located at the crossing between the matrix rows and columns. When a key is pressed, its row and column\u00a0form an electrical contact. The rows and columns can be connected to the pins\u00a0of microcontroller ports. The big advantage of using a matrix keypad is that it allows to interface a large number of keys with a relatively small number of microcontroller pins. For example, a 16-key keypad requires only 8 (instead of 16, if interfaced individually) I\/O pins of the microcontroller if organized into a 4 rows and 4 columns matrix. The circuit diagram for this experimental tutorial is shown below. It interconnects a 4&#215;4 matrix keypad to PORTB of the PIC16F1827 microcontroller. The microcontroller runs at 500 KHz using the internal clock source. The PORTA pins drive a character LCD that displays the pressed key information.<\/p>\n<div id=\"attachment_3439\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/MatrixKeyPadCkt.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3439\" class=\"size-medium wp-image-3439\" title=\"MatrixKeyPadCkt\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/MatrixKeyPadCkt-550x441.png\" alt=\"\" width=\"550\" height=\"441\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/MatrixKeyPadCkt-550x441.png 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/MatrixKeyPadCkt.png 622w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-3439\" class=\"wp-caption-text\">Interfacing a 4x4 matrix keypad to PICMicro<\/p><\/div>\n<p>Now lets talk about the different approaches of keypad scanning, which is required to identify which key has been pressed.<br \/>\n<em><strong>Sequential exploration of rows:<\/strong><\/em> In this method, the I\/O pins connecting the rows are all configured as output and those connecting the columns are defined as input, or vice-versa. The column lines, which are inputs to the microcontroller, are pulled-high using the internal pull-up resistors. Therefore, the default input to these lines is 1. The state for the matrix keypad can be explored by turning the rows low sequentially, one at a time, and reading the columns. For example, set the first row to 0 and read all the columns. If any key has been pressed in that row, the corresponding column line must read as 0. Otherwise, go to the next row and set it to 0, and read the columns again. This process is repeated until a 0 is found in a column. This determines the row and column for the pressed key, thus giving the exploration code for that key.<br \/>\n<em><strong>Simultaneous exploration of rows and columns:<\/strong><\/em> In this method, all rows and columns are explored simultaneously in two phases. First, the rows are configured as output and the columns as input. Internal pull-ups are enabled on the column lines. Then all rows are set to 0 and the columns are read. In any key has been pressed, the corresponding column will be read as 0. This detects the column but not its row. The whole process is reversed next. The rows are configured as input and the columns as output. Internal weak pull-ups are now enabled on row lines. All columns are set to 0, and the rows are read. The row that reads 0 contains the pressed key. This gives both the row and column for the pressed key. This approach of exploring the keypad is relatively faster than\u00a0sequential exploration approach.<\/p>\n<p><span style=\"color: #800000;\">In case the microcontroller does not have the internal pull-up feature on its I\/O port, the pull-up resistors on the input lines must be connected externally.<\/span><\/p>\n<p>I am using my <a href=\"http:\/\/embedded-lab.com\/blog\/?p=2889\" target=\"_blank\">PIC16F1827 development board<\/a> to demonstrate both the techniques of keypad scanning. The setup for this experiment is shown below.<\/p>\n<div id=\"attachment_3446\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadSetUp.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3446\" class=\"size-full wp-image-3446\" title=\"KeyPadSetUp\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadSetUp.jpg\" alt=\"\" width=\"560\" height=\"411\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadSetUp.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadSetUp-550x403.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3446\" class=\"wp-caption-text\">Experimental setup with a keypad interfaced to PORTB of PIC16F1827<\/p><\/div>\n<p><strong> Software<\/strong><\/p>\n<p>I wrote the keypad scanning subroutine in mikroC Pro for PIC compiler, for both the techniques described in the theory section. In order to enable the weak pull-up on PORTB pins, the Weak Pull-up Enable (WPUEN)\u00a0bit of OPTION_REG must be cleared and the\u00a0corresponding bits of Weak Pull-Up on PORTB (WPUB) register must be set. The two programs (source code and HEX files) can be downloaded from the links provided below. Note that the mikroC Pro for PIC compiler also provides built-in library for Matrix Keypad, but that is not used here.<\/p>\n<p><strong><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F1827\/MatrixKeypad1.zip\">mikroC Project files for sequential exploration of rows<\/a><\/strong><\/p>\n<p><strong><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F1827\/MatrixKeypad2.zip\">mikroC Project files for simultaneous exploration of rows and columns<\/a><\/strong><\/p>\n<div id=\"attachment_3449\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadOutput.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3449\" class=\"size-full wp-image-3449\" title=\"KeyPadOutput\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadOutput.jpg\" alt=\"\" width=\"560\" height=\"451\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadOutput.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/08\/KeyPadOutput-550x442.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3449\" class=\"wp-caption-text\">Press any key and it will be displayed on the LCD<\/p><\/div>\n<p><strong>Summary<\/strong><\/p>\n<p>We discussed two basic approaches of keypad scanning and implemented it with a 16-key keypad interfaced to PIC16F1827 microcontroller. Both the techniques have one drawback, which is the microcontroller must\u00a0continuously scan the rows and columns to make sure that it will not miss any key hit. This is a waste of time and CPU cycles, and it could burden the microprocessor&#8217;s resources and consume excessive amounts of power.\u00a0There&#8217;s an alternative approach for overcoming this drawback: Interrupt driven keypad scanning. Using an interrupt frees the microcontroller to perform other tasks or to switch into a power-down mode while awaiting the next key closure. It also reduce electromagnetic interference produced by continuously scanning the rows and columns of the keypad. With PIC microcontroller&#8217;s Interrupt-on-change feature, this is easy to implement. I will describe this technique in another section very soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matrix keypads are very common input devices in embedded systems. They have simple architecture and are easy to interface. One good thing about them is that they allow you to interface a large number of input keys to a microcontroller with minimum usage of \u00a0I\/O resources. This tutorial describes two different approaches of reading input data from a 4&#215;4 (16 keys) matrix keypad interfaced to a PIC microcontroller. The pressed key information is displayed on a character LCD. The microcontroller used in this experiment is PIC16F1827.<\/p>\n","protected":false},"author":1,"featured_media":3439,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,38],"tags":[322,323,285],"class_list":["post-3428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-lessons","category-pic-tutorials","tag-keypad-interface","tag-matrix-keypad","tag-pic16f1827"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=3428"}],"version-history":[{"count":17,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3428\/revisions"}],"predecessor-version":[{"id":3452,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3428\/revisions\/3452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/3439"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=3428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=3428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=3428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}