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: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act of copying and claiming it as yours. Most of the articles published here are kept open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guidance feel free to comment below, the author will try to help you. Also, there can be affiliation links in the article. Which will not affect you anyway, but allows the author with some commission. So please don’t take it otherwise. Thanks.

Table of Contents


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.


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.


PWM Generation with STM32

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


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:


Also let me know what you want to get as the next article, comment below!


Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.


Leave a Reply

Avatar placeholder

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

%d bloggers like this: