keypad with lcd display free sample

We have specific tutorials about keypad and LCD. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, ESP32 code... Learn more about them at the following links:

keypad with lcd display free sample

Displaying information in electronic projects has always been the most compelling issue. There are various ways to display data. These screens can be so simple such as 7segments or LEDs, or they can be more fascinating such as LCDs. Using LCDs has always been one of the most popular ways to display information. LCDs are divided into two generic types: Characters and Graphics.

One of the most common, cheapest and simplest LCDs available is the character LCD. This LCD consists of several rows and columns. Letters and numbers are written in places created by rows and columns. For example, LCD character 16*2 has 2 rows and 16 columns. So it can display 32 characters.

Working with these LCDs is very simple and they have full compatibility with all microcontrollers and processor boards. For easier use of these LCDs, its 16x2model, including four keys for making the menu, is made as a Shield which is also compatible with Arduino boards.

/*Arduino 2x16 LCD - Detect Buttonsmodified on 18 Feb 2019by Saeed Hosseini @ Electropeakhttps://electropeak.com/learn/*/#include //LCD pin to Arduinoconst int pin_RS = 8;const int pin_EN = 9;const int pin_d4 = 4;const int pin_d5 = 5;const int pin_d6 = 6;const int pin_d7 = 7;const int pin_BL = 10;LiquidCrystal lcd( pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);void setup() {lcd.begin(16, 2);lcd.setCursor(0,0);lcd.print("Electropeak.com");lcd.setCursor(0,1);lcd.print("Press Key:");}void loop() {int x;x = analogRead (0);lcd.setCursor(10,1);if (x < 60) {lcd.print ("Right ");}else if (x < 200) {lcd.print ("Up ");}else if (x < 400){lcd.print ("Down ");}else if (x < 600){lcd.print ("Left ");}else if (x < 800){LCD.print ("Select");}}

Initial configuration of the LCD by specifying the number of columns and rows. The first argument is the number of columns, and the second is the number of rows.

We can do it easily using the above functions./*Arduino 2x16 LCD - LCD Scrollmodified on 18 Feb 2019by Saeed Hosseinihttps://electropeak.com/learn/*/#includeconst int RS = 8;const int EN = 9;const int d4 = 4;const int d5 = 5;const int d6 = 6;const int d7 = 7;const int pin_BL = 10; // arduino pin wired to LCD backlight circuitLiquidCrystal lcd( RS, EN, d4, d5, d6, d7);void setup() {lcd.begin(16, 2);lcd.print("Electropeak");delay(1000);}void loop() {// scroll 11 positions ("Electropeak" length) to the leftfor (int positionCounter = 0; positionCounter < 11; positionCounter++) {lcd.scrollDisplayLeft();delay(400); //Scrolling speed}// scroll 27 positions ("Electropeak" length + display length) to the rightfor (int positionCounter = 0; positionCounter < 27; positionCounter++) {lcd.scrollDisplayRight();delay(400);}// scroll 16 positions (display length) to the leftfor (int positionCounter = 0; positionCounter < 16; positionCounter++) {lcd.scrollDisplayLeft();delay(50);}delay(1000);}

You can create a character in each block from your LCD. To do this, you should convert your desired character to an array of codes, then display it on LCD. To convert your character to codes you can use online websites likethis.Design your character, then copy the generated array to your code.

