My goal is to use BLE on my Arduino Nano 33 IoT to transmit IMU data back to my computer. This will correctly run when the Arduino is connected to my laptop via a micro usb cable. However, I think due to the wire, the connection gets interrupted very frequently when I'm moving the Arduino and the range is also limited by the length of the cable.
So, I tried connecting my Nano to a breadboard using a power supply module and a 9V battery connected to it. I am able to successfully start the Nano, and by testing around with the code (turning the LED on at different steps), I determined that the BLE.begin()
would return 0, causing it to run an infinite loop.
Here's the code:
#include <ArduinoBLE.h>
#include <Arduino_LSM6DS3.h>
#define BLE_UUID_ACCELEROMETER_SERVICE "1101"
#define BLE_UUID_ACCELEROMETER_X "2101"
#define BLE_UUID_ACCELEROMETER_Y "2102"
#define BLE_UUID_ACCELEROMETER_Z "2103"
#define BLE_DEVICE_NAME "Elfo"
#define BLE_LOCAL_NAME "Elfo"
BLEService accelerometerService(BLE_UUID_ACCELEROMETER_SERVICE);
BLEFloatCharacteristic accelerometerCharacteristicX(BLE_UUID_ACCELEROMETER_X, BLERead | BLENotify);
BLEFloatCharacteristic accelerometerCharacteristicY(BLE_UUID_ACCELEROMETER_Y, BLERead | BLENotify);
BLEFloatCharacteristic accelerometerCharacteristicZ(BLE_UUID_ACCELEROMETER_Z, BLERead | BLENotify);
float x, y, z;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
// initialize IMU
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1)
;
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
digitalWrite(LED_BUILTIN, HIGH);
//BLE.stopScan();
// initialize BLE
if (!BLE.begin()) {
Serial.println("Starting Bluetooth® Low Energy module failed!");
while (1)
;
}
digitalWrite(LED_BUILTIN, HIGH);
// set advertised local name and service UUID
BLE.setDeviceName(BLE_DEVICE_NAME);
BLE.setLocalName(BLE_LOCAL_NAME);
BLE.setAdvertisedService(accelerometerService);
accelerometerService.addCharacteristic(accelerometerCharacteristicX);
accelerometerService.addCharacteristic(accelerometerCharacteristicY);
accelerometerService.addCharacteristic(accelerometerCharacteristicZ);
BLE.addService(accelerometerService);
accelerometerCharacteristicX.writeValue(0);
accelerometerCharacteristicY.writeValue(0);
accelerometerCharacteristicZ.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE Accelerometer Peripheral");
}
void loop() {
BLEDevice central = BLE.central();
if (IMU.accelerationAvailable()) {
digitalWrite(LED_BUILTIN, HIGH);
IMU.readAcceleration(x, y, z);
Serial.print(x);
Serial.print(y);
Serial.print(z);
Serial.println();
accelerometerCharacteristicX.writeValue(x);
accelerometerCharacteristicY.writeValue(y);
accelerometerCharacteristicZ.writeValue(z);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
}
I've been searching through previous articles, and the most typical suggestion is to remove while (!Serial);
but I don't have that in my program anyways. I've been trying to debug this for quite a while now but I haven't made much progress because I'm new to Arduino.