Main Content

readMagneticField

Read one magnetic field data sample from LSM9DS1 sensor

Description

example

[magReadings,timestamp] = readMagneticField(sensor) returns one sample of the magnetic field data from the LSM9DS1 IMU sensor. The magnetic field sample is returned as a 3-by-1 vector in μT (microtesla) along the X, Y, and Z axes. The function also returns the timestamp at which MATLAB® receives the magnetic field data from the LSM9DS1 sensor. The timestamp output is optional.

Examples

collapse all

Read the magnetic field data from the LSM9DS1 IMU sensor on the X, Y, and Z axes.

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)

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

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

  lsm9ds1 with properties:

                         I2CAddress: 106 ("0x6A")
                                   : 28 ("0x1C")
                                Bus: i2c-1 

Return one sample of magnetic field data read from the LSM9DS1 sensor on the X, Y, and Z axes.

[magReadings,timestamp] = readMagneticField(sensor);
magReadings =

  data

    9.1980    7.0980  -52.9480


timestamp = 

  datetime

   9-Dec-2020 16:13:28.573

Since the timestamp output is optional, you can obtain only the magnetic field data from the LSM9DS1 sensor.

[magReadings] = readMagneticField(sensor);
magReadings =

  data

    9.1980    7.0980  -52.9480

Input Arguments

collapse all

LSM9DS1 sensor object connection to the Raspberry Pi hardware board, specified as a lsm9ds1 object.

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

Output Arguments

collapse all

Read the magnetic field data from the LSM9DS1 sensor on the X, Y, and Z axes.

Read the date and time at which MATLAB receives the magnetic field sample value from the LSM9DS1 sensor, specified as a datetime.

Extended Capabilities

Introduced in R2021a