/*Arduino 2x16 LCD - LCD Special Charmodified on 18 Feb 2019by Saeed Hosseinihttps://electropeak.com/learn/*/#includeconst int RS = 8;const int EN = 9;const int d4 = 4;const int d5 = 5;const int d6 = 6;const int d7 = 7;const int pin_BL = 10; // arduino pin wired to LCD backlight circuitLiquidCrystal lcd( RS, EN, d4, d5, d6, d7);// smily facebyte smiley[8] = {B00000,B10001,B00000,B00000,B10001,B01110,B00000,};// Battery signbyte battery[] = {B01110,B01010,B11011,B10001,B11111,B11111,B11111,B11111};// arrow rightbyte R_arrow[8] = {B00000,B00100,B00010,B11111,B00010,B00100,B00000,B00000};// arrow leftbyte L_arrow[8] = {B00000,B00100,B01000,B11111,B01000,B00100,B00000,B00000};// Ohm signbyte ohm[8] = {B00000,B01110,B10001,B10001,B10001,B01010,B11011,B00000};// Heartbyte heart[8] = {B00000,B01010,B10101,B10001,B10001,B01010,B00100,B00000};int i = 0;void setup() {lcd.begin(16, 2);lcd.createChar(0, smiley);lcd.createChar(1, battery);lcd.createChar(2, R_arrow);lcd.createChar(3, L_arrow);lcd.createChar(4, ohm);lcd.createChar(5, heart);lcd.createChar(6, ohm);for (int n = 0; n < 6; n++){lcd.setCursor(n * 2, 0);lcd.write(n);}}void loop() {}

keypad with lcd display free sample

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 > “select”, “up”, “right”, “down” and “left”. 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.

The LCD-Keypad Shield works perfectly in 4-bit mode with the “LiquidCrystal” 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!

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.

keypad with lcd display free sample

The 16x2 LCD And Keypad Shield is very simple to use because it"s fully compatible with the Arduino "LiquidCrystal" library. You can initialise the LCD and display messages on it with just a few lines of code, but it also gives you the flexibility to do more advanced projects such as display menu items and select them using the buttons.

The LCD & Keypad Shield requires a good 5V power supply to ensure the backlight fully illuminates and the display contrast is high, and if you power your Arduino from USB with the LCD Shield attached you may experience a voltage drop over the USB cable. If you have trouble with display contrast or backlight brightness, try attaching a power supply of around 7 to 9Vdc to the 2.1mm DC jack on the Arduino. A typical symptom in an undervoltage situation is that one line of the LCD will show pale rectangles in place of the characters, and the other line will show nothing at all. The Arduino may even continue running normally because it"s quite happy at just 4V or so, but the LCD & Keypad Shield won"t function.

All the hard work of interfacing with the LCD Shield is handled by the LiquidCrystal library, which is included as part of the official Arduino distribution. You can check whether you have it installed by starting up the IDE and looking under Files -> Examples -> LiquidCrystal. If it exists, you"re good to go.

The LCD Shield includes 5 buttons designed for use as navigational or control input. The buttons are arranged in a handy pattern and referred to as UP, DOWN, LEFT, RIGHT, and SELECT, but of course it"s totally up to your sketch to decide what to do when any particular button is pressed.

Now consider what happens if the "DOWN" button is pressed. Now A0 is being presented with a voltage that is divided between the 2K resistor that is trying to pull it up to 5V, and the 330R and 620R resistors in series (totaling 950R) that are trying to pull it down to 0V. The voltage presented to A0 in that case is about 1.61V, which means if you perform an analogRead() on A0 it will return a value of about 329. So if you read a value of about 329 from A0 you know the "DOWN" button is being pressed.

The extensive example below combines a number of techniques to demonstrate how to show messages on the LCD, read from the buttons, and change the display message depending on which buttons are pressed.

keypad with lcd display free sample

In this one we’ll use it to connect a Keypad to an Arduino and again save some pins, and also have a quick overview on what and how the i2c protocol works.

The main difference between those two modules, is their pinouts, the LCD Backpack pinout is made to fit on an LCD with additional outputs for the backlight.

Since both the I2C port Expander and the I2C LCD Backpack are pretty much the same, couldn’t I just use the LCD Backpack to connect the Bourns encoder?

But if your project can make due with only 7 I/O pins then you can use the LCD Backpack as an I2C Expander since those other pins are properly connected.

So instead of using 7 Pins on the Arduino, were’s using the I2C protocol and using only 2 Pins to read the Keypad as well as display the results on the LCD screen.

