Arduino UNO R4 WiFi Bluetooth Digital Pins Example Hướng dẫn Điều khiển GPIO qua BLE
Tổng Quan
Ví dụ Bluetooth Digital Pins cung cấp khả năng điều khiển và giám sát chân GPIO từ xa thông qua ứng dụng DIYables Bluetooth STEM. Được thiết kế cho Arduino UNO R4 WiFi sử dụng BLE (Bluetooth Low Energy) để điều khiển chân output và giám sát chân input không dây từ điện thoại thông minh của bạn. Hoàn hảo cho việc điều khiển relay, giám sát nút nhấn, chuyển đổi LED và bất kỳ ứng dụng nào cần truy cập chân từ xa.
Lưu ý: Arduino UNO R4 WiFi chỉ hỗ trợ BLE (Bluetooth Low Energy). Nó không hỗ trợ Classic Bluetooth. Ứng dụng DIYables Bluetooth hỗ trợ cả BLE và Classic Bluetooth trên Android, và BLE trên iOS. Vì bo mạch này sử dụng BLE, ứng dụng hoạt động trên cả Android và iOS.
Tính Năng
Điều khiển Output: Đặt chân digital HIGH/LOW từ xa
Giám sát Input: Đọc trạng thái chân digital và analog
Đặt Tên Chân: Gán tên thân thiện cho mỗi chân (ví dụ: "LED", "Relay")
Cập Nhật Thời Gian Thực: Đẩy thay đổi trạng thái chân đến ứng dụng
Lên đến 16 Chân: Điều khiển nhiều chân cùng lúc
Hoạt động trên Android & iOS: BLE được hỗ trợ trên cả hai nền tảng
Không Cần Ghép Nối: BLE tự động kết nối không cần ghép nối thủ công
Kết nối bo mạch Arduino UNO R4 WiFi với máy tính bằng cáp USB.
Khởi động Arduino IDE trên máy tính.
Chọn bo mạch Arduino UNO R4 WiFi và cổng COM phù hợp.
Điều hướng đến biểu tượng Libraries trên thanh bên trái của Arduino IDE.
Tìm kiếm "DIYables Bluetooth", sau đó tìm thư viện DIYables Bluetooth của DIYables
Nhấn nút Install để cài đặt thư viện.
Bạn sẽ được hỏi về việc cài đặt một số thư viện phụ thuộc khác
Nhấn nút Install All để cài đặt tất cả thư viện phụ thuộc.
Code BLE
Trên Arduino IDE, đi tới File Examples DIYables Bluetooth ArduinoBLE_PinControl example, hoặc copy code trên và dán vào editor của Arduino IDE
/* * DIYables Bluetooth Library - Bluetooth Pin Control/Monitor Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Pin Control/Monitor feature: * - Control digital output pins via Bluetooth * - Monitor digital input pins * - Monitor analog input pins * - Configure pin modes (INPUT/OUTPUT) * * Compatible Boards: * - Arduino UNO R4 WiFi * - Arduino Nano 33 BLE / BLE Sense * - Arduino Nano 33 IoT * - Arduino MKR WiFi 1010 * - Arduino Nano RP2040 Connect * - Any board supporting the ArduinoBLE library * * Setup: * 1. Upload the sketch to your Arduino * 2. Open Serial Monitor to see connection status * 3. Use DIYables Bluetooth App to connect and control pins * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */#include <DIYables_BluetoothServer.h>#include <DIYables_BluetoothPinControl.h>#include <platforms/DIYables_ArduinoBLE.h>// BLE Configurationconst char* DEVICE_NAME = "Arduino_Pins";const char* SERVICE_UUID = "19B10000-E8F2-537E-4F6C-D104768A1214";const char* TX_UUID = "19B10001-E8F2-537E-4F6C-D104768A1214";const char* RX_UUID = "19B10002-E8F2-537E-4F6C-D104768A1214";// Create Bluetooth instancesDIYables_ArduinoBLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID);DIYables_BluetoothServer bluetoothServer(bluetooth);// Create Pin Control/Monitor app instanceDIYables_BluetoothPinControl bluetoothPins;// Pin configurationconstint LED_PIN = 13;constint OUTPUT_PIN_1 = 12;constint OUTPUT_PIN_2 = 11;constint INPUT_PIN_1 = 7;constint INPUT_PIN_2 = 6;constint ANALOG_PIN_1 = A0;constint ANALOG_PIN_2 = A1;voidsetup() {Serial.begin(9600);while (!Serial);Serial.println("DIYables Bluetooth - Pin Control/Monitor Example");// Initialize pinspinMode(LED_PIN, OUTPUT);pinMode(OUTPUT_PIN_1, OUTPUT);pinMode(OUTPUT_PIN_2, OUTPUT);pinMode(INPUT_PIN_1, INPUT_PULLUP);pinMode(INPUT_PIN_2, INPUT_PULLUP);// Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin();// Add digital pins app to server bluetoothServer.addApp(&bluetoothPins);// Configure which pins are accessible via Bluetooth with custom names bluetoothPins.enablePin(LED_PIN, BT_PIN_OUTPUT, "LED"); bluetoothPins.enablePin(OUTPUT_PIN_1, BT_PIN_OUTPUT, "Out1"); bluetoothPins.enablePin(OUTPUT_PIN_2, BT_PIN_OUTPUT, "Out2"); bluetoothPins.enablePin(INPUT_PIN_1, BT_PIN_INPUT, "Btn1"); bluetoothPins.enablePin(INPUT_PIN_2, BT_PIN_INPUT, "Btn2"); bluetoothPins.enablePin(ANALOG_PIN_1, BT_PIN_INPUT, "A0"); bluetoothPins.enablePin(ANALOG_PIN_2, BT_PIN_INPUT, "A1");// Set up connection event callbacks bluetoothServer.setOnConnected([]() {Serial.println("Bluetooth connected!"); }); bluetoothServer.setOnDisconnected([]() {Serial.println("Bluetooth disconnected!"); });// Set up callback for pin write commands bluetoothPins.onPinWrite([](int pin, int state) {digitalWrite(pin, state);Serial.print("Pin ");Serial.print(pin);Serial.print(" set to ");Serial.println(state ? "HIGH" : "LOW"); });// Set up callback for pin read commands bluetoothPins.onPinRead([](int pin) -> int {// Read analog pins with analogRead, digital pins with digitalReadint state;if (pin == ANALOG_PIN_1 || pin == ANALOG_PIN_2) { state = analogRead(pin);Serial.print("Analog pin ");Serial.print(pin);Serial.print(" read: ");Serial.println(state); } else { state = digitalRead(pin);Serial.print("Digital pin ");Serial.print(pin);Serial.print(" read: ");Serial.println(state ? "HIGH" : "LOW"); }return state; });// Set up callback for pin mode changes bluetoothPins.onPinModeChange([](int pin, int mode) {pinMode(pin, mode == BT_PIN_OUTPUT ? OUTPUT : INPUT_PULLUP);Serial.print("Pin ");Serial.print(pin);Serial.print(" mode changed to ");Serial.println(mode == BT_PIN_OUTPUT ? "OUTPUT" : "INPUT"); });Serial.println("Waiting for Bluetooth connection...");Serial.print("Enabled pins: ");Serial.println(bluetoothPins.getEnabledPinCount());}voidloop() {// Handle Bluetooth server communications bluetoothServer.loop();// Optional: Monitor input pins and send updatesstaticunsignedlong lastInputCheck = 0;staticint lastInputState1 = HIGH;staticint lastInputState2 = HIGH;staticint lastAnalogState1 = 0;staticint lastAnalogState2 = 0;if (millis() - lastInputCheck >= 100) { lastInputCheck = millis();// Check digital input pin 1int currentState1 = digitalRead(INPUT_PIN_1);if (currentState1 != lastInputState1) { lastInputState1 = currentState1; bluetoothPins.updatePinState(INPUT_PIN_1, currentState1);Serial.print("Input pin ");Serial.print(INPUT_PIN_1);Serial.print(" changed to ");Serial.println(currentState1 ? "HIGH" : "LOW"); }// Check digital input pin 2int currentState2 = digitalRead(INPUT_PIN_2);if (currentState2 != lastInputState2) { lastInputState2 = currentState2; bluetoothPins.updatePinState(INPUT_PIN_2, currentState2);Serial.print("Input pin ");Serial.print(INPUT_PIN_2);Serial.print(" changed to ");Serial.println(currentState2 ? "HIGH" : "LOW"); }// Check analog input 1 (send update if changed by more than 10)int currentAnalog1 = analogRead(ANALOG_PIN_1);if (abs(currentAnalog1 - lastAnalogState1) > 10) { lastAnalogState1 = currentAnalog1; bluetoothPins.updatePinState(ANALOG_PIN_1, currentAnalog1);Serial.print("Analog pin ");Serial.print(ANALOG_PIN_1);Serial.print(" changed to ");Serial.println(currentAnalog1); }// Check analog input 2 (send update if changed by more than 10)int currentAnalog2 = analogRead(ANALOG_PIN_2);if (abs(currentAnalog2 - lastAnalogState2) > 10) { lastAnalogState2 = currentAnalog2; bluetoothPins.updatePinState(ANALOG_PIN_2, currentAnalog2);Serial.print("Analog pin ");Serial.print(ANALOG_PIN_2);Serial.print(" changed to ");Serial.println(currentAnalog2); } }delay(10);}
Nhấn nút Upload trên Arduino IDE để upload code lên Arduino UNO R4 WiFi
Mở Serial Monitor
Kiểm tra kết quả trên Serial Monitor. Nó sẽ trông như sau:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
DIYables Bluetooth - Pin Control/Monitor Example
Waiting for Bluetooth connection...
Enabled pins: 7
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
Ứng Dụng Di Động
Cài đặt ứng dụng DIYables Bluetooth trên điện thoại thông minh: Android | iOS
Lưu ý: Ứng dụng DIYables Bluetooth hỗ trợ cả BLE và Classic Bluetooth trên Android, và BLE trên iOS. Vì Arduino UNO R4 WiFi sử dụng BLE, ứng dụng hoạt động trên cả Android và iOS. Không cần ghép nối thủ công cho BLE — chỉ cần quét và kết nối.
Mở ứng dụng DIYables Bluetooth
Khi mở ứng dụng lần đầu, nó sẽ yêu cầu quyền. Vui lòng cấp các quyền sau:
Quyền Nearby Devices (Android 12+) / Quyền Bluetooth (iOS) - cần thiết để quét và kết nối với thiết bị Bluetooth
Quyền Location (chỉ Android 11 trở xuống) - được yêu cầu bởi các phiên bản Android cũ để quét thiết bị BLE
Đảm bảo Bluetooth đã được bật trên điện thoại
Trên màn hình chính, chạm vào nút Connect. Ứng dụng sẽ quét thiết bị BLE.
Tìm và chạm vào "Arduino_Pins" trong kết quả quét để kết nối.
Sau khi kết nối, ứng dụng tự động quay về màn hình chính. Chọn ứng dụng Digital Pins từ menu ứng dụng.
Lưu ý: Bạn có thể chạm vào biểu tượng settings trên màn hình chính để ẩn/hiện các ứng dụng trên màn hình chính. Để biết thêm chi tiết, xem Hướng dẫn Sử dụng Ứng dụng DIYables Bluetooth.
Bạn sẽ thấy danh sách các chân đã kích hoạt với tên và trạng thái hiện tại của chúng
Chạm vào chân output để chuyển đổi HIGH/LOW, và xem giá trị chân input được cập nhật
Bây giờ hãy nhìn lại Serial Monitor trên Arduino IDE. Bạn sẽ thấy:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Bluetooth connected!
Pin 13 set to HIGH
Pin 13 set to LOW
Digital pin 7 read: HIGH
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
Tùy Chỉnh Sáng Tạo - Điều chỉnh Code theo Dự án của Bạn