How to Use STM32 PWM with Simulink Coder Support Package for Nucleo Boards - MATLAB & Simulink
Video Player is loading.
Current Time 0:00
Duration 3:20
Loaded: 0%
Stream Type LIVE
Remaining Time 3:20
 
1x
  • Chapters
  • descriptions off, selected
  • en (Main), selected
    Video length is 3:20

    How to Use STM32 PWM with Simulink Coder Support Package for Nucleo Boards

    From the series: Getting Started with STM32 Nucleo Boards Using Simulink

    This video is a continuation of How to Use STM32 ADC with Simulink Coder Support Package for Nucleo Boards

    Follow a step-by-step guide on how to design a model in Simulink® using the PWM output ports on an STM32 Nucleo hardware board. It includes examples of how to adjust LED brightness using a potentiometer and how to adjust the speed of a DC motor connected to an L298N motor driver.

    Published: 17 Jul 2022

    Hello, everyone. This is Jayakarthigeyan Prabakar here from MathWorks. In the previous part of this video, we saw how to work with the analog inputs on STM32 Nucleo board using Simulink. If you haven't seen it yet, I recommend you to watch it. The link of the previous video is mentioned in the description of this video.

    In this video, we will continue working with the model we created earlier to understand how to use a PWM signal to adjust the brightness of an LED connected to the PWM port Followed by connecting the analog input block to the PWM output block to adjust the brightness of an LED using the potentiometer. Also at the end, we will use the same model to vary the speed of a DC motor using a L298N motor driver using the PWM signal.

    To get started, open the model. Let's save a copy of this model with a different name. I'm going to call it PWM control. As the first step, let's add the PWM block from the Simulink Coder Support Package for STMicroelectronics Nucleo boards. Open the Block Parameters window and open the Help page. As you can see, the input value should range from 0 to 100 which will correspond to the duty cycle of the PWM signal from 0% to 100% on the specified PWM pin.

    We need to specify the PWM pin initial frequency of the PWM signal and the initial duty cycle as the block parameters. Click on the View Pin Map to see the list of PWM pins. You can see that onboard LED is tagged as LED1 is also a PWM pin. Let us use this value as the pin configuration for this block. Once you have configured the PWM block, let's bring in a constant block to give us input to the PWM block and connect them.

    Log the signal to be viewed in the data inspector and run the model on the hardware in the connected I/O mode. As we change the constant parameter value you can notice the value changing on the data inspector and the brightness of the LED on the hardware correspondingly. Now instead of using the constant block, we can connect the analog input block with the PWM output block to control the PWM signal using the potentiometer connected to it. But as we know, the output signal of the analog input block gives the output value in the range of 0 to 1, whereas the PWM output block takes input values in the range of 0 to 100 for the duty cycle of the signal. So let's add a MATLAB function block to multiply the signal by the value 100. You can also use a multiplier block instead and set the gain as 100. I'll go with the MATLAB function block.

    Now when you run the model you can see that the brightness of the LED varies as you rotate the potentiometer. You can take this project a step ahead by doing a DC motor control system using a motor driver that can control the speed of the motor with PWM input. I have such a setup here with me using the L298N driver connected to a brush DC motor where I have connected the LED pin of the Nucleo 401RE board to the motor driver input pin. And we can control the speed of the DC motor using the potentiometer just like we changed the brightness of the LED. This is just an example and you can use any motor driver that enables you to control the speed of the motor using a PWM signal and you will be able to deploy this project on any Nucleo board that is supported by Simulink Coder Support Package for STMicroelectronics Nucleo boards. With that, we come to the end of this video. Thank you for watching.

    View more related videos