keypad with lcd display free sample

You may have used an electronic device with a small Liquid Crystal Display (LCD) which has a textual hierarchical menu system for setting device configuration parameters. If you have tried writing menu code for your Arduino projects, you will recognise the challenge in developing a generic menu system for an open prototyping platform. This is because there are many input and display devices available, and a generic menu system must be independent of whichever input and display devices you wish to use. With our Arduino menu library, this independence is achieved by having the menu manager code use callback methods for handling user input and rendering the menu display.

To keep things simple, all coding examples have been targeted to work with an R3 Arduino Uno/Leonardo/Mega2560, and an LCD keypad shield similar to one illustrated above. There are numerous manufacturers of LCD keypad shields that have the same or similar pin connections, and you must ensure that the sample menu code uses the pin connections that are right for your shield. If the keypad buttons of your shield give different analog readings, you’ll need to make changes to file LcdKeypad.h. Bear in mind that the analog readings are not always consistent, which can lead to the occasional misreporting of a button press. Once you become familiar with the menu library, adapting it for use with other input and display devices should be straight-forward.

With numerous menu libraries readily available, why use this Arduino menu library? We think it is easier to use thanks to our online code generator, and has better memory efficiency with its use of PROGMEM. Watch the short video clip below and see for yourself.

You can use the downloaded sample Arduino project as a starting point for your own coding requirements. You will need to write your own code in the body of method processMenuCommand(byte cmdId) to determine what must be done when a menu item is selected. The cmdId parameter is the Id you associate with a menu item in your menu Xml file. Callback method getNavAction() handles user input, and callback method refreshMenuDisplay(byte refreshMode) renders the menu. To work with other input and display devices you’ll need to re-write the code in these methods. Some LCD keypad shields are not suited for hardware PWM backlight control, and as such the coding example uses a soft PWM alternative.

The Select button on the LCD shield starts/stops a timer, with a long press resetting the timer. A long press of Up enters the menu. Up/Down/Right buttons are for navigating the menu, and Select for choosing a menu item. When an item is selected, Up/Down are used for changing values. When the Reset menu item is displayed, a long press of Select loads default configuration values. Digital pin 2 is used for activating a beeper for the alarm. Examining the source should give you good insight for using the menu system in your own projects. If you find this Arduino menu library guide useful, please share it.

keypad with lcd display free sample

A keypad is a useful feature to add to a project that requires data input. Today we will see how easy it is to use an inexpensive matrix membrane keypad with an Arduino.

One of the simplest interfaces for an Arduino project is a push button switch. It can easily be added by connecting one side of the switch to ground and the other side to one of the Arduino’s digital input pins. Hold the input pin HIGH, either by using a resistor or the microcontroller’s internal pullup, and that’s all you need. Pressing the button will send the input pin LOW, and you can detect this change within your sketch to provide functionality for the push button.

With this arrangement, a group of push buttons are connected to different points within a resistor array, that itself is connected between the reference voltage and ground.

But it certainly is a viable choice, and we have used it before in the article about using LCD displays. The LCD display shield has a group of push buttons wired up in this exact fashion.

A matrix keypad is a group of switches connected in an X-Y, or row-column,  matrix arrangement. Pressing a button will connect a row with a column, and we can then scan the array to determine which button was pressed.

By arranging the push buttons in a matrix as opposed to connecting them directly to input pins we can usually reduce the amount of wiring. Commercial matrix keypads are inexpensive and readily available, which simplifies the wiring even more.

You’ll still need a lot of inputs, not as many as discrete switches (providing you stay over five) but more than the resistive keypad or ASCII keyboard solutions.  Once you exceed 25 push buttons you’re better off choosing the ASCII Keyboard solution.

The type of keypad we will be using today is a “Matrix Membrane Keypad”.  This is an X-Y matrix of switches created using layers of plastic and conductive surfaces.  These devices are inexpensive and can be styled to match your project, reducing the mechanical work of mounting all of those push buttons.

