MATLAB Examples

Change the LED glow intensity on STMicroelectronics Nucleo Boards

This example demonstrates how to use the Simulink Coder Support Package for STMicroelectronics Nucleo Boards to run a Simulink® model on an STMicroelectronics Nucleo board to brighten up and dims down the LED glow intensity.



Simulink Coder Support Package for STMicroelectronics Nucleo Boards enables you to use PWM Output block to vary the duty cycle of digital output pulse. This PWM output is written to GPIO 13 which is a user LED.

In this example you will learn how to configure a simple Simulink model to change the intensity of user LED for STMicroelectronics Nucleo F401RE board. You can configure this model for other supported Nucleo boards by selecting them as the "Hardware board" on the Hardware Implementation pane of the Model Configuration Parameters.

Available versions of this example:


We recommend completing the Getting Started with STMicroelectronics Nucleo board example, completing the Communicating with STMicroelectronics Nucleo board example

Required Hardware

To run this example you will need the following hardware:

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


The following figure shows the example model:

Task 1 - Connect Pin D3 with D13

1. We will write PWM output to GPIO D3 and and connect D3 to D13(user LED) using a breadboard wire. The picture below explains the same:

Task 2 - Configure model for code generation and download

In this task, you will configure the model for the supported STMicroelectronics Nucleo board

1. Open the PWM-LED 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. The user LED is driven by GPIO Pin 13 on the Nucleo F401RE board. The user LED might be driven by a different Pin on different Nucleo boards.

3. Click on "Deploy To Hardware" to automatically download the generated bin file, stmnucleo_pwm_LED.bin, on to the connected Nucleo board.

4. Observe that the onboard LED connected at Pin 13 brightens up to the fullest and then dims up to the lowest intensity.

5. You can change the value of bias and sample time in "sine wave" block to observe the difference in LED intesntiy and it's frequency.


This example showed how to program your STMicroelectronics Nucleo board to use PWM Output block for changing the duty cycle and frequency of the digital output signal.