본문 바로가기
MCU/아두이노

[아두이노] 나노 33 IoT로 LSM6DS3 센서 데이터를 BLE로 전송하기(Sending LSM6DS3 Sensor Data via BLE with Arduino Nano 33 IoT)

by linuxgo 2025. 8. 3.
반응형

준비물

  • 아두이노 나노 33 IoT 보드

Arduino Nano 33 IoT

  • Arduino IDE (최신 버전)
  • Arduino_LSM6DS3 라이브러리 (센서 데이터 읽기용)
  • ArduinoBLE 라이브러리 (BLE 통신용)
  • BLE 수신 앱: nRF Connect 또는 LightBlue (iOS/Android)

라이브러리 설치

  1. Arduino IDE를 열고 도구 > 라이브러리 관리로 이동합니다.

예제 코드

아래 코드는 LSM6DS3 센서에서 가속도(x, y, z)와 자이로(x, y, z) 데이터를 읽어 BLE로 전송합니다.

#include <Arduino_LSM6DS3.h>
#include <ArduinoBLE.h>

// BLE 서비스 및 특성 UUID 정의  
// Define BLE service and characteristic UUIDs  
BLEService imuService("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEStringCharacteristic imuDataCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify, 40);

void setup() {
  Serial.begin(9600);
  while (!Serial); // 시리얼 포트 연결 대기 (Wait for serial port connection)
  
  // IMU 초기화 (Initialize IMU)
  if (!IMU.begin()) {
    Serial.println("LSM6DS3 초기화 실패! (LSM6DS3 initialization failed!)");
    while (1);
  }
  Serial.println("LSM6DS3 초기화 완료! (LSM6DS3 initialized!)");

  // BLE 초기화 (Initialize BLE)
  if (!BLE.begin()) {
    Serial.println("BLE 초기화 실패! (BLE initialization failed!)");
    while (1);
  }
  Serial.println("BLE 초기화 완료! (BLE initialized!)");

  // BLE 서비스 및 특성 설정 (Set up BLE service and characteristic)
  BLE.setLocalName("Nano33IoT_IMU");
  BLE.setAdvertisedService(imuService);
  imuService.addCharacteristic(imuDataCharacteristic);
  BLE.addService(imuService);

  // 광고 시작 (Start advertising)
  BLE.advertise();
  Serial.println("BLE 장치 광고 중... (BLE device advertising...)");
}

void loop() {
  // 중앙 장치 연결 대기 (Wait for central device connection)
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("연결됨: (Connected: ");
    Serial.println(central.address());

    while (central.connected()) {
      float accelX, accelY, accelZ;
      float gyroX, gyroY, gyroZ;

      // 가속도 데이터 읽기 (Read acceleration data)
      if (IMU.accelerationAvailable()) {
        IMU.readAcceleration(accelX, accelY, accelZ);

        // 자이로 데이터 읽기 (Read gyroscope data)
        if (IMU.gyroscopeAvailable()) {
          IMU.readGyroscope(gyroX, gyroY, gyroZ);

          // 데이터 문자열로 포맷팅 (Format data as string)
          String data = String(accelX, 2) + "," + String(accelY, 2) + "," + String(accelZ, 2) + "," +
                        String(gyroX, 2) + "," + String(gyroY, 2) + "," + String(gyroZ, 2);

          // BLE 특성에 데이터 쓰기 (Write data to BLE characteristic)
          imuDataCharacteristic.writeValue(data);

          // 디버깅용 시리얼 출력 (Serial output for debugging)
          Serial.println("데이터 전송: (Data sent: " + data + ")");
        }
      }

      delay(200); // 200ms 대기 (200ms delay)
    }
    Serial.println("연결 끊김 (Disconnected)");
  }
}

코드 설명

Code Explanation

  • 라이브러리: Arduino_LSM6DS3는 센서 데이터를 읽고, ArduinoBLE는 BLE 통신을 처리합니다.
  • BLE 설정: 사용자 정의 UUID로 서비스와 특성을 생성해 데이터를 전송합니다.
  • 데이터 전송: 가속도와 자이로 데이터를 쉼표로 구분된 문자열로 포맷해 200ms마다 전송합니다.

실행 방법

  1. 보드 연결: 아두이노 나노 33 IoT를 USB로 컴퓨터에 연결합니다.
  2. 보드 및 포트 선택: Arduino IDE에서 도구 > 보드로 "Arduino Nano 33 IoT"를 선택하고 포트를 선택합니다.
  3. 코드 업로드: 위 코드를 복사해 업로드합니다.
  4. BLE 연결: 스마트폰에서 nRF Connect 또는 LightBlue 앱을 열고 Nano33IoT_IMU 장치에 연결합니다.
  5. 데이터 수신: 특성 UUID 19B10001-E8F2-537E-4F6C-D104768A1214에서 "Notify"를 활성화해 데이터를 확인합니다.

출력 예시

  • 시리얼 모니터 (Serial Monitor):
LSM6DS3 초기화 완료! (LSM6DS3 initialized!)
BLE 초기화 완료! (BLE initialized!)
BLE 장치 광고 중... (BLE device advertising...)
연결됨: 12:34:56:78:9A:BC (Connected: 12:34:56:78:9A:BC)
데이터 전송: 0.02,0.01,1.00,0.12,0.05,-0.03 (Data sent: 0.02,0.01,1.00,0.12,0.05,-0.03)
...
연결 끊김 (Disconnected)
  • BLE 앱: 0.02,0.01,1.00,0.12,0.05,-0.03 같은 데이터를 주기적으로 수신합니다.

주의사항

  • BLE 연결: 한 번에 하나의 호스트만 연결 가능합니다.
  • 데이터 크기: 특성 데이터는 40바이트로 제한됩니다.
  • 앱 선택: nRF Connect 또는 LightBlue로 쉽게 테스트할 수 있습니다.
반응형