Two extremely common membrane matrix keypads are the 4 x 3 keypad (12 switches) and the 4 x 4 keypad (16 switches). They are laid out in the same fashion as a telephone keypad, which is a common user interface that most people are familiar with.

As you can see from the above illustration the keypad consists of four rows and four columns, overlaid by 16 push button switches (represented by circles).  Pressing a switch will connect its corresponding row and column lines.

In order to use the keypad we will make all of the column connections inputs and hold them HIGH, so they all have a value of digital one. The rows are configured as outputs, each with a value of zero or LOW.

The keypad we will be using in our experiments is very inexpensive and easily available. It consists of four rows and four columns, each connected to a film ribbon cable.

If you obtain a 4 x 3 keypad you can use it in the experiments, the only difference is that it will have a 7-pin connector and will be missing the Column 4 connection.

The first experiment we’ll perform with the keypad is to simply connect it up to the Arduino and use the serial monitor to verify that we can read all of the keys.  This is a good way to get to know how to work with the keypad, and can also be used to verify that all of the keys are indeed working.

The easiest way to get everything connected is to use a multi-conductor male-to-male Dupont ribbon cable with 8 conductors.  The hookup is quite simple, as the Arduino connections are all made in the same order as they are on the keypad connector.

The sketch we’ll be using is simplified by the use of a special library, theKeypad Library by Mark Stanley and Alexander Brevig. With this library using matrix keypads is very easy.

After including the Keypad library we define a couple of constants for the number of rows and columns in our keypad. If you are using a different keypad then modify these parameters accordingly.

The Loop is very simple. We use thegetKeymethod of the keypad library to get a key value when it detects a keypress. Then we simply print that to the serial monitor.

Load the sketch and open your serial monitor, making sure to observe the baud rate (it should be 9600 baud). Now press some keys on the keypad. You should observe the key values displayed on the serial monitor.

Our next experiment is just an extension of the first one. This time we will use an LCD display instead of the serial monitor to read keypress values.

To reduce the number of connections required we will be using an LCD display with an I2C backpack.  This makes the hookup extremely simple, as illustrated below:

One difference, of course, is that we are including both the Wire Library for I2C and the new LiquidCrystal_I2C library that we just installed.  Both of these libraries are required to use the LCD display with the I2C backpack.

In line 39 we create an object to represent our LCD display. The first parameter in the object is “0x3F”, which is the hexadecimal I2C address of the display. If you are using a display that has a different I2C address you will need to modify this parameter accordingly.

The Loop is almost identical to the previous sketch. Once again we get the value of the keypress and hold it in a character variable. Then we clear the LCD display and print the character to the display.  It will remain on the display until the next key is pressed.

A great use for a matrix keypad is to build a combination lock. You can use this to lock a door or a drawer or to regulate access to an electronic device.

Two character variable arrays are defined. TheDataarray holds the key entries, and theMasterarry has the actual password. You should edit the password to be something harder to guess than just the first seven digits on the keypad!

We start the Loop by placing the LCD cursor in the top left corner and printing “Enter Password:”.   Then we look for a keypress, as we did in the previous sketches.

Once we have enough characters we compare theDataarray to theMasterarray. If the arrays are identical then we have the correct password and we activate the relay for 5 seconds.  If it is incorrect we print “Incorrect” on the display and hold it there for a second.

Matrix keypads are an easy-to-use input device that can add a lot of functionality to your Arduino projects.  They are inexpensive and they can give your project a professional look and feel.

Matrix Keypads are easy-to-use input devices that can add a lot of functionality to your Arduino project. In this article, I"ll show you how to use these keypads with an Arduino. We will also build an electronic combination lock.

keypad with lcd display free sample

