Main Content


Connect to LSM6DSL sensor on Raspberry Pi hardware I2C bus

Since R2021a


The lsm6dsl object reads ambient temperature, and linear acceleration and angular velocity along the X, Y, and Z axes using the STMicroelectronics® I2C-based LSM6DSL inertial measurement unit (IMU) sensor interfaced with the Raspberry Pi® hardware. This object represents a connection from MATLAB® to the LSM6DSL sensor connected on the Raspberry Pi hardware board I2C pins I2C1_SDA (GPIO 2) and I2C1_SCL (GPIO 3). Before you use the lsm6dsl object, ensure that the I2C interface is enabled on your Raspberry Pi kernel. You can then create a connection to the Raspberry Pi board using the raspi object and set its properties. For more information, see enableI2C and raspi.




sensor = lsm6dsl(mypi) creates a LSM6DSL sensor object with default property values. The object represents a connection to the sensor on the Raspberry Pi hardware mypi.


sensor = lsm6dsl(mypi,Name,Value) creates a LSM6DSL sensor object with properties using one or more Name,Value arguments.

Input Arguments

expand all

Raspberry Pi hardware board connection specified as a raspi object.

Example: sensor = lsm6ds1(mypi) creates a connection to the LSM6DSL sensor on the Raspberry Pi object mypi.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

I2C bus address of the Raspberry Pi hardware board.

Example: sensor = lsm6dsl(mypi, 'Bus', 'i2c-0') creates the sensor object on the 'i2c-0' bus of the Raspberry Pi board.

Data Types: char

I2C address of the LSM6DSL sensor. This is the address of the sensor when multiple sensors are connected to the same hardware board. LSM6DSL sensor has two peripheral addresses that depend on the logic level of pin SDO/SA0 of the sensor. For example, if SDO/SA0 is low, the I2C address is '0x6A' and if SDO/SA0 is high, the I2C address is '0x6B'.

Example: sensor = lsm6dsl(mypi,'I2CAddress','0x6A'), '0x6A' is the I2C address of the accelerometer and gyroscope of LSM6DSL.

Data Types: string | integer | hexadecimal | binary | char

Object Functions

readTemperatureRead one ambient temperature data sample from LSM6DSL sensor
readAccelerationRead one acceleration data sample from LSM6DSL sensor
readAngularVelocityRead one angular velocity data sample from LSM6DSL sensor


collapse all

Ensure that the I2C interface on the Raspberry Pi kernel is enabled and then create an lsm6dsl sensor object. Follow these steps to enable the I2C interface in the Raspberry Pi hardware kernel.

Open the Raspberry Pi Linux® terminal.

Open the Raspberry Pi Software Configuration Tool dialog box.

sudo raspi-config

Select Interfacing Options > I2C.

Raspberry Pi Kernel I2C Interfacing options selection on Linux terminal

Select Yes when prompted to enable the I2C interface.

Raspberry Pi Kernel I2C Interfacing enabling options

Select Yes when prompted to automatically load the I2C kernel module.

Select Finish.

Select Yes when prompted to reboot.


You can also enable the I2C interface using the Raspberry Pi Resource Monitor App. For more information, see Enabling External Peripherals on Raspberry Pi Hardware Kernel.

Create a connection from MATLAB to the Raspberry Pi hardware board.

mypi = raspi;
mypi = 

  Raspi with Properties:

           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18734
               BoardName: 'Raspberry Pi 3 Model B+'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [4,5,6,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
    AvailableSPIChannels: {‘CE0’,’CE1’}
       AvailableI2CBuses: {'i2c-1'}
             I2CBusSpeed: 100000
        AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)

Get the addresses of the LSM6DSL sensor connected to the I2C bus 'i2c-1'.

sensor = scanI2CBus(mypi,'i2c-1');
ans =

  1×1 cell array


Create an lsm6dsl sensor object on the I2C bus 'i2c-1' on address '0x6B'.

sensor = lsm6dsl(mypi,'Bus','i2c-1','I2CAddress','0x6B');
sensor = 

  lsm6dsl with properties:

                         I2CAddress: 107 ("0x6B")
                                Bus: i2c-1

Extended Capabilities

Version History

Introduced in R2021a