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.
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.
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.
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:
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.
Now, build the project and check the output.
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:
- Reading ADC with DMA in STM32 MCU
- ADC Reading with STM32103C8Tx (BluePill)
- Adafruit_FONA Library with ESP32
- INA219 interfacing with STM32
- Logging Data to Excel from Arduino
- STM32 as USB Device
For Professional Designs or Help:
Also let me know what you want to get as the next article, comment below!