Raspberry Pi Pico -- 4x4 Matrix Keypad and 1602 LCD Interface

by PugazhM in Circuits > Raspberry Pi

9072 Views, 4 Favorites, 0 Comments

Raspberry Pi Pico -- 4x4 Matrix Keypad and 1602 LCD Interface

Circuit_V01.jpeg

This experimentation uses RPi Pico, to interface with 4x4 Matrix Keypad and 1602 LCD. The Python program scans the Keypad at interval, and if a valid key press is sensed, then displays corresponding key value at 1602 LCD Screen

Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos.

https://www.youtube.com/channel/UCY4mekPLfeFinbQHp...

The Matrix keypad

Abstract

The Raspberry Pi Pico is a tiny, fast, and versatile board built using RP2040 features a dual-core Arm Cortex-M0+ processor with 264KB internal RAM and support for up to 16MB of off-chip Flash. It provides wide range of flexible I/O options includes I2C, SPI, and uniquely Programmable I/O (GPIO) pins.

In Embedded system design, matrix keypad (4x4, 4x3, 3x3 or 5x5) is used for key in the user inputs.

Similarly, character LCD displays [16x2, 16x4, 20x2 or 20x4 LCDs] are used for indicating the system status / parameters

This experimentation uses RPi Pico, to interface with 4x4 Matrix Keypad and 1602 LCD. The Python program scans the Keypad at interval, and if a valid key press is sensed, then displays corresponding key value at 1602 LCD Screen.

Reference

Raspberry Pi Pico, Getting Started on Board Blink LED” Instruct-able / You-tube by PugazhM

https://www.instructables.com/Raspberry-Pi-Pico-G...

“Raspberry Pi Pico -- 16x2 LCD, 4Bit Mode Interface, BCD Counter” Instruct-able / You-tube by PugazhM https://www.instructables.com/Raspberry-Pi-Pico-G...

https://www.instructables.com/Raspberry-Pi-Pico-G...

The “lcd_4bit_mode.py” driver library

Component

Components.jpg

Raspberry Pi Pico

Micro USB Cable

16x2 LCD Keypad Shield

4x4 Matrix Keypad

Schematic -- 4Bit Mode - 16x2 LCD Connection

Circuit_V01.jpeg

The 16x2 is very common type LCD, with two rows.

The “16x2 LCD Keypad shield” can be connected to Raspberry Pi Pico GPIO pins (GPIO0--GPIO6).

LCD operates at 4-bit data mode as given in above circuit.

If the display is not visible, adjust the Contrast pot (1K), to make it visible.

4x4 Matrix Keypad Interface

Matrix key pad can be arranged by push button switches in rows and columns.

In a simple technique, the 16 keys of matrix keypad are connected with 8 digital GPIO pins of RPi Pico.

Usually, the keypad scan procedure is Write HIGH to Row One. And keep rest of the Row to LOW.

Scan (Read) the Column One to Column Four, to find the key.

Repeat until a key press (are multiple) is identified. The key is decoded through Row selection / Column read.

The Row pins are connected to 8,9,10 and 11 of RPi Pico GPIO pins.

The Column pins are connected to 12,13,15 and 16 of RPi Pico GPIO pins

Python Program – 4x4 Matrix Keypad Interface

TaskEvent_V01.jpeg

As referenced above, download “lcd_4bit_mode.py” driver library

Open the RPi Pico as drive, and then copy the library into root directory

LCD is initialized and welcome screen of “RPi Pico 4x4 Matrix Keypad” is displayed in two rows, for about 5 seconds

The timer_one is initialized and callbacks the “BlinkLED” functionality for toggling on board LED at 200mS duration. (frequency = 5)

The timer_two is initialized and callbacks the “PollKeypad” functionality for polling the keypad at 500mS duration. (frequency = 2)

The “PollKeypad” function, first writes HIGH to Row One, and keep rest of the Row to LOW. Then scans (Read) the Column One to Column Four, to find out the key.

If the key is pressed, then the program detects the key press and displays corresponding key value at LCD screen

