Seeed xiao ble nrf52840 sense arduinoble

Hello!
I created a code to send acceleration data via BLE using seeed xiao ble nrf52840 sense under Arduino 2.3.2 and Seeed nRF52 mbed-enabled Boards Boards environment. In the code below, I cannot change the BLE advertising interval, it remains at the default of 100 ms even after setting it with BLE.setAdvertisingInterval();. Any ideas for improvement?

#include <ArduinoBLE.h> // Arduino BLE library

#include “LSM6DS3.h”

#include “Wire.h”

//Create an instance of class LSM6DS3

LSM6DS3 xIMU(I2C_MODE, 0x6A); //I2C device address 0x6A

/* Online GUID / UUID Generator:

64cf715d-f89e-4ec0-b5c5-d10ad9b53bf2

*/

// UUid for Service

const char* UUID_serv = “64cf715d-f89e-4ec0-b5c5-d10ad9b53bf2”;

// UUids for accelerometer characteristics

const char* UUID_ax = “64cf715e-f89e-4ec0-b5c5-d10ad9b53bf2”;

const char* UUID_ay = “64cf715f-f89e-4ec0-b5c5-d10ad9b53bf2”;

const char* UUID_az = “64cf7160-f89e-4ec0-b5c5-d10ad9b53bf2”;

const char* UUID_wx = “64cf7161-f89e-4ec0-b5c5-d10ad9b53bf2”;

const char* UUID_wy = “64cf7162-f89e-4ec0-b5c5-d10ad9b53bf2”;

const char* UUID_wz = “64cf7163-f89e-4ec0-b5c5-d10ad9b53bf2”;

// BLE Service

BLEService myService(UUID_serv);

// BLE Characteristics

BLEFloatCharacteristic chAX(UUID_ax, BLERead|BLENotify);

BLEFloatCharacteristic chAY(UUID_ay, BLERead|BLENotify);

BLEFloatCharacteristic chAZ(UUID_az, BLERead|BLENotify);

BLEFloatCharacteristic chWX(UUID_wx, BLERead|BLENotify);

BLEFloatCharacteristic chWY(UUID_wy, BLERead|BLENotify);

BLEFloatCharacteristic chWZ(UUID_wz, BLERead|BLENotify);

void setup()

{

// Serial.begin(115200);

// while (!Serial);

// Serial.println(“Seeed XIAO BLE Sense IMU-Acc-Gyro Data Logger”);

bool err=false;

pinMode(LEDR, OUTPUT); // onboard led red

pinMode(LEDB, OUTPUT); // onboard led blue

digitalWrite(LEDB, HIGH); // led blue off

digitalWrite(LEDR, LOW); // led red on

// init IMU

if (xIMU.begin() != 0) {

// Serial.println("Device error");

err = true;

} else {

// Serial.println("Device OK!");

}

// init BLE

if (!BLE.begin())

{

// Serial.println("BLE: failed");

err=true;

}

// Serial.println(“BLE: ok”);

// error: flash led forever

if (err)

{

// Serial.println("Init error. System halted");

while(1)

{

  digitalWrite(LEDR, LOW);

  delay(500);

  digitalWrite(LEDR, HIGH); // led on

  delay(500);



}

}

// Set BLE name

BLE.setLocalName(“IMU-Acc-Gyro DataLogger”);

BLE.setDeviceName(“XIAO-BLE-Sense”);

// Set AdvertisingInterval

BLE.setAdvertisingInterval(160); // 10 ms ,16 * 0.625 ms, units of 0.625 ms

// Set advertised Service

BLE.setAdvertisedService(myService);

// Add characteristics to the Service

myService.addCharacteristic(chAX);

myService.addCharacteristic(chAY);

myService.addCharacteristic(chAZ);

myService.addCharacteristic(chWX);

myService.addCharacteristic(chWY);

myService.addCharacteristic(chWZ);

// add service to BLE

BLE.addService(myService);

// characteristics initial values

chAX.writeValue(0);

chAY.writeValue(0);

chAZ.writeValue(0);

chWX.writeValue(0);

chWY.writeValue(0);

chWZ.writeValue(0);

// start advertising

BLE.advertise();

// Serial.println(“Advertising started”);

// Serial.println(“Bluetooth device active, waiting for connections…”);

}

void loop()