This is a very popular LCD Keypad shield for Arduino or Freeduino board. It includes a 2x16 LCD display and 6 momentary push buttons. Pins 4, 5, 6, 7, 8, 9 and 10 are used to interface with the LCD. Analog Pin 0 is used to read the push buttons. The LCD shield supports contrast adjustment and backlit on/off functions. It also expands analog pins for easy analog sensor reading and display.

The LCD Keypad shield is developed for Arduino compatible boards, to provide a user-friendly interface that allows users to go through the menu, make selections etc. It consists of a 1602 white character blue backlight LCD. The keypad consists of 5 keys — select, up, right, down and left. To save the digital IO pins, the keypad interface uses only one ADC channel. The key value is read through a 5 stage voltage divider.

Creates a variable of type LiquidCrystal. The display can be controlled using 4 or 8 data lines. If the former, omit the pin numbers for d0 to d3 and leave those lines unconnected. The RW pin can be tied to ground instead of connected to a pin on the Arduino; if so, omit it from this function"s parameters. for example:

Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display. begin() needs to be called before any other LCD library commands.for example:

The J1-J8 include the both the user interface, i.e. Analog pins, APC220(Serial) pins, Digital pins, and the pins connected with the lower Arduino card, e.g. Uno/ Leonardo. Here is a simple mapping picture.

keypad with lcd display free sample

The processors that power the Arduino development boards aren"t your granddad"s microcontrollers - they"ve got serious processing power and can be used to monitor and control many types of physical hardware and sensors simultaneously. The XOD graphical programming IDE for Arduino can simplify the task of rapid-prototyping hardware designs, but as a system grows in complexity there"s the issue of user control and information management - how to arrange information about the process being controlled for display and let the user control it in a logical manner. Dedicated buttons and switches work for simple projects but one soon runs out logical ways to arrange them (or places to put them on an enclosure.) Many designs use a display of some type to give this kind of feedback, even a microwave oven usually has a small screen that allows editing settings and entering cook times, and while too much "menu diving" to access obscure settings can compromise user experience, menu-driven interfaces are a fact of life for projects of all kinds.

Software frameworks for developing menu-based interfaces in C or C++ for microcontrollers often have a steep learning curve and can be difficult to make rapid design changes in, as often everything interacts with everything else. The goal of this project was to use the XOD graphical programming environment to provide the ability to rapidly prototype menu-driven interfaces for Arduino-compatible microcontroller projects using a drag-and-drop/WYSIWYG style of interface. The output and input parameters of the generated menu structure are modular and generic and should be able to be used with a wide variety of input sources (switches, buttons, encoders) and produces a sequence of plain text outputs which can be fed to a wide variety of XOD-supported display types, including multi-line text LCD modules as used in this example.

At the right of the patch editing screen in XOD are four nodes connected up in a descending fashion. From top to bottom there"s first an analog input-type node, which is selected to be connected to one the Arduino"s analog input ports. In my tests I"ve been using an Arduino Uno with an ATMega328 8 bit processor, with a 16x2 LCD/keypad shield connected on top of it. The keypad on the shield is connected via the shield pins to the Arduino"s "A0" analog input, and the hardware generates voltages of differing levels to signal to the Arduino which buttons on the pad are depressed.

Other than the "reset" button on the keypad which doesn"t interface to the Arduino sketch directly, this type of display shield provides four directional buttons - up, down, left, and right, a "select" button. The second node down fed by the analog input node contains code which reads the differing voltage levels and outputs pulse-type triggers depending on which button is pressed. A "timeout" input is provided so an XOD developer can adjust the debounce timeout of the LCD shield keypad to avoid false triggering, adjusted to a particular application"s button or switch type.

As currently implemented it accepts a single Number-type input which can be any parameter the user can change from an external control, say a potentiometer or dial on a control panel which represents some parameter that can be changed. When a given leaf-type menu is selected by the menu-controller receiving a pulse to its "Invoke" input, over in the menu tree to the left the node of the screen the user is currently looking at will output a pulse on its own output, and also send out a parameter change. There are also two String inputs which can be used to generate a splash screen on the display when the Arduino powers up.

