Arduino UNO R4 WiFi Bluetooth Joystick Điều Khiển 2D qua BLE Tutorial

Tổng Quan

Ví dụ Bluetooth Joystick cung cấp điều khiển joystick 2D tương tác 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) để nhận tọa độ X/Y thời gian thực (-100 đến +100) cho việc điều khiển hướng. Hoàn hảo cho điều hướng robot, servo pan-tilt, điều khiển động cơ và bộ điều khiển game.

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. Do bo mạch này sử dụng BLE, ứng dụng hoạt động trên cả Android và iOS.

Arduino UNO R4 WiFi Bluetooth joystick - Điều khiển 2d qua ble tutorial

Tính Năng

  • Điều Khiển 2D: Trục X và Y với phạm vi -100 đến +100
  • Độ Nhạy Có Thể Cấu Hình: Ngưỡng chuyển động tối thiểu để kích hoạt cập nhật
  • Tùy Chọn Tự Động Quay Về: Joystick có thể tự về giữa khi được thả ra
  • Giá Trị Thời Gian Thực: Cập nhật vị trí liên tục trong khi kéo
  • 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 mà không cần ghép nối thủ công
  • Tiết Kiệm Năng Lượng: BLE tiêu thụ ít năng lượng hơn Classic Bluetooth

Phần Cứng Cần Thiết

1×Arduino UNO R4 WiFi
1×Alternatively, DIYables STEM V4 IoT
1×(Tùy chọn) DIYables STEM V4 IoT
1×Cáp USB Type-C
1×breadboard
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Code Arduino UNO R4 WiFi

Các Bước Nhanh

Thực hiện theo các hướng dẫn này từng bước:

  • Nếu đây là lần đầu bạn sử dụng Arduino UNO R4 WiFi, hãy tham khảo Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Kết nối bo mạch Arduino UNO R4 WiFi với máy tính của bạn bằng cáp USB.
  • Khởi động Arduino IDE trên máy tính của bạn.
  • 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 ở 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.
Arduino UNO R4 diyables Bluetooth thư viện
  • Bạn sẽ được yêu cầu 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.
Arduino UNO R4 diyables Bluetooth dependency

Code BLE

  • Trên Arduino IDE, Đi đến File Examples DIYables Bluetooth ArduinoBLE_Joystick, hoặc copy code trên và paste vào editor của Arduino IDE
/* * DIYables Bluetooth Library - Bluetooth Joystick Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Joystick feature: * - Interactive joystick control via Bluetooth * - Real-time X/Y coordinate values (-100 to +100) * - Control pins based on joystick position * * 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 the joystick * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothJoystick.h> #include <platforms/DIYables_ArduinoBLE.h> // BLE Configuration const char* DEVICE_NAME = "Arduino_Joystick"; 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 instances DIYables_ArduinoBLE bluetooth(DEVICE_NAME, SERVICE_UUID, TX_UUID, RX_UUID); DIYables_BluetoothServer bluetoothServer(bluetooth); // Create Joystick app instance // Configure with autoReturn=false and sensitivity=5 (minimum 5% change to trigger updates) DIYables_BluetoothJoystick bluetoothJoystick(false, 5); // Variables to store current joystick values int currentJoystickX = 0; int currentJoystickY = 0; void setup() { Serial.begin(9600); while (!Serial); Serial.println("DIYables Bluetooth - Joystick Example"); // TODO: initialize your hardware pins here // Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin(); // Add joystick app to server bluetoothServer.addApp(&bluetoothJoystick); // Set up connection event callbacks bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); // Set up joystick callback for position changes bluetoothJoystick.onJoystickValue([](int x, int y) { // Store the received values currentJoystickX = x; currentJoystickY = y; // Print joystick position values (-100 to +100) Serial.print("Joystick - X: "); Serial.print(x); Serial.print(", Y: "); Serial.println(y); // TODO: Add your control logic here based on joystick position // Examples: // - Control motors: if (x > 50) { /* move right */ } // - Control servos: servo.write(map(y, -100, 100, 0, 180)); // - Control LEDs: analogWrite(LED_PIN, map(abs(x), 0, 100, 0, 255)); // - Send commands to other devices via Serial, I2C, SPI, etc. }); // Optional: Handle requests for current joystick values (when app loads) bluetoothJoystick.onGetConfig([]() { // Send the stored joystick values back to the app bluetoothJoystick.send(currentJoystickX, currentJoystickY); Serial.print("App requested values - Sent: X="); Serial.print(currentJoystickX); Serial.print(", Y="); Serial.println(currentJoystickY); }); // You can change configuration at runtime: // bluetoothJoystick.setAutoReturn(false); // Disable auto-return // bluetoothJoystick.setSensitivity(10.0); // Only send updates when joystick moves >10% (less sensitive) Serial.println("Waiting for Bluetooth connection..."); } void loop() { // Handle Bluetooth server communications bluetoothServer.loop(); // TODO: Add your main application code here 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:
COM6
Send
DIYables Bluetooth - Joystick Example Waiting for Bluetooth connection...
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Ứng Dụng Di Động

  • Cài đặt ứng dụng DIYables Bluetooth trên smartphone của bạn: Android | iOS

Lưu ý: Ứng dụng DIYables Bluetooth hỗ trợ cả BLE và Classic Bluetooth trên Android, và BLE trên iOS. Do 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 tiên, 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 các 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 của bạn
  • Ở màn hình chính, nhấn nút Connect. Ứng dụng sẽ quét các thiết bị BLE.
diyables Bluetooth app - home screen with scan nút nhấn
  • Tìm và nhấn "Arduino_Joystick" trong kết quả quét để kết nối.
  • Một khi kết nối, ứng dụng tự động quay về màn hình chính. Chọn ứng dụng Joystick từ menu ứng dụng.
