Transmission time , 2 Nano33 BLE ,example sketch

Hello ,Everyone.
I'm very new to BLE and started studying with the example sketches from the Arduino BLE library.
I used the items below to test the transmission time.
・2 Arduino Nano33 BLE
・Central: LedControl(modified)
Peripheral: LED(modified)
・distance:<10cm

Central code:

#include <ArduinoBLE.h>
unsigned long now;

// variables for button
const int buttonPin = 2;
int oldButtonState = LOW;
int buttonState;

void setup() {

  buttonState=1;
  Serial.begin(9600);
  while (!Serial);

  // configure the button pin as input
  pinMode(buttonPin, INPUT);

  // initialize the Bluetooth® Low Energy hardware
  BLE.begin();

  //set connection interval
   BLE.setConnectionInterval(0x0006, 0x0c80); // 7.5 ms minimum, 4 s maximum=0x0c80
  
  Serial.println("Bluetooth® Low Energy Central - LED control");

  // start scanning for peripherals
  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}

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();

    if (peripheral.localName() != "LED") {
      return;
    }

    // stop scanning
    BLE.stopScan();

    controlLed(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
  }
}

void controlLed(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;
  }

  // retrieve the LED characteristic
  BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");

  if (!ledCharacteristic) {
    Serial.println("Peripheral does not have LED characteristic!");
    peripheral.disconnect();
    return;
  } else if (!ledCharacteristic.canWrite()) {
    Serial.println("Peripheral does not have a writable LED characteristic!");
    peripheral.disconnect();
    return;
  }

  while (peripheral.connected()) {
    // while the peripheral is connected

    // read the button pin
    //int buttonState = digitalRead(buttonPin);
        
    if (oldButtonState != buttonState) {
      // button changed
      oldButtonState = buttonState;

      if (buttonState) {
        Serial.println("button pressed");

        // button is pressed, write 0x01 to turn the LED on
        now=micros();
        ledCharacteristic.writeValue((byte)0x01);
        Serial.println(micros()-now);
      } else {
        Serial.println("button released");

        // button is released, write 0x00 to turn the LED off
        now=micros();
        ledCharacteristic.writeValue((byte)0x00);
        Serial.println(micros()-now);
      }
    }

    delay(1000);
    if (buttonState==1){
      buttonState=0;
    }else if(buttonState==0){
      buttonState=1;
    }
    
  }

  Serial.println("Peripheral disconnected");
}

Peripheral:

#include <ArduinoBLE.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN; // pin to use for the LED

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

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);
  //add connection interval
  BLE.setConnectionInterval(0x0006, 0x0c80); // 7.5 ms minimum, 4 s maximum=0x0c80

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW);          // will turn the LED off
        }
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

The result is here.

Bluetooth® Low Energy Central - LED control
Found b2:1d:95:b2:c2:0b 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...
Connected
Discovering attributes ...
Attributes discovered
button pressed
32428
button released
25867
button pressed
19854
button released
17085
button pressed
22027
button released
29878
button pressed
29876
button released
29867
button pressed
29865
button released
29838
button pressed
29860
button released
21008
button pressed
25803
button released
29868
button pressed
24067
button released
29866
button pressed
21022
button released
16051
button pressed
32070
button released
29864
button pressed
29865
button released
29859
button pressed
29869
button released
29046
button pressed
19871
button released
19845
button pressed
36847
button released
29867
button pressed
25082
button released
19859
button pressed
29863
button released
29850
button pressed
17126
button released
21006
button pressed
19873
button released
29856
button pressed
19869
button released
19147
button pressed
25016
button released
29765
button pressed
1462098
Peripheral disconnected

It costs almost 20ms~30ms to transmit 2 bytes of data.
I'm wondering is it normal?
I meant I thought it was longer than I calculated.

What I have learned is the data throughput is influenced by several connection parameters and environment connection conditions.
Since I found that the Arduino BLE library supports Bluetooth® 4.0, I guess I should use the parameters below.
MTU=23byte
Data rate=1Mbps
connection interval=7.5ms (min)
Total transmission time will be ① or ② or ③.
①Packet(header23byte+ATT payload 2byte)8bit 184us)+IFS 150us+ACK 80us+IFS 150us=564us
②Packet(41bytes
8bit 328us)+IFS 150us+ACK 80us+IFS 150us=708us
③1 connection interval=7.5ms

Obviously the result(20~30ms) is 4 x or more than ①②③.
I know there should be some time for MCU to process the command.
But it is still longer than I expected.

Are there any mistakes in my code?
Is the understanding for calculation wrong?
Is it the actual speed of Arduino Nano33 BLE?

Please guide me if I have something wrong.
Thanks a lot for reading this topic.

That does not sound unreasonable to me, although I have not used the Nano 33 BLE.

BLE is designed for low power, infrequent transmissions of tiny amounts of data, like a heart rate every minute or two, and has huge overhead. Why do you care if it takes 30 ms to transmit a couple of bytes?

If you take a few moments to describe what you really want to do, and are willing to consider other options, there are easier and faster ways to send data by radio, over much longer distances.

I'm so excited to see your reply . I really appreciated it.
Let me describe my project.
I'd like to transmit sensor data to a PC/tablet via wireless.
The plan is 48 sensors with 2byte data each ==>48*2=96byte
The scan interval of the sensor is the shorter the better. It is expected to be under 30~40ms.
In the future ,I wish I could use a battery-power.
BLE is a good choice for battery-powered devices.

Moreover, I read this article .
https://novelbits.io/bluetooth-5-speed-maximum-throughput/
Bluetooth 5 speed: How to achieve maximum throughput for your BLE application

Roughly calculated my target can be easily achieved.
96byte*8bit/232.29kpbs=3.3ms <7.5ms
That told me 1 connection 7.5ms for each transmission is enough.

So I used Nano33BLE to do the transmission time test to see if it can achieve my specification.
But I got an unexpected result.

First of all, I want to know is it the limitation of BLE ? or the limitation of the Nano33 BLE board?
or there's something missing in my code?

By the way, I'm willing to consider other wireless options.
I look forward to hearing from you your recommendations.
Thank you very much.

Most likely, the bottleneck is in your code(*) and/or the Arduino implementation of the BLE protocol. Arduino has recently been rushing out products that are not thoroughly tested and don't meet user expectations. The Nano 33 BLE series is among those.

(*) Are you aware that it takes 1 millisecond to transmit 1 character at 9600 Baud?

 Serial.begin(9600);

On the other hand, BLE is not designed for a project like yours. ANY project with 48 wireless nodes, with expectations of high data throughput will be a major challenge. What is the plan for message collisions?

I think you need to back off, do some more research on basic project requirements, then decide what sort of radio and network protocol might work for it.

Hi jremington,

Thank you for your information.
That's really helpful.

I tested at 115200 Baud as you mentioned the serial speed.
It looks like there is no big difference between 9600 and 115200.

What is the plan for message collisions?

hahaha, No plan. I knew nothing about wireless 1 month ago.
Still studying. Next ,I'd like to do some tests using Bluetooth classic or WIFI .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.