Arduino UNO R4 Keypad 3x4

Hướng dẫn này sẽ chỉ bạn cách sử dụng Arduino UNO R4 với keypad 3x4. Cụ thể, chúng ta sẽ học:

Arduino UNO R4 3x4 bàn phím ma trận

Phần Cứng Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Thay thế)
1×Cáp USB Type-C
1×Bàn phím ma trận 3x4
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Keypad 3x4

Keypad có 12 nút bấm màng được sắp xếp theo hàng và cột, được gọi là ma trận. Mỗi nút được gọi là một phím.

Sơ Đồ Chân

Keypad 3x4 có 7 chân, chia thành hai loại: hàng và cột.

  • 4 chân dành cho các hàng (R1, R2, R3, R4).
  • 3 chân dành cho các cột (C1, C2, C3).
3x4 bàn phím ma trận sơ đồ chân

Sơ Đồ Kết Nối

Arduino UNO R4 bàn phím ma trận 3x4 sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Arduino UNO R4

#include <DIYables_Keypad.h> // DIYables_Keypad library 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); void setup() { Serial.begin(9600); delay(1000); Serial.println("Keypad 3x4 example"); } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); } }

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

Hãy thực hiện theo các hướng dẫn từng bước sau:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Kết nối Arduino Uno R4 với keypad 3x4 theo sơ đồ được cung cấp.
  • Kết nối Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Mở Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
  • Vào biểu tượng Libraries ở phía bên trái của Arduino IDE.
  • DIYables_Keypad vào ô tìm kiếm, và tìm thư viện keypad của DIYables.io.
  • Nhấn nút Install để cài đặt thư viện keypad.
Arduino UNO R4 bàn phím ma trận thư viện
  • Sao chép code ở trên và mở nó bằng Arduino IDE.
  • Nhấn nút Upload trong Arduino IDE để tải code lên Arduino UNO R4.
  • Mở Serial Monitor.
  • Nhấn một số phím trên keypad.
  • Kiểm tra kết quả trong Serial Monitor.
COM6
Send
3 6 9 4 * #
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Keypad và Mật Khẩu

Một ứng dụng phổ biến của keypad là nhập mật khẩu. Trong ứng dụng này, chúng ta chú ý đến hai phím đặc biệt:

  • Một phím để bắt đầu hoặc bắt đầu lại việc nhập mật khẩu. Ví dụ, phím "*"
  • Một phím để kết thúc việc nhập mật khẩu. Ví dụ, phím "#"

Mật khẩu sẽ được tạo thành từ các phím còn lại, loại trừ hai phím đặc biệt đã chọn.

Khi một phím được nhấn:

  • Nếu phím không phải là "*" hoặc "#", thêm phím đó vào mật khẩu người dùng đang nhập.
  • Nếu phím là "#", kiểm tra xem mật khẩu người dùng nhập có khớp với mật khẩu đã đặt hay không, sau đó xóa mật khẩu đã nhập.
  • Nếu phím là "*", xóa mật khẩu đã nhập.

Code Keypad - Mật Khẩu

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-keypad-3x4 */ #include <DIYables_Keypad.h> // DIYables_Keypad library 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 ); const String password = "1234"; // change your password here String input_password; void setup(){ Serial.begin(9600); Serial.println("Keypad 3x4 password"); input_password.reserve(32); // maximum input characters is 33, change if needed } void loop(){ char key = keypad.getKey(); if (key){ Serial.println(key); if(key == '*') { input_password = ""; // clear input password } else if(key == '#') { if(password == input_password) { Serial.println("password is correct"); // DO YOUR WORK HERE } else { Serial.println("password is incorrect, try again"); } input_password = ""; // clear input password } else { input_password += key; // append new character to input password string } } }
  • Chạy code được cung cấp ở trên.
  • Mở Serial Monitor.
  • Nhập các phím "123456" và sau đó nhấn "#".
  • Nhập các phím "1234" và sau đó nhấn "#".
  • Kiểm tra kết quả trên Serial Monitor.
COM6
Send
password is incorrect, try again password is correct
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.