아두이노 우노 R4 WiFi 블루투스 아날로그 게이지 예제 BLE를 통한 게이지 표시 튜토리얼

개요

블루투스 아날로그 게이지 예제는 DIYables 블루투스 STEM 앱을 통해 시각적 아날로그 스타일 게이지 디스플레이를 제공합니다. BLE(Bluetooth Low Energy) 를 사용하는 Arduino UNO R4 WiFi 전용으로 설계되었으며, 스마트폰에서 구성 가능한 범위와 단위로 아름다운 게이지에 모든 아날로그 값을 표시합니다. 스피드미터, 압력 게이지, RPM 표시 및 다이얼 스타일 시각화의 이점이 있는 모든 값에 적합합니다.

참고: Arduino UNO R4 WiFi는 BLE(Bluetooth Low Energy)만 지원합니다. 클래식 블루투스는 지원하지 않습니다. DIYables 블루투스 앱은 Android에서 BLE와 클래식 블루투스를 모두 지원하고, iOS에서는 BLE를 지원합니다. 이 보드는 BLE를 사용하므로 앱은 Android와 iOS 모두에서 작동합니다.

아두이노 우노 R4 와이파이 블루투스 아날로그 게이지 예제 - BLE를 통한 게이지 표시 튜토리얼

기능

  • 아날로그 게이지 표시: 스마트폰에서 아름다운 다이얼 스타일 게이지
  • 구성 가능한 범위: 최소 및 최대 값 설정
  • 사용자 지정 단위: km/h, RPM, PSI 또는 임의의 단위 표시
  • 빠른 업데이트: 초당 최대 5회 업데이트(200ms 간격)
  • 요청 시 응답: 앱에서 현재 값 요청 가능
  • Android 및 iOS에서 작동: BLE는 두 플랫폼 모두에서 지원
  • 페어링 불필요: BLE는 수동 페어링 없이 자동 연결

필요한 하드웨어

1×아두이노 우노 R4 와이파이 쿠팡 | 아마존
1×(또는) DIYables STEM V4 IoT 쿠팡 | 아마존
1×USB 케이블 타입-A to 타입-C (USB-A PC용) 쿠팡 | 아마존
1×USB 케이블 타입-C to 타입-C (USB-C PC용) 아마존
1×(추천) 아두이노 우노 R4용 스크루 터미널 블록 쉴드 쿠팡 | 아마존
1×(추천) Sensors/Servo Expansion Shield for Arduino UNO R4 아마존
1×(추천) 아두이노 우노 R4용 브레드보드 쉴드 쿠팡 | 아마존
1×(추천) 아두이노 우노 R4용 케이스 쿠팡 | 아마존
1×(추천) 아두이노 우노 R4용 전원 분배기 쿠팡 | 아마존
1×(추천) 아두이노 우노용 프로토타이핑 베이스 플레이트 & 브레드보드 키트 아마존
공개: 이 포스팅 에 제공된 일부 링크는 아마존 제휴 링크입니다. 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
Arduino Uno R4 WiFi Compatible Boards

Arduino UNO R4 WiFi 코드

빠른 단계

다음 단계를 순서대로 따라하세요:

  • Arduino UNO R4 WiFi를 처음 사용하는 경우 아두이노 우노 R4 - 소프트웨어 설치를 참조하세요.
  • USB 케이블을 사용하여 Arduino UNO R4 WiFi 보드를 컴퓨터에 연결합니다.
  • 컴퓨터에서 Arduino IDE를 실행합니다.
  • Arduino UNO R4 WiFi 보드와 적절한 COM 포트를 선택합니다.
  • Arduino IDE 왼쪽 바에서 Libraries 아이콘으로 이동합니다.
  • "DIYables Bluetooth"를 검색한 다음 DIYables의 DIYables Bluetooth 라이브러리를 찾습니다.
  • Install 버튼을 클릭하여 라이브러리를 설치합니다.
아두이노 우노 R4 diyaBLEs 블루투스 라이브러리
  • 다른 라이브러리 종속성 설치를 요청받습니다.
  • Install All 버튼을 클릭하여 모든 라이브러리 종속성을 설치합니다.
아두이노 우노 R4 diyaBLEs 블루투스 종속성

BLE 코드

  • Arduino IDE에서 File Examples DIYables Bluetooth ArduinoBLE_AnalogGauge 예제로 이동하거나, 위의 코드를 복사하여 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); }
  • Arduino IDE에서 Upload 버튼을 클릭하여 코드를 Arduino UNO R4 WiFi에 업로드합니다.
  • 시리얼 모니터를 엽니다.
  • 시리얼 모니터에서 결과를 확인합니다. 아래와 같이 표시됩니다:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
