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.


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
a = 
  arduino with properties:

               Address: 'DEBD293F7CDC'
                  Name: 'LSMOverBLE'
             Connected: 1
                 Board: 'Nano33BLE'
         AvailablePins: {'D0-D13', 'A0-A7'}
  AvailableDigitalPins: {'D0-D13', 'A0-A7'}
      AvailablePWMPins: {'D0-D13'}
   AvailableAnalogPins: {'A0-A3', 'A6-A7'}
    AvailableI2CBusIDs: [0, 1]
AvailableSerialPortIDs: [1]
             Libraries: {'I2C', 'SPI', 'Servo'}
Show all properties

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)
lsmObj = 
  lsm9ds1 with properties:

                         I2CAddress: 107 ("0x6B")
                                   : 30 ("0x1E")
                                Bus: 1 
                             SCLPin: "SCL1" 
                             SDAPin: "SDA1" 
Show all properties all functions

Read acceleration.

acceleration = readAcceleration(lsmObj)
acceleration = 1×3

    2.6315   -7.8341    5.6433

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

Read angular velocity.

angularVelocity = readAngularVelocity(lsmObj)
angularVelocity = 1×3

    0.0053    0.0057    0.0078

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

Read magnetic field.

magneticField = readMagneticField(lsmObj)
magneticField = 1×3

   26.1100   27.0620   14.0140

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