Arduino Bàn phím Khóa solenoid

Trong bài hướng dẫn này, chúng ta sẽ học cách sử dụng bàn phím số, khóa solenoid 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 solenoid.

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

khóa solenoid có bảng 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×Solenoid 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àn phím số và khóa solenoid

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

Sơ đồ đấu dây

sơ đồ đấu dây cho khóa solenoid điều khiển bằng bàn phím ma trận Arduino

This image is created using Fritzing. Click to enlarge image

Mã Arduino - bật khóa solenoid nếu mật khẩu đúng

Các mã dưới đây sẽ bậ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-solenoid-lock */ #include <Keypad.h> #define RELAY_PIN A0 // the Arduino pin that controls solenoid 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 solenoid 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 máy tính bằng cáp USB
  • Mở Arduino IDE, chọn bo mạch và cổng đúng
  • Điều hướng 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ấn nút Cài đặt để cài đặt thư viện keypad.
thư viện bàn phím cho Arduino
  • Sao chép mã ở phía trên và mở bằng Arduino IDE
  • Nhấp vào nút Tải lên trên Arduino IDE để tải mã lên Arduino
tải mã lên Arduino ide
  • Nhấn phím 7124 và nhấn #
  • Nhấn phím 1234 và nhấn #
  • Xem kết quả trên Màn hình Serial và trạng thái của khóa solenoid
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 trước 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 nhập. Trên bàn phím số, hai phím (*#) được sử dụng cho các 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 nhập
  • Nếu phím được nhấn là *, chuỗi nhập đượ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 nhập được so sánh với các mật khẩu được định sẵn. Nếu nó khớp với một trong các mật khẩu được định sẵn, khóa solenoid sẽ được bật.
    • Dù mật khẩu đúng hay sai, chuỗi nhập sẽ được xóa cho lần nhập tiếp theo.

Mã Arduino - bật khóa solenoid 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 solenoid trong 5 giây nếu mật khẩu đúng. Sau 5 giây, khóa solenoid đượ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-solenoid-lock */ #include <Keypad.h> #include <ezOutput.h> #define UNLOCK_TIME 5000 // in milliseconds #define RELAY_PIN A0 // the Arduino pin that controls solenoid 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 solenoid 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, turning ON relay"); relay.high(); // set high before making a low pulse relay.pulse(UNLOCK_TIME); // deactivate the solenoid 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, điều này làm cho việc quản lý thời gian theo cách không chặn trở nên dễ dàng. 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.