Arduino Nano 33 BLE

Arduino Nano 33 BLE

센서 / 모듈 이름 Info
nRF52840 32Bbit, 64MHz MCU
NINA-B306 Bluetooth
LSM9DS1 9축 IMU센서
  • RESET 버튼을 2번 눌러 Upload mode로 전환
  • begin() 메서드 필수

Aruino Nano 33 BLE


Arduino IDE와 Arduino Nano 33 BLE 연결

board-manager
  • - 보드 - 보드 매니저에서 arduino nano 33 BLE를 검색
  • Arduino nRF528x Boards 설치
serial-port
  • - 포트 - Arduino Nano 33 BLE 선택
  • 보드 정보 얻기로 연결 확인 가능
  • 안된다면 케이블을 바꿔 해결 가능(데이터 전송이 가능한 케이블)

IMU Sensor

manage-library
  • - 라이브러리 관리...
lsm9ds1
  • 9축 IMU센서의 이름 LSM9DS1 검색 후 Arduino_LSM9DS1 라이브러리 다운로드
IMU.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <Arduino_LSM9DS1.h>

float acc_x, acc_y, acc_z;
float gyro_x, gyro_y, gyro_z;
float mag_x, mag_y, mag_z;

void setup() {
Serial.begin(2000000);
while(!Serial);
if (!IMU.begin()) { //LSM9DSI센서 시작
Serial.println("LSM9DSI센서 오류!");
while (1);
}
Serial.println("acc_x,acc_y,acc_z,gyro_x,gyro_y,gyro_z,mag_x,mag_y,mag_z");
delay(100);
}
void loop() {
//가속도센서
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(acc_x, acc_y, acc_z);
Serial.print(acc_x); Serial.print(","); Serial.print(acc_y); Serial.print(","); Serial.print(acc_z); Serial.print(",");
}
//자이로센서
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(gyro_x, gyro_y, gyro_z);
Serial.print(gyro_x); Serial.print(","); Serial.print(gyro_y); Serial.print(","); Serial.print(gyro_z); Serial.print(",");
}
//지자계센서
if (IMU.magneticFieldAvailable()) {
IMU.readMagneticField(mag_x, mag_y, mag_z);
Serial.print(mag_x); Serial.print(","); Serial.print(mag_y); Serial.print(","); Serial.println(mag_z);
}
delay(50);
}

Graph

graph

Bluetooth Module

arduinoble
PeripheralExplorer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Peripheral Explorer

This example scans for BLE peripherals until one with a particular name ("LED")
is found. Then connects, and discovers + prints all the peripheral's attributes.

The circuit:
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

You can use it with another board that is compatible with this library and the
Peripherals -> LED example.

This example code is in the public domain.
*/

#include <ArduinoBLE.h>

void setup() {
Serial.begin(9600);
while (!Serial);

// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");

while (1);
}

Serial.println("BLE Central - Peripheral Explorer");

// start scanning for peripherals
BLE.scan();
}

void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();

if (peripheral) {
// discovered a peripheral, print out address, local name, and advertised service
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();

// see if peripheral is a LED
if (peripheral.localName() == "IMU") {
// stop scanning
BLE.stopScan();

explorerPeripheral(peripheral);

// peripheral disconnected, we are done
while (1) {
// do nothing
}
}
}
}

void explorerPeripheral(BLEDevice peripheral) {
// connect to the peripheral
Serial.println("Connecting ...");

if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}

// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}

// read and print device name of peripheral
Serial.println();
Serial.print("Device name: ");
Serial.println(peripheral.deviceName());
Serial.print("Appearance: 0x");
Serial.println(peripheral.appearance(), HEX);
Serial.println();

// loop the services of the peripheral and explore each
for (int i = 0; i < peripheral.serviceCount(); i++) {
BLEService service = peripheral.service(i);

exploreService(service);
}

Serial.println();

// we are done exploring, disconnect
Serial.println("Disconnecting ...");
peripheral.disconnect();
Serial.println("Disconnected");
}

void exploreService(BLEService service) {
// print the UUID of the service
Serial.print("Service ");
Serial.println(service.uuid());

// loop the characteristics of the service and explore each
for (int i = 0; i < service.characteristicCount(); i++) {
BLECharacteristic characteristic = service.characteristic(i);

exploreCharacteristic(characteristic);
}
}

