Turning an Arduino Nano 33 BLE into a Tilt-Controlled Bluetooth Mouse

Using the board’s built-in IMU and BLE to move the cursor by tilting it, with no extra hardware.
Signal Processing
Author
Published

May 7, 2021

The Arduino Nano 33 BLE has two things built in that make a wireless mouse possible: an IMU (the LSM9DS1, with an accelerometer, gyroscope and magnetometer) and Bluetooth Low Energy. I wanted to see if I could tilt the board to move the cursor, with nothing else attached.

Code: Zilch123/BLE-IMU-Mouse

Getting the board to act as a mouse

To a computer, a mouse is a HID (Human Interface Device). Over Bluetooth, the board has to advertise itself as a HID mouse and send movement reports in the format the computer expects.

Writing that from scratch is a lot of work. The mbed-ble-hid library by tcoppex does it for the Nano 33 BLE, and getting it working alongside the IMU library was most of the effort (about 5 hours). Once it was set up, sending movement is two lines:

Nano33BleMouse mouse("Ble Mouse");

auto *hid = mouse.hid();
hid->motion(dx, dy);
hid->SendReport();

The board then shows up as “Ble Mouse” in the computer’s Bluetooth settings and pairs like any other mouse.

From tilt to cursor movement

When the board is still, the only acceleration it measures is gravity. Which way gravity points tells you how the board is tilted, so roll and pitch can be calculated from the accelerometer alone:

IMU.readAcceleration(xAcc, yAcc, zAcc);

if(zAcc>0){sign  = 1;}
else{sign  = -1;}
roll  = atan2( yAcc,sign* sqrt(zAcc*zAcc+ miu*xAcc*xAcc)) * 180.0/M_PI;
pitch  = atan2(-xAcc, sqrt(yAcc*yAcc + zAcc*zAcc)) * 180.0/M_PI;

The small miu term keeps the roll calculation stable when the board is close to vertical, where the plain formula would divide by almost zero.

Roll then drives horizontal movement and pitch drives vertical movement, scaled by a sensitivity factor:

hid->motion(roll *sensitivity , pitch *sensitivity);

Tilt further, and the cursor moves faster. Hold the board flat, and it stops. It works more like a joystick than a regular mouse.

What’s left

This is a bare-minimum working version, and a few things would make it usable day to day:

  • Filtering. The accelerometer is noisy, and any hand movement adds acceleration on top of gravity, so the cursor jitters. Fusing it with the gyroscope (a complementary or Madgwick filter, which the sketch already includes but doesn’t use yet) would smooth this out.
  • A dead zone. A few degrees of tilt around flat should count as zero, so the cursor stays still when you’re not trying to move it.
  • Better mapping. Mapping the angle to cursor speed is simple, but mapping it to cursor position can feel more natural, like pointing.
  • Clicks. There’s no button yet. A tap detected by the accelerometer, or a small physical button, would cover that.

The same idea has more serious uses. A tilt-controlled mouse can be worn on the head or hand by someone who can’t use a regular mouse, which links back to the rehab game controllers I’d worked on earlier.