Communicating with Arduino Hardware Using XCP-Based External Mode Simulation

This example shows how to tune the parameters and monitor the signals of an algorithm running on the Arduino® Mega 2560 board using XCP-based External Mode simulation.

Supported Hardware

All Arduino boards as listed in the Supported Hardware section of Arduino Support from Simulink.


The provided model is preconfigured for Arduino Mega 2560 and can be run on any of the boards listed in the Supported Hardware section. To select a hardware board, follow the steps in the Configuring the Model for the Arduino Mega 2560 Board section.

Simulink® Support Package for Arduino Hardware enables you to monitor and tune algorithms running on Arduino board from the same Simulink models from which you developed the algorithms.

In this example, you will learn how to tune and monitor the algorithm in real time. When you are developing algorithms, it is often necessary to determine appropriate values of critical algorithm parameters in an iterative fashion. For example, a surveillance algorithm that measures motion energy in a room may use a threshold to detect an intruder in the presence of ambient noise. If the threshold value is set too low, the algorithm may erroneously interpret any movement as an intruder. If the threshold value is set too high, the algorithm may not be able to detect any movement at all. In such cases, you can try different threshold values until the desired algorithm performance is reached. This iterative process is called parameter tuning.

The Simulink External mode feature enables you to accelerate the process of parameter tuning by letting you change certain parameter values while the model is running on the target hardware, without stopping the simulation. When you change the parameter values from within Simulink, the modified parameter values are communicated to the target hardware immediately. The effects of the parameter tuning activity can be monitored by viewing algorithm signals on scopes or displays in Simulink.

Simulink provides these additional features for the targets that support XCP:

  • Dashboard objects such as Slider and Dashboard Scope. For more information, see Dashboard (Simulink).

  • Simulation Data Inspector for visualizing the logged signals. For more information, see Simulation Data Inspector.


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

Required Hardware

Arduino Mega 2560


The following figure shows the example model.

Configuring the Model for the Arduino Mega 2560 Board

  1. Open the arduino_xcp_externalmode model.

  2. Browse to Simulation > Model Configuration Parameters > Hardware Implementation.

  3. Select the required Arduino hardware from the Hardware board parameter list. Do not change any other settings.

  4. Click OK.

  5. This example model has one signal selected for logging. To select additional signals, right-click the required signal and select the Log Selected Signals option.

Running the Model in External Mode

1. Change the Simulation mode on the toolbar to External, and click the Run button.

At this point, your model is running on the Arduino Mega 2560 board and communicating with Simulink through External mode. You can select the Simulation Data Inspector button to view the logged signals, as shown.

2. You can tune the parameters and visualize the signals selected for logging by:

  • Double-clicking the Manual Switch block to change the input source.

  • Using the Slider to change the signal gain.

  • Double-clicking the Scope block to view the External mode simulation results.

The model runs in real-time on the target while performing these actions.