diyables Bluetooth app - home screen with joystick app

Lưu ý: Bạn có thể nhấn biểu tượng cài đặt 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 DIYables Bluetooth App.

  • Kéo joystick theo bất kỳ hướng nào
diyables Bluetooth app - joystick screen

Bây giờ hãy nhìn lại Serial Monitor trên Arduino IDE. Bạn sẽ thấy:

COM6
Send
Bluetooth connected! Joystick - X: 50, Y: 0 Joystick - X: 100, Y: 50 Joystick - X: 0, Y: -75 Joystick - X: 0, Y: 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Tùy Chỉnh Sáng Tạo - Điều Chỉnh Code Cho Dự Án Của Bạn

Xử Lý Giá Trị Joystick

bluetoothJoystick.onJoystickValue([](int x, int y) { currentJoystickX = x; currentJoystickY = y; Serial.print("Joystick - X: "); Serial.print(x); Serial.print(", Y: "); Serial.println(y); // TODO: Thêm logic điều khiển của bạn ở đây });

Cấu Hình Cài Đặt Joystick

// Constructor: DIYables_BluetoothJoystick(autoReturn, sensitivity) DIYables_BluetoothJoystick bluetoothJoystick(false, 5); // Thay đổi cài đặt khi chạy bluetoothJoystick.setAutoReturn(true); // Tự về giữa khi thả ra bluetoothJoystick.setSensitivity(10.0); // Chỉ cập nhật khi di chuyển >10%

Xử Lý Yêu Cầu Cấu Hình

bluetoothJoystick.onGetConfig([]() { bluetoothJoystick.send(currentJoystickX, currentJoystickY); });

Ví Dụ Lập Trình

Robot Differential Drive

const int LEFT_MOTOR_PIN = 9; const int RIGHT_MOTOR_PIN = 10; const int LEFT_DIR_PIN = 7; const int RIGHT_DIR_PIN = 8; bluetoothJoystick.onJoystickValue([](int x, int y) { // Differential drive: kết hợp X và Y int leftSpeed = constrain(y + x, -100, 100); int rightSpeed = constrain(y - x, -100, 100); // Đặt hướng digitalWrite(LEFT_DIR_PIN, leftSpeed >= 0 ? HIGH : LOW); digitalWrite(RIGHT_DIR_PIN, rightSpeed >= 0 ? HIGH : LOW); // Đặt tốc độ (PWM) analogWrite(LEFT_MOTOR_PIN, map(abs(leftSpeed), 0, 100, 0, 255)); analogWrite(RIGHT_MOTOR_PIN, map(abs(rightSpeed), 0, 100, 0, 255)); });

Servo Pan-Tilt

#include <Servo.h> Servo panServo, tiltServo; void setup() { panServo.attach(9); tiltServo.attach(10); bluetoothJoystick.onJoystickValue([](int x, int y) { int panAngle = map(x, -100, 100, 0, 180); int tiltAngle = map(y, -100, 100, 0, 180); panServo.write(panAngle); tiltServo.write(tiltAngle); }); }

Hướng với Vùng Chết

const int DEAD_ZONE = 15; bluetoothJoystick.onJoystickValue([](int x, int y) { if (abs(x) < DEAD_ZONE && abs(y) < DEAD_ZONE) { Serial.println("DỪNG"); return; } if (abs(y) > abs(x)) { Serial.println(y > 0 ? "TIẾN" : "LÙI"); } else { Serial.println(x > 0 ? "PHẢI" : "TRÁI"); } });

Khắc Phục Sự Cố

Các Vấn Đề Thường Gặp

1. Không thể tìm thấy thiết bị trong ứng dụng

  • Đảm bảo Arduino UNO R4 WiFi được cấp nguồn và sketch đã được upload
  • Đảm bảo Bluetooth của điện thoại được bật
  • Trên Android 11 trở xuống, cũng bật dịch vụ Vị trí

2. Joystick không phản hồi

  • Kiểm tra trạng thái kết nối Bluetooth trong ứng dụng
  • Xác minh callback onJoystickValue được thiết lập đúng cách
  • Kiểm tra Serial Monitor để xem thông báo kết nối

3. Chuyển động cảm thấy chậm

  • Giảm giá trị sensitivity để có cập nhật thường xuyên hơn
  • Đảm bảo bluetoothServer.loop() được gọi mà không có delay dài

4. Giá trị nhảy hoặc không nhất quán

  • Thêm bộ lọc vùng chết cho các chuyển động nhỏ
  • Kiểm tra cài đặt sensitivity

5. Kết nối bị ngắt thường xuyên

  • Di chuyển gần Arduino hơn (giảm khoảng cách)
  • Đảm bảo nguồn cấp USB ổn định

6. Upload thất bại hoặc bo mạch không được nhận dạng

  • Cài đặt gói bo mạch Arduino UNO R4 mới nhất qua Board Manager
  • Thử cáp USB hoặc cổng khác

Ý Tưởng Dự Án

  • Bộ điều khiển robot không dây
  • Giá đỡ pan-tilt camera
  • Điều khiển 2 trục cho tay robot
  • Bộ điều khiển vị trí ma trận LED
  • Bộ điều khiển game cho các game Arduino

Bước Tiếp Theo

Sau khi thành thạo ví dụ Bluetooth Joystick, hãy thử:

  1. Bluetooth Slider - Để điều khiển giá trị tuyến tính
  2. Bluetooth Rotator - Để điều khiển góc
  3. Bluetooth Monitor - Để phản hồi trạng thái
  4. Nhiều Ứng Dụng Bluetooth - Kết hợp joystick với các điều khiển khác

Hỗ Trợ

Để được trợ giúp thêm: