Using HTS221 with BLE seems to require LSM9DS1?

Hi all,

I'd like to get temperature and humidity readings from a Nano 33 BLE Sense via bluetooth. The following code works fine:

#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h> // even if we aren't using the accelerometer, its library needs to be included?
#include <Arduino_HTS221.h>

int humX = 1;
int tempY = 1;
float x, y;

BLEService customService("1101");
BLEUnsignedIntCharacteristic customXChar("2101",
  BLERead | BLENotify);
BLEUnsignedIntCharacteristic customYChar("2102",
  BLERead | BLENotify);

void read_temphum() {
  x = HTS.readHumidity();
  y = HTS.readTemperature();
  humX = x*100;
  tempY = y*100;
}


void setup() {
  IMU.begin(); // Without this line (and the associated include LSM9DS1.h at the top) arduino returns 0 - 0 and freezes on BLE connect
  HTS.begin();
  Serial.begin(9600);
  while (!Serial);

  pinMode(LED_BUILTIN,OUTPUT); // Ready the onboard LED

  // BLE error catching
  if (!BLE.begin()){
    Serial.println("BLE failed to initiate");
    while(1);
  }

  BLE.setLocalName("arduinoTsensor");
  BLE.setAdvertisedService(customService);
  customService.addCharacteristic(customXChar);
  customService.addCharacteristic(customYChar);
  BLE.addService(customService);
  customXChar.writeValue(humX);
  customYChar.writeValue(tempY);

  BLE.advertise();

  Serial.println("Bluetooth device now active, waiting for connect...");


}

void loop() {
  BLEDevice central = BLE.central();
  if (central){
    Serial.print("Connected to central: ");
    Serial.println(central.address());
    digitalWrite(LED_BUILTIN, HIGH);
    
    while (central.connected()){
      delay(1000);
      read_temphum();

      customXChar.writeValue(humX);
      customYChar.writeValue(tempY);

      Serial.print("At main function");
      Serial.println("");
      Serial.print(humX);
      Serial.print(" - ");
      Serial.println(tempY);
      Serial.println("");
      Serial.println("");

    }

    digitalWrite(LED_BUILTIN,LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());

  }

}

But, if I comment out or delete both:

  • the inclusion of the Arduino_LSM9DS1.h library, and;
  • the IMU.begin() statement;

then Arduino returns "0 - 0" upon BLE connect and then freezes.

Can anyone recreate this issue or help me to understand why it's happening?

To be explicit, the following code fails:

#include <ArduinoBLE.h>
#include <Arduino_HTS221.h>

int humX = 1;
int tempY = 1;
float x, y;

BLEService customService("1101");
BLEUnsignedIntCharacteristic customXChar("2101",
  BLERead | BLENotify);
BLEUnsignedIntCharacteristic customYChar("2102",
  BLERead | BLENotify);

void read_temphum() {
  x = HTS.readHumidity();
  y = HTS.readTemperature();
  humX = x*100;
  tempY = y*100;
}


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

  pinMode(LED_BUILTIN,OUTPUT); // Ready the onboard LED

  // BLE error catching
  if (!BLE.begin()){
    Serial.println("BLE failed to initiate");
    while(1);
  }

  BLE.setLocalName("arduinoTsensor");
  BLE.setAdvertisedService(customService);
  customService.addCharacteristic(customXChar);
  customService.addCharacteristic(customYChar);
  BLE.addService(customService);
  customXChar.writeValue(humX);
  customYChar.writeValue(tempY);

  BLE.advertise();

  Serial.println("Bluetooth device now active, waiting for connect...");


}

void loop() {
  BLEDevice central = BLE.central();
  if (central){
    Serial.print("Connected to central: ");
    Serial.println(central.address());
    digitalWrite(LED_BUILTIN, HIGH);
    
    while (central.connected()){
      delay(1000);
      read_temphum();

      customXChar.writeValue(humX);
      customYChar.writeValue(tempY);

      Serial.print("At main function");
      Serial.println("");
      Serial.print(humX);
      Serial.print(" - ");
      Serial.println(tempY);
      Serial.println("");
      Serial.println("");

    }

    digitalWrite(LED_BUILTIN,LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());

  }

}

Cheers!

I had the same issue a while ago and left myself a note in an example project. Just found it.

// Without Serial when using USB power bank HTS sensor seems to need some time for setup
delay( 10 );

