Arduino UNO R4 WiFi Bluetooth Đồng Hồ Đo Analog Hướng Dẫn Hiển Thị Đồng Hồ Đo qua BLE

Tổng Quan

Ví dụ Bluetooth Analog Gauge (Đồng hồ đo Analog) cung cấp màn hình hiển thị đồng hồ đo kiểu analog trực quan 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) — hiển thị bất kỳ giá trị analog nào trên đồng hồ đo đẹp mắt với phạm vi và đơn vị có thể cấu hình trên smartphone của bạn. Hoàn hảo cho đồng hồ tốc độ, đồng hồ áp suất, màn hình RPM và bất kỳ giá trị nào có lợi từ việc hiển thị kiểu mặt số.

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

Arduino UNO R4 WiFi Bluetooth Đồng hồ Đo analog - hướng dẫn hiển thị Đồng hồ Đo qua ble

Tính Năng

  • Hiển Thị Đồng Hồ Đo Analog: Đồng hồ đo kiểu mặt số đẹp mắt trên smartphone
  • Phạm Vi Có Thể Cấu Hình: Đặt giá trị tối thiểu và tối đa
  • Đơn Vị Tùy Chỉnh: Hiển thị km/h, RPM, PSI hoặc bất kỳ đơn vị tùy chỉnh nào
  • Cập Nhật Nhanh: Lên đến 5 lần cập nhật mỗi giây (khoảng cách 200ms)
  • Yêu Cầu Theo Nhu Cầu: Ứng dụng có thể yêu cầu giá trị hiện tại
  • 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

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×(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

Làm theo hướng dẫn từng bước sau:

  • Nếu đây là lần đầu tiên 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 board 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 board Arduino UNO R4 WiFi và COM port 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ấp vào nút Install để cài đặt thư viện.
Arduino UNO R4 diyables Bluetooth 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ấp vào 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_AnalogGauge example, hoặc sao chép code trên và dán vào editor của Arduino IDE
/* * DIYables Bluetooth Library - Bluetooth Analog Gauge Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Analog Gauge feature: * - Display values on an analog meter/gauge * - Configurable range and unit * - Perfect for sensor monitoring (speed, pressure, voltage, etc.) * * 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 * * Optional: Analog sensor (potentiometer, pressure sensor, etc.) * * Setup: * 1. Upload the sketch to your Arduino * 2. Open Serial Monitor to see connection status * 3. Use DIYables Bluetooth App to connect and view the gauge * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothAnalogGauge.h> #include <platforms/DIYables_ArduinoBLE.h> // BLE Configuration const char* DEVICE_NAME = "Arduino_Gauge"; 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 Analog Gauge app instance (min=0, max=100, unit="km/h") DIYables_BluetoothAnalogGauge bluetoothGauge(0.0, 100.0, "km/h"); // Variables for gauge value float currentValue = 0.0; unsigned long lastUpdate = 0; const unsigned long UPDATE_INTERVAL = 200; // Update every 200ms // Optional: Analog input pin for sensor const int ANALOG_PIN = A0; // Function to read sensor value float readSensorValue() { // TODO: Replace with actual sensor reading // Examples: // - Pressure sensor: readPressure() // - Voltage sensor: analogRead(A0) * (5.0 / 1023.0) // - Speed sensor: calculateSpeed() // Option 1: Read from analog pin and map to gauge range // int rawValue = analogRead(ANALOG_PIN); // return map(rawValue, 0, 1023, 0, 100); // Option 2: Simulated data (sine wave) static float phase = 0; phase += 0.05; if (phase > 2 * PI) phase = 0; return 50 + 50 * sin(phase); // Oscillates between 0-100 } void setup() { Serial.begin(9600); while (!Serial); Serial.println("DIYables Bluetooth - Analog Gauge Example"); // Optional: Initialize analog pin // pinMode(ANALOG_PIN, INPUT); // Initialize Bluetooth server with platform-specific implementation bluetoothServer.begin(); // Add gauge app to server bluetoothServer.addApp(&bluetoothGauge); // Set up connection event callbacks bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); // Send initial value currentValue = readSensorValue(); bluetoothGauge.send(currentValue); Serial.print("Initial value sent: "); Serial.print(currentValue); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); // Optional: Handle requests for current value bluetoothGauge.onValueRequest([]() { currentValue = readSensorValue(); bluetoothGauge.send(currentValue); Serial.print("Value requested - Sent: "); Serial.print(currentValue); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); }); // You can change gauge configuration at runtime: // bluetoothGauge.setRange(0.0, 200.0); // Change range to 0-200 // bluetoothGauge.setUnit("mph"); // Change unit to mph // bluetoothGauge.setRange(0.0, 5.0); // For voltage (0-5V) // bluetoothGauge.setUnit("V"); Serial.println("Waiting for Bluetooth connection..."); Serial.print("Gauge range: "); Serial.print(bluetoothGauge.getMin()); Serial.print(" - "); Serial.print(bluetoothGauge.getMax()); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); } void loop() { // Handle Bluetooth server communications bluetoothServer.loop(); // Send gauge updates periodically (only when connected) if (bluetooth.isConnected() && millis() - lastUpdate >= UPDATE_INTERVAL) { lastUpdate = millis(); // Read sensor value currentValue = readSensorValue(); // Send to Bluetooth app bluetoothGauge.send(currentValue); // Print to Serial Monitor Serial.print("Gauge: "); Serial.print(currentValue, 1); Serial.print(" "); Serial.println(bluetoothGauge.getUnit()); } delay(10); }
  • Nhấp vào 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ư thế này:
COM6
Send
DIYables Bluetooth - Analog Gauge 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. Vì Arduino UNO R4 WiFi sử dụng BLE nên ứ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 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
  • Trê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_Gauge" 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 Analog Gauge từ menu ứng dụng.
diyables Bluetooth app - home screen with analog gauge app

Lưu ý: Bạn có thể nhấn vào 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 Ứng Dụng DIYables Bluetooth.

  • Bạn sẽ thấy đồng hồ đo analog hiển thị các giá trị với chuyển động kim mượt mà, mô phỏng đồng hồ tốc độ
diyables Bluetooth app - analog gauge 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! Gauge value: 50.00 km/h Gauge value: 59.76 km/h Gauge value: 68.78 km/h
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

Cấu Hình Phạm Vi và Đơn Vị Đồng Hồ Đo

// Đồng hồ tốc độ: 0-200 km/h DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 200.0, "km/h"); // Đồng hồ áp suất: 0-100 PSI DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 100.0, "PSI"); // Đồng hồ RPM: 0-8000 RPM DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 8000.0, "RPM"); // Phần trăm: 0-100% DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 100.0, "%");

Gửi Giá Trị Đồng Hồ Đo

// Gửi một giá trị đến đồng hồ đo bluetoothGauge.send(75.5); // Đọc từ cảm biến và gửi float sensorValue = analogRead(A0) * (100.0 / 1023.0); bluetoothGauge.send(sensorValue);

Xử Lý Yêu Cầu Giá Trị

bluetoothGauge.onValueRequest([]() { float value = readSensor(); bluetoothGauge.send(value); Serial.print("Requested: "); Serial.println(value); });

Ví Dụ Lập Trình

Đồng Hồ Đo Potentiometer

DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 100.0, "%"); void loop() { bluetoothServer.loop(); static unsigned long lastTime = 0; if (millis() - lastTime >= 200) { lastTime = millis(); float percent = analogRead(A0) * (100.0 / 1023.0); bluetoothGauge.send(percent); } }

Màn Hình Mức Pin

DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 100.0, "%"); float readBatteryLevel() { float voltage = analogRead(A0) * (5.0 / 1023.0) * 2; // voltage divider float percent = map(voltage * 100, 300, 420, 0, 100); return constrain(percent, 0, 100); } void loop() { bluetoothServer.loop(); static unsigned long lastTime = 0; if (millis() - lastTime >= 1000) { lastTime = millis(); bluetoothGauge.send(readBatteryLevel()); } }

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 bật nguồn và sketch đã được upload
  • Đảm bảo Bluetooth của điện thoại bạn được bật
  • Trên Android 11 trở xuống, cũng bật dịch vụ Location

2. Đồng hồ đo không cập nhật

  • Kiểm tra xem send() có được gọi trong vòng lặp không
  • Xác minh thời gian khoảng cách cập nhật
  • Đảm bảo bluetoothServer.loop() được gọi

3. Đồng hồ đo hiển thị phạm vi sai

  • Kiểm tra giá trị min/max trong constructor
  • Đảm bảo chuỗi đơn vị đúng
  • Các giá trị ngoài phạm vi sẽ bị giới hạn

4. Kim nhảy không đều

  • Thêm làm mượt hoặc lấy trung bình cho các đọc cảm biến
  • Giảm tần suất cập nhật nếu cần
  • Kiểm tra đầu vào analog có nhiễu

5. Upload thất bại hoặc board không được nhận diện

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

Ý Tưởng Dự Án

  • Đồng hồ tốc độ cho xe RC
  • Đồng hồ áp suất cho hệ thống pneumatic
  • Chỉ báo mức pin
  • Đồng hồ RPM cho motor (động cơ)
  • Đồng hồ đo cường độ tín hiệu

Các Bước Tiếp Theo

Sau khi làm chủ ví dụ Bluetooth Analog Gauge, hãy thử:

  1. Bluetooth Temperature - Cho đồng hồ đo nhiệt độ cụ thể
  2. Bluetooth Plotter - Cho dữ liệu theo thời gian
  3. Bluetooth Slider - Cho việc đặt giá trị ngược lại Arduino
  4. Nhiều Ứng Dụng Bluetooth - Kết hợp đồng hồ đo với các ứng dụng khác

Hỗ Trợ

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