{"id":250,"date":"2010-04-03T13:29:54","date_gmt":"2010-04-03T18:29:54","guid":{"rendered":"http:\/\/nootropicdesign.com\/projectlab\/?p=250"},"modified":"2018-11-02T09:20:50","modified_gmt":"2018-11-02T14:20:50","slug":"keypad-tutorial","status":"publish","type":"post","link":"https:\/\/nootropicdesign.com\/projectlab\/2010\/04\/03\/keypad-tutorial\/","title":{"rendered":"Tutorial: Reading a 12-Button Keypad"},"content":{"rendered":"<p><strong><em>Difficulty Level = 1<\/em><\/strong>  <a href=\"\/projectlab\/difficulty-levels\/\">[What&#8217;s this?]<\/a><\/p>\n<p>Most keypads like this are wired so it makes it straightforward to figure out what button is being pressed.  With 3 columns and 4 rows of buttons, you only need 7 wires.  Typically all the buttons in a column are connected together with the same wire, and all the buttons in a row are connected together with the same wire.  To determine which button is pressed, you apply a voltage to the wire attached to a column and then check the wires attached to each row to see if current is flowing through any of them.  If so, then the switch for a particular button is closed (button pressed).  Then you proceed to the next column and try each row again, etc.  Not rocket science &#8212; just scanning a bunch of switches to see which one is closed.  In fact, there is a <a href=\"http:\/\/www.arduino.cc\/playground\/Code\/Keypad\">keypad library in the Arduino Playground<\/a> that makes it easy to do this.<\/p>\n<p><a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/keypad.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/keypad.jpg\" alt=\"\" title=\"keypad\" width=\"480\" height=\"640\" class=\"alignnone size-full wp-image-255\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/keypad.jpg 480w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/keypad-225x300.jpg 225w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><\/p>\n<p>Well, <a href=\"https:\/\/www.jameco.com\/webapp\/wcs\/stores\/servlet\/ProductDisplay?productId=2081828\">the keypad I bought here<\/a> has 10 wires instead of 7, and it&#8217;s wired in a really goofy way.  I&#8217;m not sure if this is common, but I thought keypads were generally wired as described above.  Here&#8217;s the schematic that shows how this one is actually wired:<br \/>\n<div id=\"attachment_249\" style=\"width: 650px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/keypadSchematic.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-249\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/keypadSchematic.png\" alt=\"\" title=\"keypadSchematic\" width=\"640\" height=\"480\" class=\"size-full wp-image-249\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/keypadSchematic.png 640w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/keypadSchematic-300x225.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-249\" class=\"wp-caption-text\">Schematic of my non-standard keypad<\/p><\/div><\/p>\n<p\/>\n<p\/>\nNotice that the gray wire is used only for the 9 key.  And the orange wire is only used for the * key.  And the brown wire is only used for the # key.  Why is this built so inefficiently?  I have no idea!<\/p>\n<p>Regardless, here is Arduino code that I used to scan this keypad.  You can do something similar if you have a keypad that is not wired in a straightforward way.<\/p>\n<pre class=\"codeblockscroll\">\r\n\r\n\/\/ Pins\r\n#define BLACK 2\r\n#define WHITE 3\r\n#define GRAY 4\r\n#define PURPLE 5\r\n#define BLUE 6\r\n#define GREEN 7\r\n#define YELLOW 8\r\n#define ORANGE 9\r\n#define RED 10\r\n#define BROWN 11\r\n\r\n#define STAR 10\r\n#define POUND 11\r\n\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n\r\n  \/\/ Rows\r\n  pinMode(BLACK, INPUT);\r\n  digitalWrite(BLACK, HIGH);  \/\/ set pull-up resistors for all inputs\r\n\r\n  pinMode(WHITE, INPUT);\r\n  digitalWrite(WHITE, HIGH);\r\n\r\n  pinMode(GRAY, INPUT);\r\n  digitalWrite(GRAY, HIGH);\r\n\r\n  pinMode(PURPLE, INPUT);\r\n  digitalWrite(PURPLE, HIGH);\r\n\r\n  pinMode(ORANGE, INPUT);\r\n  digitalWrite(ORANGE, HIGH);\r\n\r\n  pinMode(BROWN, INPUT);\r\n  digitalWrite(BROWN, HIGH);\r\n\r\n\r\n  \/\/ Columns\r\n  pinMode(BLUE, OUTPUT);\r\n  digitalWrite(BLUE, HIGH);\r\n\r\n  pinMode(GREEN, OUTPUT);\r\n  digitalWrite(GREEN, HIGH);\r\n\r\n  pinMode(YELLOW, OUTPUT);\r\n  digitalWrite(YELLOW, HIGH);\r\n\r\n  pinMode(RED, OUTPUT);\r\n  digitalWrite(RED, HIGH);\r\n\r\n}\r\n\r\nvoid loop() {\r\n  int key = scanKeypad();\r\n\r\n  if (key != -1) {\r\n    if (key == STAR) {\r\n      Serial.println(\"*\");\r\n    } else {\r\n      if (key == POUND) {\r\n        Serial.println(\"#\");\r\n      } else {\r\n          Serial.println(key);\r\n      }\r\n    }\r\n  }\r\n}\r\n\r\nint scanKeypad() {\r\n  int key = -1;\r\n\r\n  \/\/ Pull the first column low, then check each of the rows to see if a\r\n  \/\/ button is pressed.\r\n  digitalWrite(BLUE, LOW);\r\n  if (digitalRead(BLACK) == LOW) {\r\n    key = 1;\r\n  }\r\n  if (digitalRead(WHITE) == LOW) {\r\n    key = 4;\r\n  }\r\n  if (digitalRead(PURPLE) == LOW) {\r\n    key = 7;\r\n  }\r\n  digitalWrite(BLUE, HIGH);\r\n\r\n  \/\/ Moving on to the second column....\r\n  digitalWrite(GREEN, LOW);\r\n  if (digitalRead(BLACK) == LOW) {\r\n    key = 2;\r\n  }\r\n  if (digitalRead(WHITE) == LOW) {\r\n    key = 5;\r\n  }\r\n  if (digitalRead(PURPLE) == LOW) {\r\n    key = 8;\r\n  }\r\n  digitalWrite(GREEN, HIGH);\r\n\r\n  \/\/ Third \"column\".  Note that the 0 key is wired to this column even though\r\n  \/\/ the 0 is really in the second column.\r\n  digitalWrite(YELLOW, LOW);\r\n  if (digitalRead(BLACK) == LOW) {\r\n    key = 3;\r\n  }\r\n  if (digitalRead(WHITE) == LOW) {\r\n    key = 6;\r\n  }\r\n  if (digitalRead(GRAY) == LOW) {\r\n    key = 9;\r\n  }\r\n  if (digitalRead(PURPLE) == LOW) {\r\n    key = 0;\r\n  }\r\n  digitalWrite(YELLOW, HIGH);\r\n\r\n  \/\/ Last \"column\" is not really it's own column.  Only wired to * and #\r\n  digitalWrite(RED, LOW);\r\n  if (digitalRead(ORANGE) == LOW) {\r\n    key = STAR;\r\n  }\r\n  if (digitalRead(BROWN) == LOW) {\r\n    key = POUND;\r\n  }\r\n  digitalWrite(RED, HIGH);\r\n\r\n  return key;\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Difficulty Level = 1 [What&#8217;s this?] Most keypads like this are wired so it makes it straightforward to figure out what button is being pressed. With 3 columns and 4 rows of buttons, you only need 7 wires. Typically all the buttons in a column are connected together with the same wire, and all the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-250","post","type-post","status-publish","format-standard","hentry","category-arduino"],"_links":{"self":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":14,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":1973,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts\/250\/revisions\/1973"}],"wp:attachment":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}