Main Content

Create ADXL343 Sensor Block to Read Acceleration Using IO Device Builder App

This example shows how to use the IO Device Builder app to create ADXL343 sensor to measure acceleration. In this example, the ADXL343 sensor values are retrieved by using the SPI bus requiring four wires (SCL, SDA, SDO and CS).

Prerequisites

Circuit Connections

To run this example, make these circuit connections.

  • For 5.0V LOGIC boards: Connect 5V on the Arduino®/Metro to VIN (red wire) on the ADXL343.

  • For 3.3V LOGIC boards: connect 3.3V on the Arduino to VIN on the ADXL343.

  • Connect GND on the Arduino to GND on the ADXL343.

  • Connect SDO to MISO, SDA to MOSI, SCL to SCLK and CS to digital pin 10 from the sensor to Arduino.

Open the IO Device Builder App

To open the IO Device Builder app, perform these steps.

  1. Start MATLAB® and then open / create a Simulink® model.

  2. On the Hardware tab of the Simulink toolstrip, in Prepare section, under Design, choose IO Device Builder.

Select Working Directory and Add Third-party Source Files

Once the Source files location page loads, select the working directory and add third-party source files.

On the Source files location page:

  1. Click Select and select the working directory. In the working directory, the generated system object, along with the corresponding cpp and h files, and the model are located.

  2. Click Add folder and add the folders you downloaded. Only the files present directly within the selected folder are included, and any files present within subfolders are excluded.

  3. Click Next to continue.

Select Source Files

On the Source files page, select the required source files and then click Next to continue.

Specify Block Parameters

On the Block parameters page:

  1. Specify the block name and add block description.

  2. Add mask parameter with the name accelRange, int8 as Data type, 3 as initial value, and Tunable as Type.

  3. Click Next to continue.

Select Outputs for the Block

On the Outputs page:

  1. Add Output port as x,y and z as mentioned in driver file and set data type as single and Dimension as [1,1].

  2. Click Next to continue.

Select Inputs for the Block

On the Inputs page:

  1. Remove inputs for the block, if any already added.

  2. Click Next to continue.

Preview Block

On the Block image page, view the preview of the block with the inputs and outputs you added. Click Next to continue.

Generate System Object Files

On the Generate page, the file generation location is displayed. Click Generate to generate the system object files and then click Finish in the Next Steps screen.

Next Steps

On the Next Steps page, the files generated are shown and the next steps to be performed are displayed.

Perform these steps.

1. The generated .cpp file opens automatically. Modify the generated .cpp file as shown below.

#include "C:\user\adxlSensor\adxlSensor.h"  // App generated header. Do not modify
#include "Wire.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_ADXL343.h"
#include "Arduino.h"

#define ADXL343_CS 10
Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_CS, &SPI, 12345);
void setupFunctionadXLT2(int8_T * accelRange,int size_vector__1){
    while (!Serial);
    Serial.println("ADXL343 Single Tap INT Tester"); Serial.println("");
    if(!accel.begin())
    {
        while(1);
    }
    accel.setRange(*accelRange);
}

void stepFunctionadXLT2(float * x,int size_vector_1,float * y,int size_vector_2,float * z,int size_vector_3){
    sensors_event_t event;
    accel.getEvent(&event);
    delay(10);
    *x=event.acceleration.x;
    *y=event.acceleration.y;
    *z=event.acceleration.z;
}

2. Assign the output to the selected output port.

3. Add the #include "Arduino.h" header and other required headers for the driver, as mentioned in ino example with the absolute address.

4. Open the generated Simulink model.

5. Add and connect Display blocks to the output ports.

6. In the Simulink model, navigate to Modeling > Model Settings and in the Configuration Parameters dialog box, click Hardware Implementation and then select an Arduino board.

A sample Simulink model is shown here.