Arduino Bảng phím Khóa điện từ

Trong bài hướng dẫn này, chúng ta sẽ học cách sử dụng bàn phím ma trận, khóa điện từ và Arduino cùng nhau. Cụ thể, nếu người dùng nhập đúng mật khẩu trên bàn phím, Arduino sẽ bật khóa điện từ.

Bài hướng dẫn cũng cung cấp mã để kích hoạt khóa điện từ trong một khoảng thời gian và sau đó tắt đi mà không dùng hàm delay(). Mã Arduino cũng hỗ trợ nhiều mật khẩu.

khóa điện từ bàn phím Arduino

Phần cứng cần chuẩn bị

1×Arduino Uno R3
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Bàn phím ma trận 3x4
1×Relay
1×dây jumper
1×Electromagnetic Lock
1×12V Power Adapter
1×(Tùy chọn) DC Power Jack
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino Uno
1×(Khuyến nghị) Breadboard Shield for Arduino Uno
1×(Khuyến nghị) Enclosure for Arduino Uno
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

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

Về bảng phím và khóa điện từ

Nếu bạn chưa biết về bàn phím số và khóa từ điện từ (cấu hình chân pin, cách hoạt động, cách lập trình ...), hãy tìm hiểu về chúng trong các bài hướng dẫn sau:

Sơ đồ đấu dây

sơ đồ nối dây khóa từ điện từ với Arduino và bàn phím

This image is created using Fritzing. Click to enlarge image

Mã Arduino - bật khóa nam châm điện khi mật khẩu đúng

Các mã dưới đây sẽ mở một khóa điện từ nếu mật khẩu đúng.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino 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/arduino-keypad-electromagnetic-lock */ #include <Keypad.h> #define RELAY_PIN A0 // the Arduino pin that controls electromagnetic lock via relay #define ROW_NUM 4 // four rows #define 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 Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password_1 = "1234"; // change your password here const String password_2 = "56789"; // change your password here const String password_3 = "901234"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum password size is 32, change if needed pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, HIGH); // lock the electromagnetic lock } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); if (key == '*') { input_password = ""; // reset the input password } else if (key == '#') { if (input_password == password_1 || input_password == password_2 || input_password == password_3) { Serial.println("The password is correct => unlock"); digitalWrite(RELAY_PIN, LOW); } else { Serial.println("The password is incorrect, try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Hướng dẫn từng bước

  • Kết nối Arduino với PC bằng cáp USB
  • Mở Arduino IDE, chọn đúng board và cổng
  • Đi tới biểu tượng Thư viện ở thanh bên trái của Arduino IDE.
  • Tìm kiếm “keypad”, sau đó tìm thư viện keypad do Mark Stanley, Alexander Brevig.
  • Nhấp nút Cài đặt để cài đặt thư viện keypad.
thư viện bàn phím ma trận Arduino
  • Sao chép mã ở phía trên và mở bằng Arduino IDE
  • Nhấn nút Tải lên trên Arduino IDE để nạp mã lên Arduino
tải mã lên Arduino ide
  • Nhấn các phím 8123 và nhấn #
  • Nhấn các phím 1234 và nhấn #
  • Xem kết quả trên Serial Monitor và trạng thái của khóa điện từ
COM6
Send
The password is incorrect, try again The password is correct => unlock
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Giải thích mã nguồn

Các mật khẩu được cấp quyền đã được định nghĩa sẵn trong mã Arduino.

Một chuỗi được dùng để lưu mật khẩu do người dùng nhập vào, được gọi là chuỗi đầu vào. Trên bàn phím, hai phím (*#) được sử dụng cho mục đích đặc biệt: xóa mật khẩu và kết thúc mật khẩu. Khi một phím trên bàn phím được nhấn:

  • Nếu phím được nhấn không phải là hai phím đặc biệt, nó được thêm vào chuỗi đầu vào
  • Nếu phím được nhấn là *, chuỗi đầu vào được xóa. Bạn có thể dùng nó để bắt đầu hoặc nhập lại mật khẩu.
  • Nếu phím được nhấn là #:
    • chuỗi đầu vào được so sánh với các mật khẩu được định nghĩa sẵn. Nếu nó khớp với một trong các mật khẩu được định nghĩa sẵn, khóa điện từ được bật.
    • Dù mật khẩu đúng hay sai, chuỗi đầu vào được xóa cho lần nhập tiếp theo.

Mã Arduino - bật khóa điện từ trong một khoảng thời gian nếu mật khẩu đúng

Đoạn mã dưới đây sẽ bật khóa điện từ trong 5 giây nếu mật khẩu đúng. Sau 5 giây, khóa điện từ sẽ được tắt.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino 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/arduino-keypad-electromagnetic-lock */ #include <Keypad.h> #include <ezOutput.h> #define UNLOCK_TIME 5000 // in milliseconds #define RELAY_PIN A0 // the Arduino pin that controls electromagnetic lock via relay #define ROW_NUM 4 // four rows #define 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 Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); ezOutput relay(RELAY_PIN); const String password_1 = "1234"; // change your password here const String password_2 = "56789"; // change your password here const String password_3 = "901234"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum password size is 32, change if needed relay.high(); // lock the electromagnetic lock } void loop() { relay.loop(); // MUST call the loop() function first char key = keypad.getKey(); if (key) { Serial.println(key); if (key == '*') { input_password = ""; // reset the input password } else if (key == '#') { if (input_password == password_1 || input_password == password_2 || input_password == password_3) { Serial.println("The password is correct => unlock"); relay.high(); // set high before making a low pulse relay.pulse(UNLOCK_TIME); // deactivate the electromagnetic lock during UNLOCK_TIME duration } else { Serial.println("The password is incorrect, try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Xin lưu ý rằng đoạn mã ở trên sử dụng thư viện ezOutput, giúp quản lý thời gian một cách không chặn. Bạn có thể tham khảo Hướng dẫn cài đặt thư viện ezOutput.

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.