Using I2C with a 4×4 Matrix Keypad

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 from China). This means they dont last very long.
2) A typical 4×4 Matrix keypad will require 8 of your precious IO pins for itself.

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.

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

Circuit diagram for a 4×4 Matrix Keypad

As we can see, to build a 4×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…

This definitely solves the first of my problems, but we still need 8 pins to control this keypad… or do we? No, we don’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 🙂

Let us see how to connect the keypad to the I2C Module

a 4×4 Membrane Matrix Keypad with PCF8574 I2C port expander module
Connecting the two together, note that we do not connect the INT pin
Connect Power (VCC, GND and I2C lines
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.

Now, we need to install some libraries

The first one is the actual Keypad library, you can download it from the link below

The second library that we will need, is the keypad_i2c library, once again, download it from the link below.

Coding the keypad



#include <Key.h>
#include <Keypad.h>
#include <Keypad_I2C.h>

#define I2CADDR 0x26 // Set the Address of the PCF8574

const byte ROWS = 4; // Set the number of Rows
const byte COLS = 4; // Set the number of Columns

// Set the Key at Use (4x4)
char keys [ROWS] [COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// define active Pin (4x4)
byte rowPins [ROWS] = {0, 1, 2, 3}; // Connect to Keyboard Row Pin
byte colPins [COLS] = {4, 5, 6, 7}; // Connect to Pin column of keypad.

// makeKeymap (keys): Define Keymap
// rowPins:Set Pin to Keyboard Row
// colPins: Set Pin Column of Keypad
// ROWS: Set Number of Rows.
// COLS: Set the number of Columns
// I2CADDR: Set the Address for i2C
// PCF8574: Set the number IC
Keypad_I2C keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574);

void setup () {
  Wire .begin (); // Call the connection Wire
  keypad.begin (makeKeymap (keys)); // Call the connection
  Serial.begin (9600);

}
void loop () {
 
  char key = keypad.getKey (); // Create a variable named key of type char to hold the characters pressed
 
  if (key) {// if the key variable contains
    Serial.println (key); // output characters from Serial Monitor
  }
}

Upload this to your Arduino device and enjoy. This sketch can also be adapted for 1×4, and 4×3 keypads, and with a little modification, will also work perfectly on ESP32 or ESP8266 as well…

31 thoughts on “Using I2C with a 4×4 Matrix Keypad”

  1. Hi, thank you for this!
    Just have one question. Does this work for multiple inputs simultaneously? (I’ve not tried this)

  2. DevorahDeschamps7623@hotmail.com

    Wonderful web site. Plenty of useful info here. I am sending it to several buddies ans also sharing in delicious. And of course, thanks to your effort!

  3. kristenlittleton@gmx.net

    If some one wishes to be updated with latest technologies
    therefore he must be go to see this web site and be up to date every day.

  4. DarylPiefer22564@hotmail.com

    We’re a group of volunteers and opening a new scheme in our community. Your site provided us with valuable info to work on. You’ve done a formidable job and our whole community will be grateful to you.

  5. JaimeMatey39714@hotmail.com

    That is the best blog for anybody who desires to seek out out about this topic. You understand so much its nearly laborious to argue with you (not that I actually would want匟aHa). You positively put a brand new spin on a subject thats been written about for years. Great stuff, just great!

  6. FrancescoStammler84191@hotmail.com

    Spot on with this write-up, I truly suppose this website needs way more consideration. I抣l in all probability be once more to read rather more, thanks for that info.

  7. GinetteDebiasi80029@hotmail.com

    Hi there! This is my first comment here so I just wanted to give a quick shout out and say I truly enjoy reading through your articles. Can you recommend any other blogs/websites/forums that cover the same topics? Thanks a lot!

  8. DannieCockburn30007@hotmail.com

    Hmm it looks like your website ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to the whole thing. Do you have any recommendations for inexperienced blog writers? I’d genuinely appreciate it.

  9. EdmundKoziel23887@hotmail.com

    hello!,I like your writing very much! share we communicate more about your post on AOL? I require a specialist on this area to solve my problem. Maybe that’s you! Looking forward to see you.

  10. I was curious if you ever thought of changing the structure of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it
    better. Youve got an awful lot of text for only having 1 or 2 pictures.

    Maybe you could space it out better?

    1. Constructive critisism… I like it… can you tell me a bit more, and how you would do it? I would love to know your thoughts, as I do want to connect with people better… The post you are looking at is also very old… do the newer posts also suffer from the same problem?
      Please get back to me.. and let’s see if I can do something to follow your advice…

  11. KristoferFlores12278@hotmail.com

    I really appreciate this post. I抳e been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thank you again

  12. ConradDetwiler6743@hotmail.com

    Thanks for some other magnificent post. Where else may just anyone get that type of information in such a perfect means of writing? I have a presentation next week, and I am at the look for such information.

  13. MaryamGranato24612@hotmail.com

    Great website. Plenty of helpful info here. I am sending it to several friends ans additionally sharing in delicious. And naturally, thank you in your sweat!

  14. KieshaVotraw86305@hotmail.com

    Hi I am so delighted I found your website, I really found you by accident, while I was looking on Bing for something else, Regardless I am here now and would just like to say cheers for a fantastic post and a all round enjoyable blog (I also love the theme/design), I don抰 have time to look over it all at the moment but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the superb job.

  15. AntoineRosell40108@hotmail.com

    You made some good points there. I did a search on the theme and found nearly all people will have the same opinion with your blog.

  16. ChungBrisson@hotmail.com

    I’m really enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Fantastic work!

  17. HaiPhilliber@hotmail.com

    Great post. I was checking continuously this blog and I’m impressed! Extremely useful information specially the last part 🙂 I care for such info much. I was seeking this particular information for a long time. Thank you and good luck.

  18. PorfirioLukens@hotmail.com

    I like the helpful information you provide for your articles. I will bookmark your blog and check once more right here frequently. I’m slightly certain I抣l be told many new stuff right here! Best of luck for the next!

  19. MichaleSpace@hotmail.com

    Hello there, You have performed an incredible job. I抣l definitely digg it and in my opinion suggest to my friends. I’m confident they will be benefited from this site.

  20. DovieMutart@hotmail.com

    I like the valuable information you provide in your articles. I will bookmark your blog and check again here frequently. I’m quite certain I will learn many new stuff right here! Good luck for the next!

  21. ArianneQualia@hotmail.com

    I discovered your blog website on google and check a couple of of your early posts. Proceed to keep up the superb operate. I simply extra up your RSS feed to my MSN Information Reader. Searching for forward to studying more from you in a while!?

  22. MohammadHubel@hotmail.com

    I’m extremely inspired together with your writing abilities and also with the structure to your blog. Is this a paid theme or did you modify it yourself? Either way keep up the excellent high quality writing, it is uncommon to see a nice blog like this one these days..

Leave a Reply

Your email address will not be published. Required fields are marked *