Following that node is a stock 16x2 LCD controller module from the XOD environment standard library, but any type of display that a module is available for can be used to display the output text here.

At the top of the sketch screenshot is a tree-like structure which, when compiled and the final output "root" of the tree is routed to the menu-controller input that accepts it, will automatically generate a navigable menu just as pictured in the graphical designer. Currently implemented are three types of menu tree nodes, a "leaf"-type menu - a final sub menu, with no orange-colored input ports - this represents some action or parameter the user can control, a "branch"-type menu with both an input and output, where the user can select among several child sub-menus, and a "concat"-type menu where sub-menus are grouped together and fed into the input of a branch-type menu, to make a group of options. It should hopefully be somewhat self-explanatory from the diagram how to wire up this set of nodes.

Selecting "invoke" when a "branch"-type menu is displayed descends into its associated sub menu group, rather than generating an output as a leaf-type menu does. Sub-sub menus, sub-sub-sub menus, etc. are possible! Complex structures can be implemented, limited primarily by the available RAM and Flash memory resources of a particular Arduino model"s processor.

This set of nodes has been tested and should work OK with the Arduino Uno as-is, but a few issues remain that weren"t resolved by this project"s deadline. Currently there must be a branch-type menu in the tree somewhere for a sketch to compile, though most projects would probably want to have at least one. The "top" menu input to the menu-controller, intended to return all the way back to the title screen of the interface, isn"t currently implemented but should be an easy feature for the next revision.

Four-line displays are supported by the menu controller node but the leaf-type menu interface with four lines of text input hasn"t been written, yet. A good amount of effort has been put into reducing the memory overhead of this utility but the way the XOD transpiler currently stores constant strings isn"t optimal for very low RAM overhead, and menu-based interfaces tend to have a lot of text. For this project a work-around was developed and a set of flash-string type nodes added in the patch to ensure longer strings did not consume precious program RAM.

Using the flash-string type nodes isn"t too painful but does require duplicating the patch and code for each new string stored in program memory, and clicking into the C++ code contained within the node itself to edit the output string:

The "ID" input of leaf and branch nodes doesn"t have a function, yet. The thought is it would be good for each set of sub-menus to have associated ID numbers arranged in order so the user can keep track of their position in a set of displays but it will require some more thought on how to code this in C++ to happen automatically.

keypad with lcd display free sample

I have the bigger version with 4 instead of 3 columns. The bigger version includes all buttons of the smaller version but has also buttons for the letters: A, B, C, D

The keypad is internally build like a matrix with rows and columns. The you can identify a cell in an Excel sheet with the row and column number, the Arduino is able to identify the pressed button on the keypad. The following picture shows that the first 4 connectors identify the row number (R1, R2, R3, R4) and the last 3 to 4 connectors identify the column number (C1, C2, C3, C4), depending on the version of the keypad.

In the table on the right sight of the picture you see that if a button is pressed, the keypad field has a unique identifier, the row and column number. Therefore the microcontroller is able to identify the keypad field through a 4 step mechanism that is described with the following picture:

Now, after we know how the keypad works, Is show you how to connect the keypad to your microcontroller. The following picture shows the wiring between the most used Arduino, ESP8266 and ESP32 microcontroller and the keypad. If you are missing your favorite microcontroller, leave a comment at the bottom of this article and I will also add you microcontroller.

Now, after we know how the keypad is wired to your microcontroller, we make a basic example. In this example we want to display the number which is pressed in the serial monitor. If this example is to easy for you, at the end of this article there is also a more advanced example where we build a password lock.

For this example we use the Keypad library of Mark Stanley and Alexander Brevig which make the use of the keypad very easy. This library support the 3×4 and 4×4 keypad. If you do now know how to install a library, click here for a step by step tutorial.

In the first line we include the previous or already installed library Keypad. Then we define the number of rows and columns your keypad has. In my case I have to 4×4 keypad.

