Main Content

Measure LSM9DS1 Sensor Outputs Using Nano 33 BLE Sense

This example shows how to use the MATLAB® Support Package for Arduino® Hardware to connect an Arduino Nano 33 BLE Sense board over Bluetooth® with an LSM9DS1.

The sensor is connected to the Arduino board over an I2C interface.

You can read data from the sensor using a computer that supports Bluetooth 4.0 or higher.

Overview

The lsm9ds1 sensor is a nine degrees of freedom (DOF) inertial measurement unit (IMU) used to read acceleration, angular velocity, and magnetic field in all three dimensions.

The sensor object represents a connection to the device on the Arduino hardware I2C bus. The LSM9DS1 sensor is present on the Nano 33 BLE Sense board. You can read the data from your sensor in MATLAB using the object functions.

You can also use other sensors like hts221, lps22hb, or apds9960 on the Arduino board to connect to MATLAB over Bluetooth.

To connect the Arduino board to MATLAB over Bluetooth, type arduinosetup in the MATLAB command line and set the connection type as Bluetooth. After you select the board and the port, in the Upload Arduino Server window, select I2C. For the complete workflow, see Connection over Bluetooth.

Connect to Sensor and Read Sensor Output

Create an Arduino object.

a = arduino

Create the sensor object. The LSM9DS1 sensor is connected to the I2C bus 1 of the Nano 33 BLE Sense board.

lsmObj = lsm9ds1(a,"Bus",1)

Read acceleration.

acceleration = readAcceleration(lsmObj)

This function returns one sample of acceleration data. For more information, see readAcceleration.

Read angular velocity.

angularVelocity = readAngularVelocity(lsmObj)

This function returns one sample of angular velocity data. For more information, see readAngularVelocity.

Read magnetic field.

magneticField = readMagneticField(lsmObj)

This function returns one sample of magnetic field data. For more information, see readMagneticField.

Clean up

Once the sensor connection is no longer needed, clear the associated object.

  clear lsmObj a