So, you can ether call the HTS.begin after your Serial setup or insert a delay before. The while(!Serial) will create enough delay because it waits for you to start the Serial Monitor.

I noted the delay in your BLE loop. Have a look into the following example.

File -> Examples -> 02. Digital -> BlinkWithoutDelay

This will help you modifying your code so you can run parts of your code (in your case updating the sensor values) in an interval without using delays.

Especially with communication stacks you should not use delays. The delay function just wastes cycles on most Arduinos and puts the processor to sleep under mbedOS. You should call the functions of the stack regularly to ensure the user has a nice experience.

Thanks for the help Klaus — it's now working great!

Updated code based on your advice is below, in case anyone else is facing the same issue.

#include <ArduinoBLE.h>
#include <Arduino_HTS221.h>

int humX = 1;
int tempY = 1;
float x, y;

// Avoid using delay(ms) in the main loop with BLE. Using millis(): i) improves time accuracy (keeps the update rate independent
// of execution time), and ii) allows other code to run while waiting for an update.
const long readInterval = 1000;   // how often the user gets a reading update (ms)
unsigned long lastUpdate = 0;    // stores last time user was updated (ms)

BLEService customService("1101");
BLEUnsignedIntCharacteristic customXChar("2101",
  BLERead | BLENotify);
BLEUnsignedIntCharacteristic customYChar("2102",
  BLERead | BLENotify);

void read_temphum() {
  x = HTS.readHumidity();
  y = HTS.readTemperature();
  humX = x*100;
  tempY = y*100;
}


void setup() {
  
  Serial.begin(9600);
  while (!Serial);
  pinMode(LED_BUILTIN,OUTPUT); // Ready the onboard LED

  // BLE error catching
  if (!BLE.begin()){
    Serial.println("BLE failed to initiate");
    while(1);
  }

  // important: HTS.begin() must be called after while (!Serial). This is because HTS sensor requires time to set itself up.
  // If not using serial set up, then add delay(10) prior to HTS.begin() instead.
  HTS.begin();

  BLE.setLocalName("arduinoTsensor");
  BLE.setAdvertisedService(customService);
  customService.addCharacteristic(customXChar);
  customService.addCharacteristic(customYChar);
  BLE.addService(customService);
  customXChar.writeValue(humX);
  customYChar.writeValue(tempY);

  BLE.advertise();

  Serial.println("Bluetooth device now active, waiting for connect...");


}

void loop() {
  BLEDevice central = BLE.central();
  if (central){
    Serial.print("Connected to central: ");
    Serial.println(central.address());
    digitalWrite(LED_BUILTIN, HIGH);
    
    while (central.connected()){
            
      unsigned long timeNow = millis();             // get current time

      if (timeNow - lastUpdate >= readInterval) {   // is it time for an updated reading?
        lastUpdate = timeNow;
        read_temphum();

        customXChar.writeValue(humX);
        customYChar.writeValue(tempY);

        Serial.print("At main function");
        Serial.println("");
        Serial.print(humX);
        Serial.print(" - ");
        Serial.println(tempY);
        Serial.println("");
        Serial.println("");
      }
    }

    digitalWrite(LED_BUILTIN,LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());

  }

}

When I add a blue tooth device in Windows 10, I see arduinoTensor. I click on it and it says Connecting...

16:29:24.318 -> Bluetooth device now active, waiting for connect...
16:31:58.080 -> Connected to central: f8:28:19:89:ff:de
16:31:58.080 -> At main function
16:31:58.080 -> 3638 - 3004
16:31:58.114 ->
16:31:58.114 ->
16:31:59.107 -> At main function
16:31:59.107 -> 3566 - 3125
16:31:59.107 ->
16:31:59.107 ->
16:32:27.083 ->
16:32:28.077 -> Disconnected from central: f8:28:19:89:ff:de
16:32:28.180 -> Connected to central: f8:28:19:89:ff:de
16:32:28.214 -> At main function
16:32:28.214 -> 3550 - 3129
16:32:28.214 ->
16:32:28.214 ->
16:32:29.210 -> At main function
16:32:29.210 -> 3549 - 3129
16:32:29.210 ->

So, what do I need to do to make it connect???

@gurthang99 Please create your own post with a new subject and ensure we get all information we need to help you. Good post usually get attention in the forum.

Please read the "How to post .." for tips on how to post code, what information to add. It is at the beginning of each sub forum.