{"id":760,"date":"2020-10-05T20:55:50","date_gmt":"2020-10-05T13:55:50","guid":{"rendered":"https:\/\/www.makeriot2020.com\/?p=760"},"modified":"2020-10-05T20:55:55","modified_gmt":"2020-10-05T13:55:55","slug":"using-i2c-with-a-4x4-matrix-keypad","status":"publish","type":"post","link":"https:\/\/www.makeriot2020.com\/index.php\/2020\/10\/05\/using-i2c-with-a-4x4-matrix-keypad\/","title":{"rendered":"Using I2C with a 4&#215;4 Matrix Keypad"},"content":{"rendered":"\n<p>Using a matrix keypad is a very easy way to add multiple control buttons to a project, be it to enter a password, or to control different devices. These keypads do unfortunately have some serious flaws (in my view anyway)<br><br>1) They are usually of extremely low quality ( especially some of the membrane types from China). This means they dont last very long.<br>2) A typical 4&#215;4 Matrix keypad will require 8 of your precious IO pins for itself.<br><br>These two flaws can however easily be solved, if we use a bit of technology, and are willing to to a bit of simple circuit construction by ourselves. <br><br>What does this mean ? Most of us makers will inevitably have a piece of proto-board or strip-board lying around, as well as a few momentary push-button switches. These can easily be used to make out own, much more reliable keypad. Let us look at the circuit<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"927\" data-src=\"http:\/\/144.126.248.244\/wp-content\/uploads\/2020\/10\/Schematic_4x4-Matrix-Keypad_2020-10-05_19-36-15-1024x927.png\" alt=\"\" class=\"wp-image-761 lazyload\" data-srcset=\"https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/Schematic_4x4-Matrix-Keypad_2020-10-05_19-36-15-1024x927.png 1024w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/Schematic_4x4-Matrix-Keypad_2020-10-05_19-36-15-600x543.png 600w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/Schematic_4x4-Matrix-Keypad_2020-10-05_19-36-15-300x271.png 300w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/Schematic_4x4-Matrix-Keypad_2020-10-05_19-36-15-768x695.png 768w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/Schematic_4x4-Matrix-Keypad_2020-10-05_19-36-15.png 1041w\" data-sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/927;\" \/><figcaption>Circuit diagram for a 4&#215;4 Matrix Keypad<\/figcaption><\/figure>\n\n\n\n<p>As we can see, to build a 4&#215;4 matrix keypad, we will need 16 momentary switches. These are connected together as shown above. You can then interface it with your favourite micro-controller to read the key(s) pressed&#8230; <br><br>This definitely solves the first of my problems, but we still need 8 pins to control this keypad&#8230; or do we? No, we don&#8217;t, we need only 2 pins. That is to say if we use one of those PCF8574 I2C IO port expander modules. They are much more reliable, as well as quite cheap as well. all depending on where you buy them from, and how long you are willing to wait for shipping \ud83d\ude42<\/p>\n\n\n\n<p>Let us see how to connect the keypad to the I2C Module<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"768\" height=\"1024\" data-src=\"http:\/\/144.126.248.244\/wp-content\/uploads\/2020\/10\/20201001_155218-768x1024.jpg\" alt=\"\" class=\"wp-image-762 lazyload\" data-srcset=\"https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155218-768x1024.jpg 768w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155218-scaled-600x800.jpg 600w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155218-225x300.jpg 225w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155218-1152x1536.jpg 1152w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155218-1536x2048.jpg 1536w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155218-scaled.jpg 1920w\" data-sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 768px; --smush-placeholder-aspect-ratio: 768\/1024;\" \/><figcaption>a 4&#215;4 Membrane Matrix Keypad with PCF8574 I2C port expander module<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"768\" height=\"1024\" data-src=\"http:\/\/144.126.248.244\/wp-content\/uploads\/2020\/10\/20201001_155323-768x1024.jpg\" alt=\"\" class=\"wp-image-763 lazyload\" data-srcset=\"https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155323-768x1024.jpg 768w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155323-scaled-600x800.jpg 600w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155323-225x300.jpg 225w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155323-1152x1536.jpg 1152w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155323-1536x2048.jpg 1536w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155323-scaled.jpg 1920w\" data-sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 768px; --smush-placeholder-aspect-ratio: 768\/1024;\" \/><figcaption>Connecting the two together, note that we do not connect the INT pin<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"768\" height=\"1024\" data-src=\"http:\/\/144.126.248.244\/wp-content\/uploads\/2020\/10\/20201001_155454-768x1024.jpg\" alt=\"\" class=\"wp-image-764 lazyload\" data-srcset=\"https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155454-768x1024.jpg 768w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155454-scaled-600x800.jpg 600w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155454-225x300.jpg 225w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155454-1152x1536.jpg 1152w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155454-1536x2048.jpg 1536w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155454-scaled.jpg 1920w\" data-sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 768px; --smush-placeholder-aspect-ratio: 768\/1024;\" \/><figcaption>Connect Power (VCC, GND and I2C lines <\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"768\" height=\"1024\" data-src=\"http:\/\/144.126.248.244\/wp-content\/uploads\/2020\/10\/20201001_155507-768x1024.jpg\" alt=\"\" class=\"wp-image-765 lazyload\" data-srcset=\"https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155507-768x1024.jpg 768w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155507-scaled-600x800.jpg 600w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155507-225x300.jpg 225w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155507-1152x1536.jpg 1152w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155507-1536x2048.jpg 1536w, https:\/\/www.makeriot2020.com\/wp-content\/uploads\/2020\/10\/20201001_155507-scaled.jpg 1920w\" data-sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 768px; --smush-placeholder-aspect-ratio: 768\/1024;\" \/><figcaption>Connect to Arduino or your preferred microcontroller. We have used Arduino Uno, Note that you can also connect the I2C to A4 (SDA) and A5(SCL) if you prefer. <\/figcaption><\/figure>\n\n\n\n<p>Now, we need to install some libraries<\/p>\n\n\n\n<p>The first one is the actual Keypad library, you can download it from the link below<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"http:\/\/144.126.248.244\/wp-content\/uploads\/2020\/10\/Keypad-master.zip\">Keypad-master<\/a><a href=\"http:\/\/144.126.248.244\/wp-content\/uploads\/2020\/10\/Keypad-master.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p>The second library that we will need, is the keypad_i2c library, once again, download it from the link below.<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"http:\/\/144.126.248.244\/wp-content\/uploads\/2020\/10\/Keypad_I2C.zip\">Keypad_I2C<\/a><a href=\"http:\/\/144.126.248.244\/wp-content\/uploads\/2020\/10\/Keypad_I2C.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Coding the keypad<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>\n\n#include &lt;Key.h>\n#include &lt;Keypad.h>\n#include &lt;Keypad_I2C.h>\n\n#define I2CADDR 0x26 \/\/ Set the Address of the PCF8574\n\nconst byte ROWS = 4; \/\/ Set the number of Rows\nconst byte COLS = 4; \/\/ Set the number of Columns\n\n\/\/ Set the Key at Use (4x4)\nchar keys &#91;ROWS] &#91;COLS] = {\n  {'1', '2', '3', 'A'},\n  {'4', '5', '6', 'B'},\n  {'7', '8', '9', 'C'},\n  {'*', '0', '#', 'D'}\n};\n\n\/\/ define active Pin (4x4)\nbyte rowPins &#91;ROWS] = {0, 1, 2, 3}; \/\/ Connect to Keyboard Row Pin\nbyte colPins &#91;COLS] = {4, 5, 6, 7}; \/\/ Connect to Pin column of keypad.\n\n\/\/ makeKeymap (keys): Define Keymap\n\/\/ rowPins:Set Pin to Keyboard Row\n\/\/ colPins: Set Pin Column of Keypad\n\/\/ ROWS: Set Number of Rows.\n\/\/ COLS: Set the number of Columns\n\/\/ I2CADDR: Set the Address for i2C\n\/\/ PCF8574: Set the number IC\nKeypad_I2C keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574);\n\nvoid setup () {\n  Wire .begin (); \/\/ Call the connection Wire\n  keypad.begin (makeKeymap (keys)); \/\/ Call the connection\n  Serial.begin (9600);\n\n}\nvoid loop () {\n \n  char key = keypad.getKey (); \/\/ Create a variable named key of type char to hold the characters pressed\n \n  if (key) {\/\/ if the key variable contains\n    Serial.println (key); \/\/ output characters from Serial Monitor\n  }\n}<\/code><\/pre>\n\n\n\n<p>Upload this to your Arduino device and enjoy. This sketch can also be adapted for 1&#215;4, and 4&#215;3 keypads, and with a little modification, will also work perfectly on ESP32 or ESP8266 as well&#8230;<\/p>\n\n\n\n<p><\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Using a matrix keypad is a very easy way to add multiple control buttons to a project, be it to enter a password, or to control different devices. These keypads do unfortunately have some serious flaws (in my view anyway) 1) They are usually of extremely low quality ( especially some of the membrane types &hellip; <a href=\"https:\/\/www.makeriot2020.com\/index.php\/2020\/10\/05\/using-i2c-with-a-4x4-matrix-keypad\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using I2C with a 4&#215;4 Matrix Keypad&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":763,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,40,51,50,83],"tags":[6,42,81,67,84,85],"class_list":["post-760","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-esp32","category-i2c-devices","category-io-extender-outputs","category-matrix-keypad","tag-arduino","tag-esp32","tag-esp8266","tag-i2c","tag-matrix-keypad","tag-pcf8574"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts\/760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/comments?post=760"}],"version-history":[{"count":0,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts\/760\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/media\/763"}],"wp:attachment":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/media?parent=760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/categories?post=760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/tags?post=760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}