MATLAB Examples

Parameter Tuning and Signal Logging with Serial External Mode on STMicroelectronics Nucleo Board

This example shows how to tune the parameters and monitor the signals of an algorithm running on STMicroelectronics Nucleo Board.


Available versions of this example:

STM Nucleo: stmnucleo_communication.slx

The provided model is pre-configured for STMicroelectronics Nucleo F401RE Board and you can run on any of the board listed in the "Supported Hardware" section, by changing the Configuration Parameters > Hardware Implementation > Hardware board of the model as described in Task 3 of this example. However for STMicroelectronics Nucleo F031K6 external mode is not supported due to memory constraint on the processor.


Simulink Coder Support Package for STMicroelectronics Nucleo Boards enables you to monitor and tune algorithms running on STMicroelectronics Nucleo 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 as it is executing. 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 determine 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, the right threshold value may be obtained by trying different values until the desired algorithm performance is reached. This iterative process is called parameter tuning. Simulink's 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 target hardware, without stopping the model. When you change parameter values from within Simulink, the modified parameter values are communicated to the target hardware immediately. The effects of the parameters tuning activity may be monitored by viewing algorithm signals on scopes or displays in Simulink. This example introduces the Simulink External mode feature by showing you how to:

  • Set up communication between Simulink and STMicroelectronics Nucleo Board.
  • Use a Simulink model to tune parameters of an algorithm that is running on STMicroelectronics Nucleo Board.
  • Use Simulink scopes to monitor the state of an algorithm running on STMicroelectronics Nucleo Board.


We recommend completing Getting Started with STMicroelectronics Nucleo Board example.

Required Hardware

To run this example you will need the following hardware:

  • Supported STMicroelectronics Nucleo board
  • USB cable type A to Mini-B cable


The following figure shows the example model:

Task 1 - Configure and Run the Model on STMicroelectronics Nucleo Board

In this task, you will configure and run your model on the STMicroelectronics Nucleo Board .

1. Open the communication model. This model has been configured for the Nucleo F401RE board. Follow the steps shown in the figure below to configure the model for the appropriate Nucleo board:

2. Connect the STMicroelectronics Nucleo Board to your computer with a USB cable.

3. Open the Configuration Parameters in the model. Set the Hardware Implementation->Hardware board parameter to STM Nucleo board which is connected to your computer.

4. In Configuration Parameters > Hardware Implementation > Target Hardware Resources > External Mode > Serial port enter the COM port of the serial interface on Windows. For reference look at the figure below:

5.. Click Apply and OK.

Task 2 - Simulate the Model

To simulate the model, follow these steps:

1. Observe that the model plays the motion energy recorded in a room and compares it with a threshold to detect intrusion.

2. In the model, change the Simulation mode on the toolbar to Normal. This tells Simulink to run the model on the host computer. See Task 4 below to run the model on the STMicroelectronics Nucleo Board.

3. In the model, click the Run button in the Simulink toolbar.

4. Double-click the Scope block. Observe that the algorithm detects multiple intrusions.

5. Click Stop button in the Simulink model.

Task 3 - Run the Model in External Mode

1. In the model, change the Simulation mode on the toolbar to External as below:

2. In the model, click the Run button on the toolbar.

Task 4 - Communicate with the Model

At this point, your model is running on STMicroelectronics Nucleo Board. As the model runs on hardware, it communicates with Simulink model in External mode using serial connection over USB.

1. Notice that the LED attached to pin 13 glows constantly. This means that the selected threshold is too low and that the algorithm misinterprets even a minor motion energy change as an intrusion. You need to find a more optimal value of the threshold.

2. Double-click the Threshold block in the model, increase its value, and click OK or Apply. This changes the threshold value in the model running on the board.

3. Check whether the glowing pattern of the LED has changed. The LED should light up less frequently in a correctly tuned algorithm.

4. If there is no change in LED light pattern, repeat the Steps 2 and 3 until you find the right value of the threshold.

5. Click Stop button in the Simulink model.

Other Things to Try

Monitor other signals in the model. For example, add another scope to monitor the value of the recorded motion energy.


This example showed a workflow for tuning and monitoring an algorithm running on STMicroelectronics Nucleo Board.