Arduino Ma trận phím Rơ-le

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

Hướng dẫn cũng cung cấp mã để bật một rơ-le trong một khoảng thời gian và sau đó tắt mà không sử dụng hàm delay(). Mã Arduino cũng hỗ trợ nhiều mật khẩu.

Bằng cách kết nối rơ-le với Arduino - Khóa điện từ, Arduino - Khóa Solenoid, , Arduino - Thiết bị chấp hành, Arduino - Điều khiển phần tử gia nhiệt, Arduino - Điều khiển bơm, hoặc Arduino - Điều khiển quạt ... Chúng ta có thể điều khiển chúng bằng bàn phím.

rơ-le 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 and 4x4 Kit
1×Relay
1×dây jumper
1×(Tùy chọn) Solenoid Lock
1×(Tùy chọn) 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 và Rơ-le

Nếu bạn không biết về bàn phím ma trận và Relay (bản đồ các chân, cách nó 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ơ đồ đấu nối rơ-le cho bàn phím Arduino

This image is created using Fritzing. Click to enlarge image

Mã Arduino - bật rơ-le khi mật khẩu đúng

Các mã dưới đây bật rơ-le 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-relay */ #include <Keypad.h> #define RELAY_PIN A0 // the Arduino pin, which connects to the IN pin of relay #define ROW_NUM 4 // four rows #define COLUMN_NUM 4 // four columns char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; 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, 2}; //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 = "ABC1234"; // change your password here const String password_2 = "5642B"; // change your password here const String password_3 = "9765"; // 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, LOW); } 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, turning ON relay"); digitalWrite(RELAY_PIN, HIGH); } 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 đến biểu tượng Libraries ở 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 vào nút Install để cài đặt thư viện keypad.
thư viện bàn phím Arduino
  • Tìm kiếm “ezOutput”, sau đó tìm thư viện ezOutput của ArduinoGetStarted
  • Nhấn nút Cài đặt để cài đặt thư viện ezOutput.
thư viện đầu ra của Arduino
  • Sao chép mã ở 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 các phím 1234 và nhấn #
  • Nhấn các phím 9765 và nhấn #
  • Xem kết quả trên Trình theo dõi Serial và trạng thái của rơ-le
COM6
Send
The password is incorrect, try again The password is correct, turning ON relay
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 quá trình nhập 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à một trong 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 làm trống. 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 trước. Nếu nó khớp với một trong các mật khẩu được định trước, rơ-le được bật.
    • Dù mật khẩu đúng hay sai, chuỗi đầu vào được làm trống cho lần nhập tiếp theo.

Mã Arduino - bật rơ-le trong một khoảng thời gian nếu mật khẩu đúng

Đoạn mã dưới đây bật rơ-le trong 5 giây nếu mật khẩu đúng. Sau 5 giây, rơ-le đượ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-relay */ #include <Keypad.h> #include <ezOutput.h> #define RELAY_PIN A0 // the Arduino pin, which connects to the IN pin of relay #define ROW_NUM 4 // four rows #define COLUMN_NUM 4 // four columns #define RELAY_ON_TIME 5000 // in milliseconds char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; 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, 2}; //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 = "ABC1234"; // change your password here const String password_2 = "5642B"; // change your password here const String password_3 = "9765"; // 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.low(); } 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.low(); // set low before making a high pulse relay.pulse(RELAY_ON_TIME); // turn on relay in RELAY_ON_TIME duration and then turn off } 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 } } }

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.