{

// listen for BLE centrals devices

BLEDevice central = BLE.central();

// central device connected?

if (central)

{

digitalWrite(LEDR, HIGH);

digitalWrite(LEDB, LOW); // turn on the blue led

// Serial.print("Connected to central: ");

// Serial.println(central.address()); // central device MAC address



// while the central is still connected to peripheral:

while (central.connected())

{

    chAX.writeValue(xIMU.readFloatAccelX());

    chAY.writeValue(xIMU.readFloatAccelY());

    chAZ.writeValue(xIMU.readFloatAccelZ());

    chWX.writeValue(xIMU.readFloatGyroX());

    chWY.writeValue(xIMU.readFloatGyroY());

    chWZ.writeValue(xIMU.readFloatGyroZ());

    // delay(1);      

    // Serial.print(xIMU.readFloatAccelX(), 4);

    // Serial.print(',');

    // Serial.print(xIMU.readFloatAccelY(), 4);

    // Serial.print(',');

    // Serial.print(xIMU.readRawAccelZ(), 4);

    // Serial.print(',');

    // Serial.print(xIMU.readFloatAccelZ(), 4);

    // Serial.print(xIMU.readFloatGyroX(), 4);

    // Serial.print(',');

    // Serial.print(xIMU.readFloatGyroY(), 4);

    // Serial.print(',');

    // Serial.print(xIMU.readFloatGyroZ(), 4);

    // Serial.println(); // call function for updating value to send to central

} // still here while central connected

// central disconnected:

digitalWrite(LEDB, HIGH);

// Serial.print("Disconnected from central: ");

// Serial.println(central.address());

} // no central

else

{

// Blink the red LED when no central device is connected

digitalWrite(LEDR, HIGH);

delay(500);

digitalWrite(LEDR, LOW);

delay(500);

}

}

Hi there,
two things, the Quote marks from cut and paste job are not compatible, i.e. “LSM6DS3.h” in the code above are not correct UT8 and second I used the code tags so this looks readable you should too!. </> :point_left: :wink:

Given this"

BLE Advertisement Interval

When a BLE peripheral device is in advertising mode, advertising packets are sent periodically on each advertising channel. The time interval between packet set has both a fixed interval and a random delay. The interval is specified between the set of 3 packets (and 3 channels are almost always used).

You can set the fixed interval from 20ms to 10.24 seconds, in steps of 0.625ms. The random delay is a pseudo-random value from 0ms to 10ms that is automatically added. This randomness helps reduce the possibility of collisions between advertisements of different devices (if they fell in the same rate, they could interfere more easily). We mentioned that finding advertisements is critical, so avoiding collisions at all costs is extremely important. This is just another way BLE uses to improve robustness. "

#include <ArduinoBLE.h> // Arduino BLE library
#include “LSM6DS3.h”
#include “Wire.h”
//Create an instance of class LSM6DS3
LSM6DS3 xIMU(I2C_MODE, 0x6A); //I2C device address 0x6A
/* Online GUID / UUID Generator:
guidgenerator.com
Free Online GUID Generator
Free Online GUID / UUID Generator
64cf715d-f89e-4ec0-b5c5-d10ad9b53bf2
*/
// UUid for Service
const char* UUID_serv = “64cf715d-f89e-4ec0-b5c5-d10ad9b53bf2”;
// UUids for accelerometer characteristics
const char* UUID_ax = “64cf715e-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_ay = “64cf715f-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_az = “64cf7160-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_wx = “64cf7161-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_wy = “64cf7162-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_wz = “64cf7163-f89e-4ec0-b5c5-d10ad9b53bf2”;
// BLE Service
BLEService myService(UUID_serv);
// BLE Characteristics
BLEFloatCharacteristic chAX(UUID_ax, BLERead|BLENotify);
BLEFloatCharacteristic chAY(UUID_ay, BLERead|BLENotify);
BLEFloatCharacteristic chAZ(UUID_az, BLERead|BLENotify);
BLEFloatCharacteristic chWX(UUID_wx, BLERead|BLENotify);
BLEFloatCharacteristic chWY(UUID_wy, BLERead|BLENotify);
BLEFloatCharacteristic chWZ(UUID_wz, BLERead|BLENotify);
void setup()
{
// Serial.begin(115200);
// while (!Serial);
// Serial.println(“Seeed XIAO BLE Sense IMU-Acc-Gyro Data Logger”);
bool err=false;
pinMode(LEDR, OUTPUT); // onboard led red
pinMode(LEDB, OUTPUT); // onboard led blue
digitalWrite(LEDB, HIGH); // led blue off
digitalWrite(LEDR, LOW); // led red on
// init IMU
if (xIMU.begin() != 0) {
// Serial.println("Device error");
err = true;
} else {
// Serial.println("Device OK!");
}
// init BLE
if (!BLE.begin())
{
// Serial.println("BLE: failed");
err=true;
}
// Serial.println(“BLE: ok”);
// error: flash led forever
if (err)
{
// Serial.println("Init error. System halted");
while(1)
{
  digitalWrite(LEDR, LOW);
  delay(500);
  digitalWrite(LEDR, HIGH); // led on
  delay(500);
}
}
// Set BLE name
BLE.setLocalName(“IMU-Acc-Gyro DataLogger”);
BLE.setDeviceName(“XIAO-BLE-Sense”);
// Set AdvertisingInterval
BLE.setAdvertisingInterval(160); // 10 ms ,16 * 0.625 ms, units of 0.625 ms
// Set advertised Service
BLE.setAdvertisedService(myService);
// Add characteristics to the Service
myService.addCharacteristic(chAX);
myService.addCharacteristic(chAY);
myService.addCharacteristic(chAZ);
myService.addCharacteristic(chWX);
myService.addCharacteristic(chWY);
myService.addCharacteristic(chWZ);
// add service to BLE
BLE.addService(myService);
// characteristics initial values
chAX.writeValue(0);
chAY.writeValue(0);
chAZ.writeValue(0);
chWX.writeValue(0);
chWY.writeValue(0);
chWZ.writeValue(0);
// start advertising
BLE.advertise();
// Serial.println(“Advertising started”);
// Serial.println(“Bluetooth device active, waiting for connections…”);
}
void loop()
{

// listen for BLE centrals devices
BLEDevice central = BLE.central();
// central device connected?
if (central)
{
digitalWrite(LEDR, HIGH);
digitalWrite(LEDB, LOW); // turn on the blue led
// Serial.print("Connected to central: ");
// Serial.println(central.address()); // central device MAC address
// while the central is still connected to peripheral:
while (central.connected())
{
    chAX.writeValue(xIMU.readFloatAccelX());
    chAY.writeValue(xIMU.readFloatAccelY());
    chAZ.writeValue(xIMU.readFloatAccelZ());
    chWX.writeValue(xIMU.readFloatGyroX());
    chWY.writeValue(xIMU.readFloatGyroY());
    chWZ.writeValue(xIMU.readFloatGyroZ());
    // delay(1);      
    // Serial.print(xIMU.readFloatAccelX(), 4);
    // Serial.print(',');
    // Serial.print(xIMU.readFloatAccelY(), 4);
    // Serial.print(',');
    // Serial.print(xIMU.readRawAccelZ(), 4);
    // Serial.print(',');
    // Serial.print(xIMU.readFloatAccelZ(), 4);
    // Serial.print(xIMU.readFloatGyroX(), 4);
    // Serial.print(',');
    // Serial.print(xIMU.readFloatGyroY(), 4);
    // Serial.print(',');
    // Serial.print(xIMU.readFloatGyroZ(), 4);
    // Serial.println(); // call function for updating value to send to central
} // still here while central connected
// central disconnected:
digitalWrite(LEDB, HIGH);
// Serial.print("Disconnected from central: ");
// Serial.println(central.address());
} // no central
else
{
// Blink the red LED when no central device is connected
digitalWrite(LEDR, HIGH);
delay(500);
digitalWrite(LEDR, LOW);
delay(500);
}
}

