Arduino UNO R4 - Keypad - LCD

This tutorial instructs you how to program Arduino UNO R4 to read key pressed from a keypad and display it on LCD display.

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×LCD I2C
1×Keypad 3x4
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Keypad and LCD

If you're unfamiliar with keypads and LCDs, including their pinouts, functionality, and programming, please refer to the following tutorials to learn more:

Wiring Diagram

The wiring diagram between Arduino UNO R4 Keypad LCD

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-keypad-lcd */ #include <DIYables_Keypad.h> // DIYables_Keypad library #include <LiquidCrystal_I2C.h> const int ROW_NUM = 4; // four rows const int COLUMN_NUM = 3; // three columns char keys[ROW_NUM][COLUMN_NUM] = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' }, { '*', '0', '#' } }; byte pin_rows[ROW_NUM] = { 9, 8, 7, 6 }; //connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = { 5, 4, 3 }; //connect to the column pinouts of the keypad DIYables_Keypad keypad = DIYables_Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows int cursorColumn = 0; void setup(){ lcd.init(); // initialize the lcd lcd.backlight(); } void loop(){ char key = keypad.getKey(); if (key) { lcd.setCursor(cursorColumn, 0); // move cursor to (cursorColumn, 0) lcd.print(key); // print key at (cursorColumn, 0) cursorColumn++; // move cursor to next position if(cursorColumn == 16) { // if reaching limit, clear LCD lcd.clear(); cursorColumn = 0; } } }

※ NOTE THAT:

The I2C address for the LCD might change depending on the maker. In our program, we used the address 0x27 as given by the manufacturer DIYables.

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the Arduino Uno R4 board to the keypad and LCD I2C according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Click on the Libraries icon on the left side of the Arduino IDE.
  • Type DIYables_Keypad in the search box, and look for the keypad library by DIYables.io.
  • Click the Install button to add the keypad library to your IDE.
Arduino UNO R4 keypad library
  • Search for "LiquidCrystal I2C" and look for the LiquidCrystal_I2C library by Frank de Brabander.
  • Click the Install button to install the LiquidCrystal_I2C library.
Arduino UNO R4 LiquidCrystal I2C library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino UNO R4.
Arduino IDE Upload Code
  • Press keys on the keypad
  • Check the LCD to see the result

If the LCD screen shows nothing, check Troubleshooting for LCD I2C for help.

Code Explanation

Check the explanations in the comments within the source code!

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!