Main Content


Read one sample of acceleration from sensor

Since R2021a


[accelReadings,timestamp] = readAcceleration(sensorobj) returns one sample of the acceleration data on x, y, and z axes read from the sensor in units of m/s2 along with the timestamp. Timestamp is optional.


The readAcceleration function is available for the LSM6DS3, LSM6DS3H, LSM6DSL, LSM6DSM, LSM6DSR, LSM6DSO, MPU6050, MPU9250, LSM9DS1, LSM303C, ICM20948, ADXL345, and LIS3DH sensors.


collapse all

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)

Create the sensor object for the sensor in use.


The sample code and output in this example is for lsm9ds1 object. If you are using another sensor that supports readAcceleration function, use the corresponding sensor object.

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

  lsm9ds1 with properties:

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

Return one sample of acceleration data.

[accelReadings,timestamp] = readAcceleration(sensor);
accelReadings =


    0.1712   -0.7095    9.5843

timestamp = 


   9-Dec-2020 16:11:59.074

Since the timestamp output is optional, you can obtain only the acceleration data from the sensor.

[accelReadings] = readAcceleration(sensor);
accelReadings =


    0.1712   -0.7095    9.5843

Input Arguments

collapse all

The sensor object.

Output Arguments

collapse all

The acceleration value on x, y, and z axes read from the sensor.

The time at which MATLAB receives acceleration data from the sensor, specified as a datetime.

More About

collapse all

  • Timestamp value is always returned in seconds.

  • When you create the sensor object and do not specify the I2C address in the name-value pair, the default I2C address is considered by the sensor.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2021a