which BSP are you using? the latest one 2.9.2?
I got this working and I can see the advertising interval change from the default. you can change it in the code as you go but need to stop advertising and restart it for it to stick.
HTH
GL :slight_smile: PJ
:v:

#include "ArduinoBLE.h"

const char *names[] = {"A", "B", "C"};

void setup()
{
  BLE.begin();
  BLE.setDeviceName("A");
  BLE.setLocalName("Device-Name-Test");
  // Set AdvertisingInterval
  BLE.setAdvertisingInterval(320); // 10 ms ,16 * 0.625 ms, units of 0.625 ms
  BLE.advertise();
}

void loop()
{
  static auto timeRef = millis();
  static auto count = 0;

  BLE.poll();
  if (millis() - timeRef > 5000) {
    count++;
    timeRef = millis();
    auto idx = count % 3;
    BLE.setDeviceName(names[idx]);
    BLE.setLocalName(names[idx]);
    BLE.advertise();
  }
}

Use Nrf connect for mobile and click the more info next to scan


Thank you !!

BSP2.9.2 is used.
The code I showed should be able to change the advertisement interval. However, the interval does not seem to change when I press the reset button on the Seeed xiao ble nrf52840 sense.

The receiver side was created as follows in Pycharm 3.12.

Is there a solution?

import asyncio
import matplotlib.pyplot as plt
from bleak import BleakClient
from bleak.exc import BleakError
import struct

address = “F4:64:70:0E:DB:C0”
UUID = “64cf7160-f89e-4ec0-b5c5-d10ad9b53bf2”

async def run(address, loop):.
data = [] # list to record incoming data

async with BleakClient(address, loop=loop) as client:.
    x = await client.is_connected()
    print("Connected: {0}".format(x))

    while True: try: try
        try: y = await client.read_connected()
            y = await client.read_gatt_char(UUID)
            z = struct.unpack('<f', y)[0])
            print("y: ", y, type(y), len(y))
            print("z: ", z)
            data.append(z) # add incoming data to list

            # plot
            plt.clf()
            plt.plot(data)
            plt.xlabel('Time')
            plt.ylabel('Data')
            plt.title('Real-time Data Plot')
            plt.grid(True)
            plt.pause(0.01) # pause to update the plot

        except BleakError as e:.
            print(f "Error reading characteristic: {e}")

loop = asyncio.get_event_loop()
loop.run_until_complete(run(address, loop))

Translated with DeepL.com (free version)