'''
Demonstrates the use of Matrix keypad with RPi Pico. 4Bit mode LCD interface on Instruct-able and You-tube: https://www.instructables.com/Raspberry-Pi-Pico-16x2-LCD-4Bit-Mode-Interface-BCD/ https://www.youtube.com/channel/UCY4mekPLfeFinbQHpf9QcHg * The Raspberry Pi Pico pin connections for 16x2 LCD, for 4Bit mode are given below: # LCD Power Pins * 16x2 LCD VCC pin to VBUS * 16x2 LCD GND pin to GND # LCD Data Pins * 16x2 LCD D4 pin to GPIO0 * 16x2 LCD D5 pin to GPIO1 * 16x2 LCD D6 pin to GPIO2 * 16x2 LCD D7 pin to GPIO3 # LCD Control Pins * 16x2 LCD RS pin to GPIO4 * 16x2 LCD ENABLE pin to GPIO5 * 16x2 LCD BACK LIGHT pin to GPIO6
 * The Raspberry Pi Pico pin connections for matrix keypad:
 * R1 pin of keypad to RPi Pico GPIO8
 * R2 pin of keypad to RPi Pico GPIO9
 * R3 pin of keypad to RPi Pico GPIO10
 * R4 pin of keypad to RPi Pico GPIO11
 * C1 pin of keypad to RPi Pico GPIO12
 * C2 pin of keypad to RPi Pico GPIO13
 * C3 pin of keypad to RPi Pico GPIO15
 * C4 pin of keypad to RPi Pico GPIO16
  
 Name:- M.Pugazhendi
 Date:-  20thJul2021
 Version:- V0.1
 e-mail:- muthuswamy.pugazhendi@gmail.com
'''
from machine import Pin, Timer
# Import time
import time
import utime
import machine
# Import lcd_4bit_mode
import lcd_4bit_mode
# Initialize LCD pins
RS = machine.Pin(4,machine.Pin.OUT)
ENABLE = machine.Pin(5,machine.Pin.OUT)
BACK_LIGHT = machine.Pin(6,machine.Pin.OUT)
D4 = machine.Pin(0,machine.Pin.OUT)
D5 = machine.Pin(1,machine.Pin.OUT)
D6 = machine.Pin(2,machine.Pin.OUT)
D7 = machine.Pin(3,machine.Pin.OUT)                  
 
# CONSTANTS
KEY_UP   = const(0)
KEY_DOWN = const(1)
keys = [['1', '2', '3', 'A'], ['4', '5', '6', 'B'], ['7', '8', '9', 'C'], ['*', '0', '#', 'D']]
# RPi Pico pin assignments
rows = [8,9,10,11]
cols = [12,13,15,16]
# Set pins for rows as outputs
row_pins = [Pin(pin_name, mode=Pin.OUT) for pin_name in rows]
# Set pins for columns as inputs
col_pins = [Pin(pin_name, mode=Pin.IN, pull=Pin.PULL_DOWN) for pin_name in cols]
#Initialize the onboard LED as output
led = Pin(25, Pin.OUT)
#Initialize timer_one. Used for toggling the LED
timer_one = Timer()
#Initialize timer_two. Used for polling keypad
timer_two = Timer()
def BlinkLED(timer):
    led.toggle()
def InitKeypad():
    for row in range(0,4):
        for col in range(0,4):
            row_pins[row].low()
def PollKeypad(timer):
    key = None
    for row in range(4):
        for col in range(4):
            # Set the current row to high
            row_pins[row].high()
            # Check for key pressed events
            if col_pins[col].value() == KEY_DOWN:
                key = KEY_DOWN
            if col_pins[col].value() == KEY_UP:
                key = KEY_UP
            row_pins[row].low()
            if key == KEY_DOWN:
                display.WriteLine("                ",2)
                display.WriteLine("Key Pressed = "+ keys[row][col],2)
                last_key_press = keys[row][col]
# Initialize and set all the rows to low
InitKeypad()
display = lcd_4bit_mode.LCD16x2(RS,ENABLE,BACK_LIGHT,D4,D5,D6,D7)
# Turn on Back light
display.BackLightOn()
# Welcome string
display.WriteLine('    RPi PICO',1)
display.WriteLine('4x4 MatrixKeypad',2)
# Wait for five seconds
time.sleep(5)
timer_one.init(freq=5, mode=Timer.PERIODIC, callback=BlinkLED)
timer_two.init(freq=2, mode=Timer.PERIODIC, callback=PollKeypad)

Conclusion

Conclusion.jpg

The project is successfully completed with Raspberry Pi Pico, 4x4 Matrix Keypad and 1602 LCD Keypad shield

The program polls the key pad at regular interval and identifies the valid “key pressed event” Displays the key value at LCD screen.

Video Link

Result_Video.mp4

Visit "VESZLE - The Art Of Electronics" you tube channel for further information and videos. https://www.youtube.com/channel/UCY4mekPLfeFinbQH...

If you enjoyed this instruct-able, then make sure you subscribe

The Matrix Keypad video link