Arduino Cảm biến chạm Khóa solenoid

Chúng ta sẽ học cách sử dụng Arduino để điều khiển khóa solenoid trong hai trường hợp sử dụng:

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×cảm biến chạm
1×Solenoid Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×dây jumper
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ề cảm biến chạm và khóa solenoid

Nếu bạn chưa biết về khóa solenoid và cảm biến chạm (bố trí các chân, 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 đây:

Sơ đồ đấu dây

sơ đồ đấu dây cho Arduino, cảm biến chạm và khóa điện từ

This image is created using Fritzing. Click to enlarge image

Mã Arduino - Cảm biến cảm ứng điều khiển khóa solenoid

/* * 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-touch-sensor-solenoid-lock */ const int TOUCH_SENSOR_PIN = 8; // Arduino pin connected to touch sensor's pin const int RELAY_PIN = A5; // Arduino pin connected to the solenoid lock via relay void setup() { Serial.begin(9600); // initialize serial pinMode(TOUCH_SENSOR_PIN, INPUT); // set arduino pin to input mode pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode } void loop() { int touchState = digitalRead(TOUCH_SENSOR_PIN); // read new state if (touchState == HIGH) { Serial.println("The sensor is being touched"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (touchState == LOW) { Serial.println("The sensor is untouched"); digitalWrite(RELAY_PIN, LOW); // turn off } }

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 board và cổng phù hợp
  • Sao chép mã ở 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
nạp mã vào Arduino ide
  • Chạm và giữ tay lên cảm biến cảm ứng trong một vài giây
  • Quan sát sự thay đổi trạng thái của khóa điện từ

Giải thích mã nguồn

Đọc lời giải thích theo từng dòng trong các chú thích của mã nguồn!

Mã Arduino - Cảm biến chạm bật/tắt khóa solenoid

/* * 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-touch-sensor-solenoid-lock */ const int TOUCH_SENSOR_PIN = 8; // Arduino pin connected to touch sensor's pin const int RELAY_PIN = A5; // Arduino pin connected to solenoid lock via relay // variables will change: int solenoidLockState = LOW; // the current state of relay int lastTouchState; // the previous state of touch sensor int currentTouchState; // the current state of touch sensor void setup() { Serial.begin(9600); // initialize serial pinMode(TOUCH_SENSOR_PIN, INPUT); // set arduino pin to input mode pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode currentTouchState = digitalRead(TOUCH_SENSOR_PIN); } void loop() { lastTouchState = currentTouchState; // save the last state currentTouchState = digitalRead(TOUCH_SENSOR_PIN); // read new state if(lastTouchState == LOW && currentTouchState == HIGH) { Serial.println("The sensor is touched"); // toggle state of relay solenoidLockState = !solenoidLockState; // control relay arccoding to the toggled state digitalWrite(RELAY_PIN, solenoidLockState); } }

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 bảng mạch và cổng đúng
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấn nút Upload trên Arduino IDE để tải mã lên Arduino
  • Giữ cảm biến chạm trong vài giây, rồi thả ra
  • Xem sự thay đổi trạng thái của khóa solenoid

Giải thích mã nguồn

Bạn có thể tìm lời giải thích trong dòng chú thích của mã Arduino ở trên.

Trong mã nguồn, solenoidLockState = !solenoidLockState tương đương với đoạn mã sau:

if(solenoidLockState == LOW) solenoidLockState = HIGH; else solenoidLockState = LOW;

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.