In this article, we are learning about PWM Generation with STM32. Here we are using STM32F103C8Tx MCU, but you can also follow the same way for most of the other MCUs. So let’s start!

Disclaimer:

Advertisements

What is a PWM signal:

PWM or Pulse Width Modulation is a kind of digital signal where the on-time and off-time can be varied when required.

the PWM signal can be explained by the following image:

so, the duty time = on-time/(on-time + off-time).

and more on time, more the output voltage(avg), or more brightness in a simple way like this animation.

Application of PWM signal:

When you need to control a load or a voltage with a microcontroller, in most cases, you can use a PWM signal as the controlling signal. For example, you need to drive a DC motor at different speeds with a microcontroller. Then you can use a PWM signal which you can generate with your microcontroller and use this PWM signal to drive a MOSFET which is driving the Motor.

DC motor driver with PWM

Now, let’s generate our PWM signal with STM32 MCU.

PWM Generation with STM32:

After opening a new project or from your existing project, go to timers from the Timers settings.

PWM Generation with STM32

Then check which timer is having PWM and also the pin you can use for your MCU. In my case, I use TM2 as an example. Then select the channel you need to set for PWM.

Then from ‘Parameters Settings’, set Prescaler and Counter period. Prescaler sets the frequency parameters which are related to the MCU frequency you selected on the clock setting page. And the counter period is like the % settings. In my case, I kept both of Prescaler and the counter period to 255. Which means, 0 = 0% and 255 = 100% of the duty cycle.

Now, save the project to generate the code.

Code:

In main.c file, simply add the following lines:

PWM Generation with STM32

This will start the PWM signal and in this case, the handler is Timer2(htim2) then simply call the compare to set the PWM duty cycle where you need.

PWM Generation with STM32

Now, build the project and check the output.

Output:

PWM Generation with STM32

You can check varying the value of x in “__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1, x);” line.

End:

I hope, this article will help you learn STM32. See you soon with something helpful. Thanks.

You can check my other articles too:

For Professional Designs or Help:

Loading

MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *