Arduino Nút Bật/Tắt Đèn LED

Trong Arduino - Nút nhấn - LED, chúng ta đã học cách bật LED khi nút được nhấn, và tắt LED khi nút được thả ra. Trong bài hướng dẫn này, chúng ta sẽ học cách đảo trạng thái LED mỗi lần nút được nhấn.

Bài hướng dẫn gồm hai phần chính:

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×breadboard-mount Button with Cap
1×breadboard-mount Button Kit
1×Panel-mount Push Button
1×mô-đun nút nhấn
1×LED Kit
1×LED (red)
1×LED Module
1×220Ω Resistor
1×breadboard
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ề LED và nút nhấn

Nếu bạn chưa biết về LED và nút nhấn (bố trí các chân, cách chúng 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ơ đồ nối dây LED và nút cho Arduino

This image is created using Fritzing. Click to enlarge image

Mã Arduino - Nút nhấn bật/tắt LED mà không khử nhiễu

/* * 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-button-toggle-led */ // constants won't change const int BUTTON_PIN = 7; // Arduino pin connected to button's pin const int LED_PIN = 3; // Arduino pin connected to LED's pin // variables will change: int ledState = LOW; // the current state of LED int lastButtonState; // the previous state of button int currentButtonState; // the current state of button void setup() { Serial.begin(9600); // initialize serial pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode currentButtonState = digitalRead(BUTTON_PIN); } void loop() { lastButtonState = currentButtonState; // save the last state currentButtonState = digitalRead(BUTTON_PIN); // read new state if(lastButtonState == HIGH && currentButtonState == LOW) { Serial.println("The button is pressed"); // toggle state of LED ledState = !ledState; // control LED arccoding to the toggled state digitalWrite(LED_PIN, ledState); } }

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 đúng
  • 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
  • Giữ nút trong vài giây, rồi thả ra
  • Quan sát sự thay đổi trạng thái của đèn LED

Giải thích mã nguồn

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

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

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

※ Lưu ý:

Trong thực tế, mã ở trên đôi khi không hoạt động đúng. Để nó luôn hoạt động đúng, chúng ta cần Arduino - Nút nhấn - Khử rung. Việc debounce cho nút không dễ đối với người mới bắt đầu. May mắn thay, nhờ thư viện ezButton, chúng ta có thể làm điều đó một cách dễ dàng.

Mã Arduino - Nút nhấn bật/tắt LED với chống nhiễu

Tại sao chúng ta cần debounce? ⇒ xem Arduino - Nút nhấn - Khử rung

/* * 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-button-toggle-led */ #include <ezButton.h> /// constants won't change const int BUTTON_PIN = 7; // the number of the pushbutton pin const int LED_PIN = 3; // the number of the LED pin ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7; // variables will change: int ledState = LOW; // the current state of LED void setup() { Serial.begin(9600); // initialize serial pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) { Serial.println("The button is pressed"); // toggle state of LED ledState = !ledState; // control LED arccoding to the toggleed sate digitalWrite(LED_PIN, ledState); } }

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

  • Cài đặt thư viện ezButton. Xem Hướng dẫn
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấn nút Tải lên trên Arduino IDE để tải mã lên Arduino
  • Nhấn nút một vài lần
  • Xem sự thay đổi trạng thái của LED

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.