Main Content

adxl345

Connect ADXL345, ADXL343, ADXL344, or ADXL346 sensor on Raspberry Pi hardware I2C bus

Since R2023a

Add-On Required: This feature requires the MATLAB Support Package for Raspberry Pi Hardware add-on.

Description

The adxl345 object reads linear acceleration using the ADXL345, ADXL343, ADXL344, or ADXL346 sensor connected to the I2C bus of the hardware. The ADXL34x family of accelerometers are used to read acceleration in all three dimensions. Attach an ADXL34x sensor to the I2C pins on the hardware. You can read the data from your sensor in MATLAB® using the Object Functions.

Creation

Description

accelSensor = adxl345(mypi) creates a sensor object with default property values.

accelSensor = adxl345(mypi,Name,Value) creates a sensor object with properties using one or more Name,Value pair arguments.

Input Arguments

expand all

Raspberry Pi hardware connection created using raspi, specified as an object.

Example: accelSensor = adxl345(mypi) creates a connection to the ADXL345 sensor on the Raspberry Pi hardware represented as an object, raspi.

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 = adxl345(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 ADXL34x are connected to the same hardware board. ADXL34x can have two I2C addresses depending on the logic level on pin SA0 of the sensor.

Pin NamePin StateI2C Address
ALTLow0x53
High0x1D

You can specify the I2C address in:

  • Hexadecimal, decimal, or binary format

  • String of hexadecimal value of I2C Address

  • Character vector of hexadecimal value of I2C Address

0x53 is the I2C address of the ADXL345 connected to Raspberry Pi board.

If not specified, the object will be created with one of the available I2C device address in the table. Availability of I2C device with the expected address will be determined by using scanI2CBus.

Object Functions

readAccelerationRead one sample of acceleration from sensor

Examples

collapse all

Ensure that the I2C interface on the Raspberry Pi kernel is enabled and then create an adxl345 sensor object.

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)

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

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

  1×1 cell array
 

    {'0x53'}

Create an adxl345 sensor object on the I2C bus 'i2c-1' on address '0x53'.

accelSensor = adxl345(mypi,'Bus','i2c-1','I2CAddress','0x53');
accelSensor = 

  adxl345 with properties:

                         I2CAddress: 83 ("0x53")
                                Bus: i2c-1

Extended Capabilities

Version History

Introduced in R2023a