DIYables Bluetooth - Analog Gauge Example Waiting for Bluetooth connection...
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2

모바일 앱

  • 스마트폰에 DIYables 블루투스 앱을 설치하세요: Android | iOS

참고: DIYables 블루투스 앱은 Android에서 BLE와 클래식 블루투스를 모두 지원하고, iOS에서는 BLE를 지원합니다. Arduino UNO R4 WiFi는 BLE를 사용하므로 앱은 Android와 iOS 모두에서 작동합니다. BLE에는 수동 페어링이 필요 없으며 스캔하고 연결하기만 하면 됩니다.

  • DIYables 블루투스 앱을 엽니다.
  • 처음 앱을 열면 권한을 요청합니다. 다음을 허용해 주세요:
    • 주변 기기(Nearby Devices) 권한(Android 12+) / 블루투스 권한(iOS) - 블루투스 기기를 스캔하고 연결하는 데 필요합니다.
    • 위치(Location) 권한(Android 11 이하만 해당) - 구형 Android 버전에서 BLE 기기를 스캔하는 데 필요합니다.
  • 휴대폰에서 블루투스가 켜져 있는지 확인합니다.
  • 홈 화면에서 Connect 버튼을 탭합니다. 앱이 BLE 기기를 스캔합니다.
diyaBLEs 블루투스 앱 - 스캔 버튼이 있는 홈 화면
  • 스캔 결과에서 "Arduino_Gauge"를 찾아 탭하여 연결합니다.
  • 연결되면 앱이 자동으로 홈 화면으로 돌아갑니다. 앱 메뉴에서 아날로그 게이지 앱을 선택합니다.
diyaBLEs 블루투스 앱 - 아날로그 게이지 앱이 있는 홈 화면

참고: 홈 화면의 설정 아이콘을 탭하면 앱을 표시하거나 숨길 수 있습니다. 자세한 내용은 DIYables 블루투스 앱 사용 설명서를 참조하세요.

  • 부드러운 바늘 움직임으로 값을 표시하는 아날로그 게이지가 표시됩니다(스피드미터 시뮬레이션).
diyaBLEs 블루투스 앱 - 아날로그 게이지 화면

이제 Arduino IDE의 시리얼 모니터를 다시 확인하세요. 다음과 같이 표시됩니다:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Bluetooth connected! Gauge value: 50.00 km/h Gauge value: 59.76 km/h Gauge value: 68.78 km/h
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2

창의적인 커스터마이징 - 프로젝트에 맞게 코드 적용하기

게이지 범위 및 단위 설정

// Speedometer: 0-200 km/h DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 200.0, "km/h"); // Pressure gauge: 0-100 PSI DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 100.0, "PSI"); // RPM gauge: 0-8000 RPM DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 8000.0, "RPM"); // Percentage: 0-100% DIYables_BluetoothAnalogGauge bluetoothGauge(bluetoothServer, 0.0, 100.0, "%");

게이지 값 전송

// Send a value to the gauge bluetoothGauge.send(75.5); // Read from sensor and send float sensorValue = analogRead(A0) * (100.0 / 1023.0); bluetoothGauge.send(sensorValue);

값 요청 처리

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

프로그래밍 예제

가변저항 게이지

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); } }

배터리 잔량 모니터

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()); } }

문제 해결

일반적인 문제

1. 앱에서 기기를 찾을 수 없음

  • Arduino UNO R4 WiFi가 전원이 켜져 있고 스케치가 업로드되어 있는지 확인하세요.
  • 휴대폰의 블루투스가 활성화되어 있는지 확인하세요.
  • Android 11 이하에서는 위치 서비스도 활성화하세요.

2. 게이지가 업데이트되지 않음

  • send()가 루프에서 호출되고 있는지 확인하세요.
  • 업데이트 간격 타이밍을 확인하세요.
  • bluetoothServer.loop()가 호출되고 있는지 확인하세요.

3. 게이지가 잘못된 범위를 표시함

  • 생성자의 최소/최대 값을 확인하세요.
  • 단위 문자열이 올바른지 확인하세요.
  • 범위를 벗어난 값은 클램프됩니다.

4. 바늘이 불규칙하게 움직임

  • 센서 판독값에 스무딩이나 평균화를 추가하세요.
  • 필요한 경우 업데이트 빈도를 줄이세요.
  • 노이즈가 있는 아날로그 입력을 확인하세요.