아두이노 우노 R4 WiFi 블루투스 로테이터 예제 BLE를 통한 각도 제어 튜토리얼

개요

블루투스 로테이터 예제는 DIYables 블루투스 STEM 앱을 통해 각도 위치 제어 기능을 제공합니다. BLE(Bluetooth Low Energy) 를 사용하는 Arduino UNO R4 WiFi 전용으로 설계되어 스마트폰에서 무선으로 회전 각도와 서보 모터를 제어합니다. 제한 범위 및 연속 회전 모드를 지원합니다. 서보 제어, 로봇공학, 팬-틸트 메커니즘, 각도 위치 결정에 적합합니다.

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

아두이노 우노 R4 와이파이 블루투스 로테이터 예제 - BLE를 통한 각도 제어 튜토리얼

기능

  • 제한 모드: 최소/최대 각도 범위 설정(예: 0°~180°)
  • 연속 모드: 360° 전체 연속 회전
  • 실시간 각도: 사용자가 회전할 때 각도 업데이트 수신
  • 서보 통합: 직접 서보 모터 제어
  • 초기 각도 동기화: 연결 시 현재 위치 전송
  • 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×(optional) Servo Motor 쿠팡 | 아마존
1×브레드보드 쿠팡 | 아마존
1×점퍼케이블 쿠팡 | 아마존
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_Rotator 예제로 이동하거나, 위의 코드를 복사하여 Arduino IDE 편집기에 붙여 넣습니다.
/* * DIYables Bluetooth Library - Bluetooth Rotator Example * Works with DIYables Bluetooth STEM app on Android and iOS * * This example demonstrates the Bluetooth Rotator feature: * - Rotatable disc/knob control (0-360 degrees) * - Continuous or limited angle range * - Perfect for servo control, compass display, volume knobs * * 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: Servo motor for visual feedback * * Setup: * 1. Upload the sketch to your Arduino * 2. Open Serial Monitor to see connection status * 3. Use DIYables Bluetooth App to connect and rotate the knob * * Tutorial: https://diyables.io/bluetooth-app * Author: DIYables */ #include <DIYables_BluetoothServer.h> #include <DIYables_BluetoothRotator.h> #include <platforms/DIYables_ArduinoBLE.h> // BLE Configuration const char* DEVICE_NAME = "Arduino_Rotator"; 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 Rotator app instance // Option 1: Continuous mode (0-360, wraps around) // DIYables_BluetoothRotator bluetoothRotator(ROTATOR_MODE_CONTINUOUS); // Option 2: Limited mode (constrained angle range) DIYables_BluetoothRotator bluetoothRotator(ROTATOR_MODE_LIMITED, 0, 180); // Variables to store current angle float currentAngle = 0.0; // Optional: Servo control (uncomment if using Servo library) // #include <Servo.h> // Servo myServo; // const int SERVO_PIN = 9; void setup() { Serial.begin(9600); while (!Serial); Serial.println("DIYables Bluetooth - Rotator Example"); // Optional: Initialize servo // myServo.attach(SERVO_PIN); // myServo.write(0); bluetoothServer.begin(); bluetoothServer.addApp(&bluetoothRotator); bluetoothServer.setOnConnected([]() { Serial.println("Bluetooth connected!"); bluetoothRotator.send(currentAngle); }); bluetoothServer.setOnDisconnected([]() { Serial.println("Bluetooth disconnected!"); }); bluetoothRotator.onRotatorAngle([](float angle) { currentAngle = angle; Serial.print("Rotator angle: "); Serial.print(angle); Serial.println("°"); // TODO: Add your control logic here based on angle // Examples: // - Servo control: servo.write(map(angle, 0, 360, 0, 180)); // - Stepper motor: stepper.moveTo(angleToSteps(angle)); // - LED ring: setLEDPosition(angle); // - Volume control: setVolume(map(angle, 0, 360, 0, 100)); }); Serial.println("Waiting for Bluetooth connection..."); } void loop() { bluetoothServer.loop(); 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 - Rotator 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_Rotator"를 찾아 탭하여 연결합니다.
  • 연결되면 앱이 자동으로 홈 화면으로 돌아갑니다. 앱 메뉴에서 로테이터 앱을 선택합니다.
diyaBLEs 블루투스 앱 - 로테이터 앱이 있는 홈 화면

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

  • 0°~180°를 표시하는 회전 다이얼이 표시됩니다.
  • 다이얼을 회전시켜 각도 값을 Arduino로 전송합니다.
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! Angle changed: 45.00° Angle changed: 90.00° Angle changed: 135.00°
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2

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

회전 모드

// Limited mode: specify min and max angle DIYables_BluetoothRotator bluetoothRotator(bluetoothServer, ROTATOR_MODE_LIMITED, 0, 180); // Continuous mode: full 360° rotation DIYables_BluetoothRotator bluetoothRotator(bluetoothServer, ROTATOR_MODE_CONTINUOUS);

각도 변화 처리

bluetoothRotator.onRotatorAngle([](float angle) { Serial.print("Angle: "); Serial.println(angle); // Control a servo motor myServo.write((int)angle); });

현재 각도 전송

// Send current angle to the app (e.g., on connect) bluetoothRotator.sendAngle(currentAngle);

프로그래밍 예제

서보 모터 제어

#include <Servo.h> Servo myServo; float currentAngle = 90; void setup() { myServo.attach(9); myServo.write(currentAngle); bluetoothRotator.onRotatorAngle([](float angle) { currentAngle = angle; myServo.write((int)angle); Serial.print("Servo: "); Serial.println(angle); }); } void loop() { bluetoothServer.loop(); }

팬-틸트 카메라 마운트

Servo panServo; Servo tiltServo; // Use two rotator instances for pan and tilt // Or use a single rotator for one axis // and a slider for the other void setup() { panServo.attach(9); tiltServo.attach(10); bluetoothRotator.onRotatorAngle([](float angle) { panServo.write((int)angle); Serial.print("Pan: "); Serial.println(angle); }); }

문제 해결

일반적인 문제

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

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

2. 서보가 반응하지 않음

  • 서보가 올바른 핀에 연결되어 있는지 확인하세요.
  • 서보 라이브러리가 포함되어 있고 attach()가 호출되는지 확인하세요.
  • 서보에 적절한 전원 공급을 확인하세요.

3. 각도 범위가 잘못 표시됨

  • 생성자의 최소/최대 값을 확인하세요.
  • 제한 범위에는 ROTATOR_MODE_LIMITED를 사용하고 있는지 확인하세요.

4. 재연결 시 다이얼이 0으로 초기화됨

  • 기기가 재연결될 때 sendAngle()을 사용하여 현재 각도를 전송하세요.
  • 예제 코드는 onRotatorAngle 콜백에서 이를 처리합니다.

5. 업로드 실패 또는 보드를 인식하지 못함

  • 보드 관리자를 통해 최신 Arduino UNO R4 보드 패키지를 설치하세요.
  • 다른 USB 케이블이나 포트를 사용해 보세요.

프로젝트 아이디어

  • 서보 모터 컨트롤러
  • 팬-틸트 카메라 마운트
  • 로봇 팔 관절 제어
  • 다이얼 기반 온도 조절기 제어
  • 나침반/방향 표시

다음 단계

블루투스 로테이터 예제를 마스터한 후 다음을 시도해 보세요:

  1. 블루투스 슬라이더 - 선형 값 제어용
  2. 블루투스 조이스틱 - 2D 위치 제어용
  3. 블루투스 아날로그 게이지 - 시각적 피드백용
  4. 다중 블루투스 앱 - 로테이터와 다른 앱 결합