Arduino UNO and 4×4 matrix keyboard

Arduino UNO 11-02-25
35 0

Tutorial plan

1- What is a 4x4 matrix keyboard ?

2- How Arduino UNO displays 4x4 matrix keypad data on LCD screen ?

3- Components Needed

4- Wiring the Components

5- Arduino program

 

 

What is a 4x4 matrix keyboard ?

A 4x4 matrix keyboard (or keypad) is an input device consisting of 16 buttons arranged in a grid of 4 rows and 4 columns. Each button is located at the intersection of a specific row and column. Instead of connecting each button with individual wires, the matrix configuration reduces the number of pins needed to detect key presses, making it efficient for microcontroller projects like those with Arduino or ESP32.

How It Works:

- The keypad operates on a grid system where pressing a button connects a row to a column electrically.

- A microcontroller scans the rows and columns:

1. It sets one row to LOW (active) and others to HIGH.

2. It checks the columns to see if a connection (button press) is detected.

3. This process repeats for all rows to identify the specific key pressed.

 

How Arduino UNO displays 4x4 matrix keypad data on LCD screen ?

The Arduino UNO, in combination with a 4x4 matrix keypad and an LCD screen (with I2C interface), forms an efficient system to capture user inputs (key presses) and display them in real time. Here's a step-by-step explanation of how this process works:

Step 1: Keypad Scanning (Detecting a Key Press)

The Arduino scans the keypad using a technique called "multiplexing":

- It sends a LOW signal to one row at a time while keeping the other rows HIGH.

- Simultaneously, it checks the columns for a LOW signal.

- If a LOW signal is detected on a column, it means a button connecting that specific row and column is pressed.

This process runs rapidly in the background to detect key presses almost instantly.

Step 2: Mapping the Key Press

The Arduino maps the detected row-column combination to a specific character (e.g., 1, 2, A, *).

This mapping is defined in a 2D array in the code, which represents the keypad layout.

Step 3: Sending Data to the LCD

- Once a key is detected, the Arduino uses the I2C communication protocol to send the corresponding character to the LCD.

- The I2C module converts Arduino’s signals to control the LCD using only two pins:

SDA (Serial Data Line) for data transmission.

SCL (Serial Clock Line) for synchronizing the data.

Step 4: Displaying Data on the LCD

The LCD receives the character and displays it on the screen at a specific position (cursor location).

The cursor can be moved programmatically to control where the next character appears (e.g., first line, second line).

 

Components Needed

Arduino UNO

Arduino Uno

Arduino UNO is  a Microcontroller that orchestrates the whole process: reading input from the keypad, processing it, and sending the data to the LCD.

It manages the keypad scanning, character mapping, and LCD communication.

4x4 Matrix Keypad

4x4 Matrix Keypad is an Input Device that allows the user to send data by pressing buttons. It’s a compact matrix of 16 buttons arranged in 4 rows and 4 columns.

LCD 16x2 with I2C Module

LCD Screen is an Output Device that displays the data received from the Arduino (such as the pressed key from the keypad).

Wires

Jumper wires

The wires connects the components to the Arduino UNO.

 

Wiring the Components

Keypad to Arduino:

R1–R4 (Rows) → Arduino Pins 9, 8, 7, 6

C1–C4 (Columns) → Arduino Pins 5, 4, 3, 2

LCD with I2C to Arduino:

VCC → 5V

GND → GND

SDA → A4 (Arduino UNO)

SCL → A5 (Arduino UNO)

 

Arduino program

 

1. Importing Libraries :

LiquidCrystal_I2C.h: This library simplifies controlling LCDs with I2C. It allows us to send text to the LCD screen using just the I2C protocol.

Keypad.h: This library simplifies interfacing with the 4x4 matrix keypad, handling row-column scanning and key detection.

2. LCD Initialization :

This line creates an instance of the LiquidCrystal_I2C class and initializes the LCD with an I2C address of 0x27 (this might vary, sometimes 0x3F is used) and a screen size of 16 columns and 2 rows.

The I2C address is unique to each LCD, so it is essential to ensure the correct address (you can often find this in the datasheet or use an I2C scanner to detect it).

3. Keypad Configuration :

These constants define the number of rows and columns of the 4x4 keypad.

This 2D array represents the physical layout of the keys on the keypad. Each button corresponds to a unique character, such as 1, 2, 3, etc.

The structure of the array is:

Row 1: 1, 2, 3, A

Row 2: 4, 5, 6, B

Row 3: 7, 8, 9, C

Row 4: *, 0, #, D

These arrays define the Arduino pin connections for the keypad rows and columns. The Arduino will use these pins to scan for key presses.

4. Keypad Initialization :

This line initializes the keypad using the Keypad class.

makeKeymap(keys): This function creates a mapping between the physical keys on the keypad and the corresponding characters in the keys array.

The rowPins and colPins arrays define the digital pins on the Arduino that are connected to the keypad's rows and columns.

5. LCD Initialization in setup()

lcd.begin(16, 2): Initializes the LCD display with 16 columns and 2 rows.

lcd.backlight(): Turns on the LCD backlight to make the display readable.

lcd.print("Key Pressed:"); Displays a welcome message on the LCD.

6. Main Logic in loop() :

This function checks if any key on the keypad has been pressed and stores the key in the variable key.

if (key): This condition checks if a key is pressed (i.e., if the key variable is not NULL).

lcd.setCursor(0, 1): Moves the cursor to the second row of the LCD (row 1, column 0).

lcd.print(" "); Clears the previous key press by printing spaces.

lcd.print(key): Displays the currently pressed key on the LCD.

7. Continuous Loop

The loop() function continuously scans the keypad, and whenever a key is pressed, it updates the LCD to show the corresponding key.

0 comment

Leave a comment

Veuillez noter s'il vous plaît*

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Purpose of this website

Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.

Contact details

Zaouiet Kontech-Jemmel-Monastir-Tunisia

+216 92 886 231

medaliprof@gmail.com

Pictures of the articles

Robotic site created by MedAli-Teacher info