To be able to identify the keypad right we create a matrix with the keypad. If your keypad has a different layout, then you can change the layout with this matrix. Make sure that the number of rows and columns is right.

The last part in the section where we define the variables for the wiring between the microcontroller and the keypad. You can use every digital I/O pin on your microcontroller. In my case I used the pins 2 to 9 for Arduino, pins D1 to D8 for all ESP8266 microcontroller and the first digital pins on one site of the ESP32 NodeMCU. The program code is commented for Arduino boards. If you want to use for example an ESP32 NodeMCU, you have to uncomment the lines of code for the ESP32 and comment the lines for the Arduino boards.

Next the library Keypad has to know what keypad is connected to the microcontroller. Therefore we create an object called keypad from the library and pass the previous defined variables to this object.

In the loop function we first read the character which we get from the keypad and store this character in the key variable. If the key variable is not empty, we print the character to the serial output.

Now the more advanced example where we build a lock which opens when we type in the correct password. In this example I use the keypad and an LCD display along with an Arduino Uno. If you want to use an ESP8266 or ESP32 board, you can use the wiring from the previous chapter and only have to add the LCD display.

You see that we only connected the LCD display and that the magic happens inside the program code which I explain step by step in the following section.

The first part of the code is nearly the same, only we include the library of the LCD display LiquidCrystal_I2C.h and create an object of this library called lcd. I use a 20×4 LCD display on I2C address 0x27 for this example. If you use a different one, you have to change the parameters of the lcd object.

Because I want to see as user of the password lock how many characters I already typed in, we print an asterisk character for each character that was typed in by the user. Therefore we set the cursor for the LCD screen to z-1 and not z in the second row of the screen and print the asterisk character.

Because the used keypad in this tutorial uses 8 digital I/O pins on the microcontroller, it is useful to reduce the number of used pins with an PCF8574 I2C multiplexer.

The picture on the right side shows the pins of the PCF8574 and the following picture shows the wiring between the Arduino, ESP8266 or ESP32 microcontroller, the I2C multiplexer and the keypad.

In the following section, we go over the program code to use the I2C expander in combination with the keypad. Because we do not use microcontroller specific pins, the program code is valid for Arduino, ESP8266 and ESP32.

In the first part of the program script we include the Wire library, to communicate via I2C and the I2CKeyPad library that helps us to map the keypad signals on the I2C communication protocol.

Now we create an I2CKeyPad object called keyPad and also define a variable to store the last pressed position on the keypad, called lastKey. The datatype of uint8_t is an unsigned integer of length 8 bits.

In the loop function we first create an array of the keys on the keypad. In my case I have a 4×4 keypad. The last two entries helps us to get values of the library if there is no key pressed or the readout of the keypad failed. With the getKeyfunction we store the current pressed place on the keypad in the variable idx.

In the if condition we define that only the current key is pressed, when the key is a different from the previous one and only if there is a key pressed. Therefore we look up the pressed key in the keysarray with the position of the keypad that is stored in variable idx. In the last step we save the current position of the keypad in the lastKeyvariable and set a small delay of 0.1 seconds.

The output of the I2C multiplexer is the same like you are not using the multiplexer. But you can save a lot of digital I/O pins on your microcontroller. The following picture shows the example with the I2C multiplexer.

I hope you liked this tutorial where you learned how to use the keypad as input device and build a lock which opens when we type in the correct password. If you want to reduce the number of used digital I/O pins, I recommend to read the article about the digital multiplexer.

keypad with lcd display free sample

Shield for Arduino with popular 2x16 characters LCD display and five buttons at your disposal. The screen uses 4, 5, 6, 7, 8, 9 and 10 pins. Reading the status of the buttons is done through an analog signal, thanks to this solution all five buttons use only one A0 output.

The module works with standard Arduino Liquid Crystal library. In order to start the module it is necessary to provide connection of the outputs in the library according to the table below: