Main Content

Free-Fall Detection Using LIS3DH Freefall Application Interrupt

This example shows how to use the Simulink® Coder™ Support Package for STMicroelectronics® Nucleo Boards to sense the free-fall of the sensor using the Inertial freefall application interrupt of LIS3DH linear accelerometer connected to a STMicroelectronics Nucleo board.

Prerequisites

We recommend completing the following examples:

Required Hardware

To run this example, you will need the following hardware:

  • Supported STMicroelectronics Nucleo board

  • USB cable

  • Breadboard wires

Hardware Connection

  • Connect the INT1 pin of the LIS3DH sensor to pin D5 on the STMicroelectronics Nucleo board.

  • Connect the SDA, SCL, 3.3V, and GND pin of the STMicroelectronics Nucleo board to the respective pins on the LIS3DH sensor.

Hardware Configuration in the Model

Open the stnucleo_i2c_lis3dh_automaticchutetriggering Simulink model.

The model is preconfigured to work with STM32 Nucleo F767ZI. If you are using a different STMicroelectronics Nucleo board, change the hardware board by performing the following steps:

1. Navigate to Modeling > Model Settings to open the Configuration Parameters dialog box.

3. Open the Hardware Implementation pane, and from the Hardware board list, select the STMicroelectronics Nucleo board that you are using.

3. Click Apply. Click OK to close the dialog box.

Configure the Free-fall Detection on LIS3DH for Interrupt Service Routine (ISR)

1. In the model, open the External Interrupt block, and ensure that the parameters are set as shown in the following image.

The LIS3DH sensor block is placed in the Initialize Function subsystem.

2. In ST Nucleo targets, for the I2C calls to work from within ISR, the interrupt priority of the external interrupt must be modified. The interrupt priority is modified using the System Outputs block. In this example, since D5 is configured as the external interrupt, NVIC_SetPriority(EXTI4_IRQn,0xFF); is added to the System Outputs block. The IRQn can vary with respect to targets and pins. You can modify the IRQn based on the pin and target selection.

3. Ensure that the Free-fall is selected for Detect parameter and also ensure that Generate Interrupt parameter is selected and INT1 is selected for Interrupt pin.

Initiate Monitor and Tune Action for the Model

During Monitor and Tune action, the model is deployed as a C code on the hardware. The code obtains real-time data from the hardware. The data acquisition and parameter tuning are done while the application is running on the hardware.

1. On the Hardware tab, in the Mode section, select Run on board (External Mode) and then click Monitor & Tune.

The lower-left corner of the model window displays status while Simulink prepares, downloads, and runs the Simulink model on the hardware. During simulation, the pin D5 on STMicroelectronics Nucleo board generates an interrupt on every rising edge of the signal, which triggers the function-call subsystem.

2. In the example, the threshold is set to 7.0 m/s^2 so the setup has to be dropped from a distance of 1m above the ground. If the threshold is lowered, increase the drop height appropriately.