Main Content

Free-Fall Detection Using LIS3DH Freefall Application Interrupt

This example shows how to use the Inertial freefall application interrupt of LIS3DH linear accelerometer connected to an Arduino® board to sense the free-fall of the sensor.

The sensor block is placed inside the downstream function-call subsystem. The output of the sensor block is sent to a MATLAB Function block where the data is averaged, and free-fall is detected.

Supported Arduino Hardware

  • Arduino Due

  • Arduino Leonardo

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Micro

  • Arduino MKR1000

  • Arduino MKR WIFI 1010

  • Arduino MKR ZERO

  • Arduino Nano 3.0

  • Arduino Robot Control Board

  • Arduino Robot Motor Board

  • Arduino Uno

  • Arduino Nano 33 IoT

Prerequisites

Before you start this example, complete the Get Started with Arduino Hardware example.

Required Hardware

  • Supported Arduino board

  • LIS3DH sensor

  • USB cable

  • Breadboard wires

Hardware Connection

  • Connect the INT1 pin of the LIS3DH sensor to pin 3 on the Arduino board (this example uses Arduino Mega 2560).

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

Hardware Configuration in the Model

Open the arduino_LIS3DH_freefalldetection Simulink model.

The model is preconfigured to work with Arduino Mega 2560. If you are using a different Arduino board, change the hardware board by performing the following steps:

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

2. Open the Hardware Implementation pane, and from the Hardware board list, select the type of Arduino board that you are using.

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

Configure Freefall Application Interrupt on LIS3DH for Interrupt Service Routine (ISR)

1. In the External interrupt handler area of the model, double-click the External Interrupt block, and ensure that the value of Pin number parameter is set to 3, which is same as the Arduino pin that is connected to INT1 pin on LIS3DH sensor. If required, you can change the pin. For example, if you connect INT1 to another pin that supports External Interrupt on the Arduino board. For more information on working with External Interrupts, see External Interrupts Workflow for Arduino.

2. The LIS3DH sensor block is placed in the downstream function call-sub system. The output of the block is connected to MATLAB® function block. The free-fall data acquired by the MATLAB function is averaged, and Free-fall of sensor is detected.

3. In AVR targets, for the I2C calls to work from within ISR, place sei(); inside the ISR. The sei() is added to the model using System Output block. The sei() is guarded to be included only for AVR targets and not the ARM targets. For more information, see Workaround for Interrupt Workflow with Sensors.

4. 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 code obtains real-time data from the hardware. The data acquisition and parameter tuning are done while the application is running on the hardware.

Note: Ensure that the Arduino board you are using has sufficient memory to run the application on hardware. The boards such as Arduino Uno, which has low memory, might not support this application.

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 3 on Arduino generates an interrupt on every rising edge of the signal, which triggers the function-call subsystem.

2. In this 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.