Main Content

lsm9ds1

Connect LSM9DS1 sensor on Raspberry Pi hardware I2C bus

Since R2021a

Description

The lsm9ds1 object reads linear acceleration, angular velocity, and magnetic field along the X, Y, and Z axes using the STMicroelectronics® I2C-based LSM9DS1 inertial measurement unit (IMU) sensor interfaced with the Raspberry Pi® hardware. Before you use the lsm9ds1 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 raspi.

Creation

Description

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

example

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

example

Input Arguments

expand all

Raspberry Pi hardware board connection specified as a raspi object.

Example: sensor = lsm9ds1(mypi) creates a connection to the LSM9DS1 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 = lsm9ds1(mypi, 'Bus', 'i2c-1') creates the sensor object on the 'i2c-1' bus of the Raspberry Pi board.

Data Types: char

Specify the I2C address of the sensors when multiple I2C devices with the expected address for LSM9DS1 are connected to the same hardware board. LSM9DS1 can have multiple peripheral addresses which depends on the logic level on pins SDO_A/G and SDO_M of the sensor.

SDO_A/G Pin StateSDO_M Pin StateI2C Address
LowLow[0x6A, 0x1C]
LowHigh[0x6A, 0x1E]
HighLow [0x6B 0x1C]
HighLow[0x6B, 0x1E]

You can specify the I2C address in:

  • Numeric array of hexadecimal, decimal, or binary format of I2C Addresses

  • String array of hexadecimal value of I2C Addresses

  • Cell array of character vector of hexadecimal value of I2C Addresses

Example: imu = lsm9ds1(mypi,'I2CAddress',{'0x6A','0x1C'})

'0x6A' is the I2C address of the accelerometer and gyroscope of LSM9DS1. '0x1C' is the I2C address of the magnetometer of LSM9DS1.

If not specified, the object will be created with one of the available I2C device address mentioned in the table. To check the availability of I2C device with the expected address use scanI2CBus.

Object Functions

readAccelerationRead one sample of acceleration from sensor
readAngularVelocityRead one sample of angular velocity from sensor
readMagneticFieldRead one sample of magnetic field from sensor

Examples

collapse all

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

You can enable the I2C interface using:

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

mypi = raspi('raspberrypi-hysdu8X38o','rocky','bullwinkle')
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)

GPIO header pins I2C_SDA (GPIO 2) and I2C_SCL (GPIO 3) are used for I2C functionality. Display pin mapping of the Raspberry Pi hardware board.

showPins(mypi);
Raspberry Pi 3 Model B+ GPIO Pin Map diagram

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

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

  1×2 cell array
 

    {'0x6A'}    {'0x1C'}

Create an lsm9ds1 sensor object on the I2C bus 'i2c-1' on addresses '0x6A' and '0x1C'.

sensor = lsm9ds1(mypi,'Bus','i2c-1','I2CAddress',{'0x6A','0x1C'});
sensor = 

  lsm9ds1 with properties:

            I2CAddress: 106 ("0x6A")
                      : 28 ("0x1C")
                   Bus: "i2c-1"
                SCLPin: 3
                SDAPin: 2

Show all properties all functions

Extended Capabilities

Version History

Introduced in R2021a