Read Data from ICM20948 Sensor Using Data Ready Interrupt
This example shows how to use the interrupt generated by ICM20948 IMU Sensor block to trigger a downstream function-call subsystem that reads acceleration and angular velocity data from the ICM-20948 sensor connected to an Arduino® board.
The execution of the selected code as part of an interrupt service routine (ISR) requires that the ISR be kept as short as possible. This can be achieved by ensuring that there are no input variables or returned values to read sensor data as part of the downstream function-call subsystem, and by using only global variables that can pass data between an ISR and the main program.
Simulink® provides the option to use data store that allows access to signal data from anywhere in a model. Data stores are analogous to global variables in programs.
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
TDK ICM-20948 sensor
USB cable
Breadboard wires
Hardware Connection
Connect the INT1 pin of the ICM-20948 sensor to pin 2 on the Arduino board.
Connect the SDA, SCL, 3.3V, and GND pin of the Arduino board to the respective pins on the ICM-20948 sensor.
Hardware Configuration in the Model
Open the arduino_ICM20948_readUsingInterrupt
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. Click Hardware Settings in the Hardware tab of the Simulink toolbar.
2. In the Configurations Parameters dialog box, select Hardware Implementation.
3. From the Hardware board list, select the type of Arduino board that you are using.
4. Click Apply. Click OK to close the dialog box.
Configure the Data Ready Interrupt on ICM20948 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 2, which is the same Arduino pin that is connected to INT1 pin on ICM20948 sensor. You can change the pin if required, if you connect INT1 to another pin that supports External Interrupt on the Arduino board.
2. Define the data store. This example model uses ISRFired as the name of the data store, which provides access to signal data from anywhere in the model. The initial value of ISRFIred is set as 0.
The isr_handler in the function-call subsystem sets the value of ISRFired variable as 1. The ISRFired variable is used to trigger an enabled subsystem which reads the data from the sensor.
Inside the enabled subsystem, the ICM20948 IMU Sensor block is used to read the sensor value. After reading the data, ISRFired is written a value of 0.
You need to ensure that reading of the data is done before writing ISRFired value, by verifying the execution order (on the Debug tab in Simulink, select Information Overlays and then select Execution Order). To modify the execution order if required, right click the block, select Properties and then set the Priority value to 1.
3. In the Read sensor data on interrupt area of the model, configure the Enabled Subsystem that contains the ICM20948 IMU Sensor block which reads data from the sensor. This subsystem is triggered by a Data Store Read block, which also uses the same data store name, ISRFired.
Ensure that the Generate data ready interrupt parameter is selected, and also ensure that the data store read block executes at a faster rate than the expected data update from the sensor. In this example, data update is expected at 100 Hz (the specified ODR) and ISRFired is read at every 200 Hz.
Run the Model in External Mode (Monitor and Tune)
In External mode, the model is deployed as a C code on the hardware. The code obtains real-time data from the hardware. In external mode, 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 like Arduino Uno, which have low memory, cannot support this application.
1. On the Hardware tab of the Simulink model, 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 2 on Arduino generates an interrupt on every rising edge of the signal, which triggers the function-call subsystem.
2. Move the sensor and verify the changing values at each time step by double-clicking the Display blocks in the Display data area of the model.