Raspberry Pi Keypad 1x4

Trong hướng dẫn này, chúng ta sẽ học cách sử dụng keypad 1x4 với Raspberry Pi. Cụ thể, chúng ta sẽ học:

Raspberry Pi bàn phím ma trận 1x4

Linh Kiện Cần Thiết

1×Raspberry Pi 5
1×Bàn phím ma trận 1x4
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Raspberry Pi
1×(Khuyến nghị) Raspberry Pi Prototyping Base Plate & Breadboard Kit
1×(Khuyến nghị) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Keypad 1x4

Keypad 1x4 được tạo thành từ bốn nút màng được sắp xếp thành một hàng duy nhất. Nó thường được sử dụng để nhập liệu trong các dự án như nhập mã pin, điều hướng menu, hoặc giao diện điều khiển.

Sơ Đồ Chân

Keypad 1x4 có 5 chân, không tương ứng trực tiếp với thứ tự nhãn phím. Cụ thể:

  • Chân 1: kết nối với phím 2
  • Chân 2: kết nối với phím 1
  • Chân 3: kết nối với phím 4
  • Chân 4: kết nối với phím 3
  • Chân 5: là chân chung kết nối với tất cả các phím
bàn phím ma trận 1x4 sơ đồ chân
image source: diyables.io

Sơ Đồ Kết Nối

Raspberry Pi bàn phím ma trận 1x4 sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Raspberry Pi

Mỗi phím trên keypad 1x4 hoạt động như một nút nhấn. Điều này có nghĩa là chúng ta có thể sử dụng hàm digitalRead() để kiểm tra trạng thái của từng phím. Tuy nhiên, trong thực tế, giống như với bất kỳ nút nhấn nào, chúng ta phải xử lý vấn đề về bounce, khi một lần nhấn có thể bị phát hiện nhầm là nhiều lần nhấn. Để tránh điều này, chúng ta cần debounce cho từng phím. Nhiệm vụ này trở nên thách thức khi cố gắng debounce bốn phím mà không chặn các phần khác của code. May mắn thay, thư viện ezButton đơn giản hóa quá trình này.

Các Bước Thực Hiện

  • Đảm bảo bạn đã cài đặt Raspbian hoặc hệ điều hành tương thích với Raspberry Pi khác trên Pi của bạn.
  • Đảm bảo Raspberry Pi được kết nối với cùng mạng cục bộ với PC của bạn.
  • Đảm bảo Raspberry Pi được kết nối internet nếu bạn cần cài đặt thư viện.
  • Nếu đây là lần đầu tiên bạn sử dụng Raspberry Pi, hãy xem Raspberry Pi - Cài Đặt Phần Mềm
  • Kết nối Arduino Nano với keypad 1x4.
  • Kết nối PC của bạn với Raspberry Pi qua SSH sử dụng SSH client tích hợp trên Linux và macOS hoặc PuTTY trên Windows. Xem cách kết nối PC với Raspberry Pi qua SSH.
  • Đảm bảo bạn đã cài đặt thư viện RPi.GPIO. Nếu chưa, hãy cài đặt bằng lệnh sau:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Tạo một file script Python keypad_1x4.py và thêm đoạn code sau:
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi này được cung cấp để sử dụng công khai, không có ràng buộc. # Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: # https://newbiely.vn/tutorials/raspberry-pi/raspberry-pi-keypad-1x4 import RPi.GPIO as GPIO import time # Define GPIO pins for the keypad buttons PIN_KEY_1 = 24 # The Raspberry Pi pin GPIO24 connected to the key 1 PIN_KEY_2 = 23 # The Raspberry Pi pin GPIO23 connected to the key 2 PIN_KEY_3 = 8 # The Raspberry Pi pin GPIO8 connected to the key 3 PIN_KEY_4 = 25 # The Raspberry Pi pin GPIO25 connected to the key 4 KEY_PINS = [PIN_KEY_1, PIN_KEY_2, PIN_KEY_3, PIN_KEY_4] DEBOUNCE_TIME = 0.1 # 100 milliseconds # Setup GPIO mode GPIO.setmode(GPIO.BCM) # Setup GPIO pins as inputs with pull-up resistors for pin in KEY_PINS: GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Initialize previous states for each button previous_states = [GPIO.input(pin) for pin in KEY_PINS] # Function to read the state of the keypad buttons with debouncing def get_key_pressed(): pressed_key = 0 for i, pin in enumerate(KEY_PINS): current_state = GPIO.input(pin) if previous_states[i] == GPIO.HIGH and current_state == GPIO.LOW: # Detect state change from HIGH to LOW time.sleep(DEBOUNCE_TIME) # Wait for debounce time if GPIO.input(pin) == GPIO.LOW: # Ensure button is still pressed pressed_key = i + 1 break return pressed_key try: while True: key = get_key_pressed() if key: print(f"The key {key} is pressed") # Update the previous states previous_states = [GPIO.input(pin) for pin in KEY_PINS] time.sleep(0.1) # Delay for a short period to avoid excessive CPU usage except KeyboardInterrupt: # Cleanup GPIO settings before exiting GPIO.cleanup() print("Program terminated")
  • Lưu file và chạy script Python bằng cách thực hiện lệnh sau trong terminal:
python3 keypad_1x4.py
  • Nhấn từng phím trên keypad 1x4 một cách tuần tự
  • Kiểm tra kết quả trên Terminal.
PuTTY - Raspberry Pi
The key 1 is pressed The key 2 is pressed The key 3 is pressed The key 4 is pressed