Trigger Downstream Function-Call Subsystem Using Arduino External Interrupt Block

This example shows how to trigger a downstream function call subsystem by using an External Interrupt block from the Simulink® Support Package for Arduino® Hardware.

In this example, you will learn how to trigger the function call in Normal mode and External mode simulation.

  • Normal mode simulation: In Normal mode, you can verify the model design by simulating an input to the External Interrupt block. The External Interrupt block receives an input value of 0 or 1 at each time step. When the input at the block port is 1, the block executes the function call subsystem. The function call increments the value of a counter by 1 each time it is triggered.

  • External mode simulation: In External mode, pin 4 generates a pulse-width modulation (PWM) signal at pin 2 of the hardware at each time step. When pin 2 receives a rising edge as input, the block executes the function call to increment the value of the counter.

Supported Hardware

  • Arduino Due

  • Arduino Leonardo

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Micro

  • Arduino MKR1000

  • Arduino Nano 3.0

  • Arduino Robot Control Board

  • Arduino Robot Motor Board

  • Arduino Uno

Prerequisites

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

Required Hardware

  • Supported Arduino board

  • USB cable

  • Breadboard wires

Step 1: Configure the Simulink Model for Supported Arduino Hardware

1. Open the model by executing this command in MATLAB® Command Window.

open_system('arduino_external_interrupt_sim');

Step 2: Run the Model in Normal Mode Simulation

In the model, the SimIRQ port of the External Interrupt block is connected to input 0 of the Interrupt Source area. An input value of 1 indicates that the block triggers the function call to increment the counter value by 1 at each time step during simulation.

1. In the model toolbar, set the Simulation mode to Normal.

2. Click the Run button. The lower-left corner of the model window displays status while Simulink prepares to run the model on the computer.

3. Now toggle the Interrupt Source to input 1. Counter value gets incremented as long as SimIRQ is 1.

Step 3: Settings to Run the Model in External mode

1. Open the model by executing this command in MATLAB® Command Window. This model is preconfigured to run on Arduino Mega 2560. If you are using Arduino Mega 2560, skip the remaining steps in this section.

open_system('arduino_external_interrupt');

2. On the model toolbar, click the Model Configuration Parameters button.

3. In the Configurations Parameters dialog box, select Hardware Implementation.

4. From the Hardware board list, select the type of Arduino hardware that you are using. Do not change any other settings.

5. Click OK.

6. Connect the pulse generator pin 4 to the external interrupt pin 2 of the hardware. For information on pin mapping on the hardware, see Pin Mapping on Arduino Blocks.

7. You can use a readymade model that is already configured to run in External mode by executing open_system('arduino_external_interrupt_sim') in MATLAB Command Window. Otherwise, modify the model using these steps:

a. In the model toolbar, set the Simulation Mode to External and the Simulation stop time to 20.0 seconds.

b. Modify the Interrupt Source area of the model to generate PWM signal on pin 4 of the Arduino hardware.

c. Double-click the External Interrupt block and configure the block to generate an interrupt when pin 2 of the hardware receives a rising edge of the PWM signal.

    Parameter    | Value
  _ _ _ _ _ _ _ _ _ _ _ _ _
    Pin number   | 2
    Mode         | RISING

Step 4: Run the Model in External Mode Simulation

1. Click the Run button. The lower-left corner of the model window displays status while Simulink prepares, downloads, and runs the model on the hardware. During simulation, pin 4 generates a PWM signal on pin 2 of the hardware. The pin 2 generates an interrupt on every rising edge of the signal as increments the counter value by 1.

2. View the changing value of the counter at each time step by double-clicking the Scope block in the Display data area of the model. On the scope, the counter value is plotted againts the time value. The counter value is incremented by 1 as the value received at each time step is 1.

Other Things to Try

  • Change the value of the Mode parameter in the External Interrupt block and deploy the model again.

  • Use the signal generated from an encoder as a source for the external interrupt.