void exploreCharacteristic(BLECharacteristic characteristic) {
// print the UUID and properties of the characteristic
Serial.print("\tCharacteristic ");
Serial.print(characteristic.uuid());
Serial.print(", properties 0x");
Serial.print(characteristic.properties(), HEX);

// check if the characteristic is readable
if (characteristic.canRead()) {
// read the characteristic value
characteristic.read();

if (characteristic.valueLength() > 0) {
// print out the value of the characteristic
Serial.print(", value 0x");
printData(characteristic.value(), characteristic.valueLength());
}
}
Serial.println();

// loop the descriptors of the characteristic and explore each
for (int i = 0; i < characteristic.descriptorCount(); i++) {
BLEDescriptor descriptor = characteristic.descriptor(i);

exploreDescriptor(descriptor);
}
}

void exploreDescriptor(BLEDescriptor descriptor) {
// print the UUID of the descriptor
Serial.print("\t\tDescriptor ");
Serial.print(descriptor.uuid());

// read the descriptor value
descriptor.read();

// print out the value of the descriptor
Serial.print(", value 0x");
printData(descriptor.value(), descriptor.valueLength());

Serial.println();
}

void printData(const unsigned char data[], int length) {
for (int i = 0; i < length; i++) {
unsigned char b = data[i];

if (b < 16) {
Serial.print("0");
}

Serial.print(b, HEX);
}
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
BLE Central - Peripheral Explorer
Found e8:61:57:48:1a:ef 'IMU' 1101
Connecting ...
Connected
Discovering attributes ...
Attributes discovered

Device name: IMU
Appearance: 0x0

Service 1800
Characteristic 2a00, properties 0x2, value 0x494D55
Characteristic 2a01, properties 0x2, value 0x0000
Service 1801
Characteristic 2a05, properties 0x20
Descriptor 2902, value 0x0000
Service 1101
Characteristic 2101, properties 0x12, value 0x0A
Descriptor 2902, value 0x0000

Disconnecting ...
Disconnected
peripheral.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>

BLEService ACC("1001");
BLEFloatCharacteristic accX("2001", BLERead | BLENotify);
BLEFloatCharacteristic accY("2002", BLERead | BLENotify);
BLEFloatCharacteristic accZ("2003", BLERead | BLENotify);

BLEService GYRO("1002");
BLEFloatCharacteristic gyroX("2011", BLERead | BLENotify);
BLEFloatCharacteristic gyroY("2012", BLERead | BLENotify);
BLEFloatCharacteristic gyroZ("2013", BLERead | BLENotify);

BLEService MAG("1003");
BLEFloatCharacteristic magX("2021", BLERead | BLENotify);
BLEFloatCharacteristic magY("2022", BLERead | BLENotify);
BLEFloatCharacteristic magZ("2023", BLERead | BLENotify);

float acc_x, acc_y, acc_z;
float gyro_x, gyro_y, gyro_z;
float mag_x, mag_y, mag_z;

void setup() {
Serial.begin(115200);

if(!BLE.begin()) {
Serial.println("Starting BLE Failed!");
while(1);
}

if (!IMU.begin()) { //LSM9DSI센서 시작
Serial.println("LSM9DSI센서 오류!");
while (1);
}

BLE.setDeviceName("IMU");
BLE.setLocalName("IMU");

BLE.setAdvertisedService(ACC);
BLE.setAdvertisedService(GYRO);
BLE.setAdvertisedService(MAG);
ACC.addCharacteristic(accX);
ACC.addCharacteristic(accY);
ACC.addCharacteristic(accZ);
GYRO.addCharacteristic(gyroX);
GYRO.addCharacteristic(gyroY);
GYRO.addCharacteristic(gyroZ);
MAG.addCharacteristic(magX);
MAG.addCharacteristic(magY);
MAG.addCharacteristic(magZ);
BLE.addService(ACC);
BLE.addService(GYRO);
BLE.addService(MAG);
BLE.setConnectable(true);
BLE.setAdvertisingInterval(100);
BLE.advertise();
Serial.println("Bluetooth Device Active, Waiting for Connections...");
}

void loop() {
BLEDevice central = BLE.central();

if(central) {
Serial.print("Connected to Central: ");
Serial.println(central.address());
while(central.connected()) {
IMU.readAcceleration(acc_x, acc_y, acc_z);
IMU.readGyroscope(gyro_x, gyro_y, gyro_z);
IMU.readMagneticField(mag_x, mag_y, mag_z);
accX.writeValue(acc_x);
accY.writeValue(acc_y);
accZ.writeValue(acc_z);
gyroX.writeValue(gyro_x);
gyroY.writeValue(gyro_y);
gyroZ.writeValue(gyro_z);
magX.writeValue(mag_x);
magY.writeValue(mag_y);
magZ.writeValue(mag_z);
Serial.println(acc_x);
}
}
Serial.print("Disconnected from Central: ");
Serial.println(BLE.address());
}
central.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <ArduinoBLE.h>

union dat{
unsigned char asdf[4];
float zxcv;
};

float getData(const unsigned char data[], int length) {
dat dat;
for (int i = 0; i < length; i++) {
dat.asdf[i] = data[i];
}
return dat.zxcv;
}

void printcsv(BLECharacteristic c1, BLECharacteristic c2, BLECharacteristic c3, BLECharacteristic c4, BLECharacteristic c5, BLECharacteristic c6, BLECharacteristic c7, BLECharacteristic c8, BLECharacteristic c9){
c1.read();
c2.read();
c3.read();
c4.read();
c5.read();
c6.read();
c7.read();
c8.read();
c9.read();
float f1=getData(c1.value(), c1.valueLength());
float f2=getData(c2.value(), c2.valueLength());
float f3=getData(c3.value(), c3.valueLength());
float f4=getData(c4.value(), c4.valueLength());
float f5=getData(c5.value(), c5.valueLength());
float f6=getData(c6.value(), c6.valueLength());
float f7=getData(c7.value(), c7.valueLength());
float f8=getData(c8.value(), c8.valueLength());
float f9=getData(c9.value(), c9.valueLength());
Serial.print(f1);
Serial.print(',');
Serial.print(f2);
Serial.print(',');
Serial.print(f3);
Serial.print(',');
Serial.print(f4);
Serial.print(',');
Serial.print(f5);
Serial.print(',');
Serial.print(f6);
Serial.print(',');
Serial.print(f7);
Serial.print(',');
Serial.print(f8);
Serial.print(',');
Serial.print(f9);
Serial.print('\n');
}

void setup() {
Serial.begin(115200);

if(!BLE.begin()) {
Serial.println("Starting BLE Failed!");
while(1);
}
BLE.scan();
}

void loop() {
BLEDevice peripheral = BLE.available();

if(peripheral){
if(peripheral.localName()=="IMU"){
BLE.stopScan();
if(peripheral.connect()){
Serial.println("Connect1");
}
else{
return;
}
if(peripheral.discoverAttributes()){
Serial.println("Connect2");
}
else{
return;
}
BLEService acc=peripheral.service("1001");
BLECharacteristic accx=acc.characteristic("2001");
BLECharacteristic accy=acc.characteristic("2002");
BLECharacteristic accz=acc.characteristic("2003");
BLEService gyro=peripheral.service("1002");
BLECharacteristic gyrox=gyro.characteristic("2011");
BLECharacteristic gyroy=gyro.characteristic("2012");
BLECharacteristic gyroz=gyro.characteristic("2013");
BLEService mag=peripheral.service("1003");
BLECharacteristic magx=mag.characteristic("2021");
BLECharacteristic magy=mag.characteristic("2022");
BLECharacteristic magz=mag.characteristic("2023");
while(true){
// accx.read();
// float f1=getData(accx.value(),accx.valueLength());
// Serial.print(f1);
// Serial.print(',');
// accy.read();
// float f2=getData(accy.value(),accy.valueLength());
// Serial.print(f2);
// Serial.print(',');
// accz.read();
// float f3=getData(accz.value(),accz.valueLength());
// Serial.println(f3);
if(peripheral.connected()){
printcsv(accx,accy,accz,gyrox,gyroy,gyroz,magx,magy,magz);
}
else{
peripheral.disconnect();
return;
}
}
}
}
BLE.scan();
Serial.println("rescan");
}