[SOLVED] IMU on Nano 33 BLE Sense Fails to Initialize With USBMouse Library

Hi all,

I have been working on making a gyroscope mouse with an Arduino Nano 33 BLE Sense board. I can move the mouse through simple code, such as mouse.move(20, 0), using the USBMouse library from Mbed OS. However, when I try to access other sensors on the board, such as the gyroscope, it seems that the IMU fails to initialize. This problem seems to have been reported here. The IMU.begin() function returns false if it fails to initialize, and it is returning false.

I have done some trials to try to figure out what is happening with the USBMouse and Arduino_LSM9DS1 libraries. The IMU initializes in the following code. Leaving the USBMouse object declaration in the setup does not allow the loop to run, since the red LED does not turn on. Commenting out the object declaration allows the loop to run. Adding the object declaration anywhere before the IMU.begin() causes the IMU to fail to initialize. I’m not sure why it doesn’t initialize in this case.

#include "USBMouse.h"
#include <Arduino_LSM9DS1.h>
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);


  digitalWrite(LEDB, LOW);//Show setup has started
  delay(5000);
  digitalWrite(LEDB, HIGH);


  if (!IMU.begin()) {
    digitalWrite(LEDR, LOW);//LED on if IMU fails to initialize
  }
  else {
    digitalWrite(LEDR, HIGH);//LED off if IMU initializes
  }


  delay(10000);


  float x, y, z;
  IMU.readGyroscope(x, y , z);


  Serial.println(x);
  Serial.println(y);
  Serial.println(z);


  delay(5000);


  USBMouse mouse(true);//Commenting this out causes onboard RGB LED to turn red, which implies loop() is run
}


void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LEDR, LOW);//LED on during loop
}

Any help would be appreciated.

What other information should I provide? To clarify, the main problem is that the IMU is not initializing when used with the USBMouse. I've done some tests with the USBKeyboard library as well and it does not work with the IMU either.

After creating an issue on GitHub, Mr. Facchin on the Arduino team was able to create a fix. For MacOS/Linux, you will probably only have to add one library in your code (Mbed core 1.3.0 should work):

#include "PluggableUSBHID.h"// Library that needs to be added
#include "USBMouse.h"
#include <Arduino_LSM9DS1.h>

USBMouse mouse;

void setup() {
  IMU.begin();
}

void loop() {
  float x, y, z;

  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(x, y, z);
    mouse.move(x, y);
  }
}

For Windows, the above code will work, but you will need to update to 1.3.1 of the Mbed core.

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