{"id":1305,"date":"2015-12-08T10:39:01","date_gmt":"2015-12-08T02:39:01","guid":{"rendered":"http:\/\/handsontec.com\/?page_id=1305"},"modified":"2015-12-16T09:02:46","modified_gmt":"2015-12-16T01:02:46","slug":"arduino-4-using-lcdkeypad-shield","status":"publish","type":"page","link":"https:\/\/handsontec.com\/index.php\/arduino-4-using-lcdkeypad-shield\/","title":{"rendered":"Arduino &#8211; 4 &#8211; Using LCD+Keypad Shield"},"content":{"rendered":"<h3 style=\"text-align: justify;\"><span style=\"color: #ff6600;\">How to use 16&#215;2 Character LCD + 6-buttons Keypad Shield ?<\/span><\/h3>\n<p style=\"text-align: justify;\">The LCD-Keypad Shield attaches to your Arduino board to provide a 16-character by 2-line display, white character, blue back light LCD with a keypad consisting of 5 keys &gt; &#8220;select&#8221;, &#8220;up&#8221;, &#8220;right&#8221;, &#8220;down&#8221; and &#8220;left&#8221;. With this shield you will be able to move through menus and make selections straight from one board attached to your Arduino without requiring a massive tower of shields or wiring tangling around.<\/p>\n<p style=\"text-align: justify;\">The LCD-Keypad Shield works perfectly in 4-bit mode with the \u201cLiquidCrystal\u201d library found in the Arduino IDE, using this library will allow you to control the LCD with only 6 digital I\/O lines. This shield provides you with the capability of pushing multiple buttons at once and combining the results. No longer will you be restrained to only 5 inputs, now you have the ability to make use of 32 different button combinations!<\/p>\n<p style=\"text-align: justify;\">This LCD Keypad Shield use total of 6-pins to control the LCD display which is pin-4, 5, 6, 7, 8, 9. For LCD Data, it use pin-4, 5, 6, 7, while for the RS and Enable pin, it use pin-8 and 9. The Arduino-LCD Keypad Shield are only required to plug into the Arduino main board and there was no soldering are required such as shown in figure below.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-10.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1308 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-10.jpg\" alt=\"lcd-keypad-10\" width=\"494\" height=\"327\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-10.jpg 494w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-10-200x132.jpg 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-10-300x199.jpg 300w\" sizes=\"(max-width: 494px) 100vw, 494px\" \/><\/a>Fig-1: Plug the LCD+Keypad Shield directly onto Arduino Board.<\/p>\n<h3 style=\"text-align: justify;\"><span style=\"color: #ff6600;\">Pins Assignment<\/span><\/h3>\n<table style=\"height: 226px;\" width=\"527\">\n<tbody>\n<tr>\n<td style=\"text-align: center;\">Arduino Pin<\/td>\n<td style=\"text-align: left;\">Function<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">A0<\/td>\n<td>Buttons ( Select, Up, Down, Left, Right)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">4<\/td>\n<td>LCD DB4<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">5<\/td>\n<td>LCD DB5<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">6<\/td>\n<td>LCD DB6<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">7<\/td>\n<td>LCD DB7<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">8<\/td>\n<td>LCD Register Select, RS<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">9<\/td>\n<td>LCD Enable, En<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">10<\/td>\n<td>PWM control for Backlight brightness<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1321 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-3.jpg\" alt=\"lcd-keypad-3\" width=\"439\" height=\"378\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-3.jpg 439w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-3-200x172.jpg 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-3-300x258.jpg 300w\" sizes=\"(max-width: 439px) 100vw, 439px\" \/><\/a><\/h3>\n<h3><span id=\"Application_Ideas\" class=\"mw-headline\" style=\"color: #ff6600;\">Application Ideas<\/span><\/h3>\n<p>The below code listing demonstrate the use of this LCD+Keypad shield.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">\/\/==========================================================================\r\n\/\/  Author      : Handson Technology\r\n\/\/  Project     : Arduino Uno\r\n\/\/  Description : Display \"Hello World\" and 6-keys press\r\n\/\/==========================================================================\r\n\r\n\/\/Include the LiquidCrystal header file which is inside the Arduino IDE\r\n#include &lt;LiquidCrystal.h&gt; \r\n\r\n\/* ==== PINS ASSIGNMNET ==========\r\n * LCD RS pin to digital pin 8\r\n * LCD EN pin to digital pin 9\r\n * LCD D4 pin to digital pin 4\r\n * LCD D5 pin to digital pin 5\r\n * LCD D6 pin to digital pin 6\r\n * LCD D7 pin to digital pin 7\r\n * Backlight PWM control to Pin 10\r\n * LCD R\/W pin to ground\r\n*\/\r\n\r\n\/\/ Set the I\/O pin for LCD 4-bit mode following the library assignment: \r\n\/\/  LiquidCrystal(rs, en, d4, d5, d6, d7).\r\nLiquidCrystal lcd(8, 9, 4, 5, 6, 7);\r\n\r\nint analogPin = A0;  \/\/Define the A0 as analogPin as integer type.\r\nint adc_key_old;\r\nint adc_key_in;\r\nint NUM_KEYS = 5;\r\nint key=-1;\r\nint adc_key_val[5] ={30, 150, 360, 535, 760 }; \/\/Define the value at A0 pin \r\n                                               \/\/ when a key is pressed.\r\n\r\n\/\/ Define each key as character string type for keypress display.               \r\nchar msgs[5][15] = {\"Right Key OK \", \r\n                    \"Up Key OK    \", \r\n                    \"Down Key OK  \", \r\n                    \"Left Key OK  \", \r\n                    \"Select Key OK\"};\r\n                    \r\n                    \r\n\/*******************************************************************************\r\n* PRIVATE FUNCTION: setup()\r\n* PARAMETERS:void\r\n* RETURN:void\r\n* DESCRIPTIONS:\r\n* Define of I\/O pin as Input or Output \r\n*\r\n*******************************************************************************\/\r\n\/\/ The setup() method runs once, when the sketch starts\r\nvoid setup ()\r\n{\r\n  lcd.begin(16, 2);            \/\/ set the lcd type: 16-character by 2-lines\r\n  lcd.clear();                        \/\/ LCD screen clear\r\n  lcd.print(\"  HandsOn Tech  \");      \/\/ Send the ASCII code to the LCD for \r\n                                      \/\/ displaying the message\r\n\r\n  pinMode(10, OUTPUT);                \/\/ sets backlight pin-10 as PWM output\r\n  analogWrite(10, 125);               \/\/ Set backlight to 50% brightness \r\n    \r\n  lcd.setCursor(0,1);           \/\/ set the position of next message string: \r\n                                \/\/ 1st position at 2nd line\r\n  lcd.print(\"LCD-Keypad Sh\");    \r\n  delay(5000);                        \/\/ delay for 3000ms\r\n  lcd.clear();\r\n  lcd.setCursor(0,0);              \r\n  lcd.print(\"  HandsOn Tech  \");\r\n  lcd.setCursor(0,1);\r\n  lcd.print(\"Pls press any\"); \r\n  adc_key_old = analogRead(analogPin);\/\/ store the unpress key value \r\n}\r\n\r\n\r\n\/*******************************************************************************\r\n* PRIVATE FUNCTION: loop()\r\n* PARAMETERS: void\r\n* RETURN: void\r\n* DESCRIPTIONS:\r\n* Non-Stop looping \r\n*******************************************************************************\/\r\nvoid loop()\r\n{\r\n  adc_key_in = analogRead(analogPin);  \/\/ Read the value at analogPin A0 and store \r\n                                       \/\/ the value in the adc_key_in register\r\n  adc_key_in = get_key(adc_key_in);    \/\/ Send the adc_key_in value to\r\n                                       \/\/ get_key() subroutine.\r\n  lcd.setCursor(0, 1);                 \r\n  lcd.print(msgs[adc_key_in]);         \/\/ Display message with msgs[] \r\n                                       \/\/ according to adc_key_in value\r\n}\r\n  \r\n\r\n\/*******************************************************************************\r\n* PRIVATE FUNCTION: get_key\r\n* PARAMETERS: integer\r\n* RETURN:unsigned int input\r\n* DESCRIPTIONS:\r\n* convert the ADC value to number between 0 to 4\r\n*******************************************************************************\/\r\nint get_key(unsigned int input)\r\n{\r\n  int k;\r\n    \r\n  for (k = 0; k &lt; NUM_KEYS; k++)\r\n  {\r\n    if (input &lt; adc_key_val[k])\r\n    {\r\n           \r\n    return k;\r\n        }\r\n  }\r\n    \r\n    if (k &gt;= NUM_KEYS)\r\n        k = -1;     \/\/ No valid key pressed\r\n    \r\n    return k;\r\n}<\/pre>\n<p style=\"text-align: justify;\">The code is self explanatory with the detail comments. Compile and upload the code into Arduino board and watch the result.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-13.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1325 aligncenter\" src=\"http:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-13.jpg\" alt=\"lcd-keypad-13\" width=\"376\" height=\"245\" srcset=\"https:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-13.jpg 376w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-13-200x130.jpg 200w, https:\/\/handsontec.com\/wp-content\/uploads\/2015\/12\/lcd-keypad-13-300x195.jpg 300w\" sizes=\"(max-width: 376px) 100vw, 376px\" \/><\/a>Once the code is successfully compiled and upload into Arduino board, you should see the display as above shown.<\/p>\n<h3><span style=\"color: #ff6600;\">Parts Used in this Tutorial<\/span><\/h3>\n<ul>\n<li><a href=\"http:\/\/handsontec.com\/index.php\/product\/lcd-keypad-shield\/\" target=\"_blank\">LCD+Keypad Shield<\/a><\/li>\n<li><a href=\"http:\/\/handsontec.com\/index.php\/product\/arduino-uno\/\" target=\"_blank\">Arduino Uno<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #ff6600;\">Documents<\/span><\/h3>\n<ul>\n<li><a href=\"http:\/\/www.handsontec.com\/pdf_learn\/Learn_LCD_Arduino-V1-B.pdf\" target=\"_blank\">Complete Guide to Arduino LCD Interfacing (PDF)<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><\/h3>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to use 16&#215;2 Character LCD + 6-buttons Keypad Shield ? The LCD-Keypad Shield attaches to your Arduino board to provide a 16-character by 2-line display, white character, blue back light LCD with a keypad consisting of 5 keys &gt; &#8220;select&#8221;, &#8220;up&#8221;, &#8220;right&#8221;, &#8220;down&#8221; and &#8220;left&#8221;. With this shield you will be able to move [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1304,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/pages\/1305"}],"collection":[{"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/comments?post=1305"}],"version-history":[{"count":17,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/pages\/1305\/revisions"}],"predecessor-version":[{"id":1344,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/pages\/1305\/revisions\/1344"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/media\/1304"}],"wp:attachment":[{"href":"https:\/\/handsontec.com\/index.php\/wp-json\/wp\/v2\/media?parent=1305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}