STM32 PWM Frequency & Resolution Configuration

In this tutorial, we’ll discuss The STM32 PWM Frequency & Resolution Configuration, and how to calculate and configure the STM32 PWM to achieve a specific output frequency and/or resolution. You’ll learn why it’s important to have a high PWM resolution and what it means for your target application. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 PWM Frequency
  2. STM32 PWM Resolution
  3. STM32 PWM Frequency & Resolution Control Example
  4. Wrap Up

STM32 PWM Frequency

Pulse Width Modulation (PWM) is a technique for generating a continuous HIGH/LOW alternating digital signal and programmatically controlling its pulse width and frequency. Certain loads like (LEDs, Motors, etc) will respond to the average voltage of the signal which gets higher as the PWM signal’s pulse width is increased. This technique is widely used in embedded systems to control LED brightness, motor speed, and other applications.

The Main 3 Parameters of a PWM Signal are:

  1. PWM Frequency
  2. PWM Duty Cycle
  3. PWM Resolution

In this tutorial, we’ll dig deeper into the PWM frequency and resolution to learn more about controlling each parameter and what is the relationship between the two quantities.

STM32 PWM Frequency

Generally speaking, the PWM signal’s frequency is a measure of how fast the PWM signal keeps alternating between HIGH and LOW. The frequency is measured in Hz, and it’s the inverse of the full period time interval. Here is how it looks graphically and its mathematical formula.

in STM32 microcontrollers, the PWM signal’s frequency FPWM can be controlled (configured) by adjusting one or multiple of the following parameters:

  • Timer’s Clock Source Frequency (FCLK)
  • Timer’s Prescaler (PSC)
  • The Auto-Reload Register (ARR)

The mathematical formula below governs the relationship between the output PWM signal’s frequency (FPWM) and the other three parameters.

STM32 PWM Frequency Formula - STM32 PWM Frequency Equation

For instance, if we have an STM32 microcontroller with an FCLK of 72MHz. And we’d like to generate a 1kHz PWM output signal. We’ll have to calculate the values of ARR & PSC to achieve the desired FPWM output.

There are too many solutions for such a problem, of which the following sets:

  • ARR = 35999, PSC = 1
  • ARR = 23999, PSC = 2
  • ARR = 17999, PSC = 3
  • ….
  • ….
  • ARR = 1799, PSC = 39
  • and so on …

Plug in each set of (ARR & PSC) values into the FPWM general equation shown above, and you’ll always get a 1kHz output frequency!

You can just use any combination of the (ARR & PSC) listed above and you’ll always end up having a 1kHz PWM output signal. However, among all possible solutions, only the first one is considered the best since it gives us the maximum PWM Resolution, which is always a desirable target to achieve as we’ll discuss further in the following section hereafter.


STM32 PWM Resolution

The PWM resolution is expressed in (bits). It’s the number of bits that are used to represent the duty cycle value. It can be 8bits, 10, 12, 16, or anything in between. The PWM resolution can be defined as the number of discrete duty cycle levels between 0% and 100%. The higher the PWM resolution, the higher the number of discrete levels over the entire range of the PWM’s duty cycle.

A PWM resolution of only 3 bits means there are only 8 discrete levels for the duty cycle over the entire range (from 0% up to 100%). On the other hand, a PWM with a resolution of 8 bits will have 256 discrete levels for the duty cycle over the entire range (from 0% up to 100%). You can use the interactive tool below to test this yourself.

Average voltage:

LED

PWM Duty Cycle:

50%

PWM Frequency:

x Hz

PWM Duty Cycle Resolution:

Change the duty slider to control the LED brightness while using the default PWM resolution of 3 bits. Then, change the resolution up to 8 bits or more and try changing the duty cycle again and see how it becomes much smoother with a lot more degrees of control over the duty cycle (Higher Resolution).

STM32 PWM Resolution

In STM32 microcontrollers and all the other MCUs, the PWM resolution is not a parameter that you can independently control. It's a function of both frequencies (FCLK and FPWM). To be precise, here is the mathematical formula for PWM resolution calculation equations:

1. Resolution Calculation Equation

STM32 PWM Resolution Calculation Equation

2. Resolution Control Equation

STM32 PWM Resolution Control Equation

Both equations are essentially the same. However, the first equation is used to evaluate the PWM resolution at a given setting (FCLK and FPWM). The second equation is useful to help you set up the PWM in such a way that gives you a desired resolution regardless of the FPWM of your output signal.

❕ Note

You can't "exactly" control both FPWM and ResolutionPWM at the same time. It's either or kind of situation!

You just can say I want FPWM = x kHz with a resolution n Bits. Or, I want a PWM resolution = n Bits with an FPWM x kHz.

But you can't say I want an FPWM = x kHz and a PWM resolution = n Bits. One value will dictate the other, as we've illustrated earlier with the mathematical relationship.

STM32 PWM Resolution Calculation

To practice the PWM resolution calculation steps, let's just recall the 1kHz PWM output example where the FCLK was 72MHz. The first set of ARR & PSC values that satisfy the FPWM equation was:

  • ARR = 35999, PSC = 1

Therefore, under these configurations, the FPWM is actually 1kHz as desired. And the PWM resolution is given by:

ResolutionPWM = log2(ARR+1) = log2(36000) = 15.1357 [Bits]

In other words, there are 36000 discrete levels of control over the PWM's duty cycle.

Recall my comment about that first set of (ARR & PSC) values being the best among the others. Let's now consider the last set of (ARR & PSC) values that also give us the desired FPWM of 1kHz.

  • ARR = 1799, PSC = 39

Likewise, under those configurations, the FPWM is also 1kHz as desired. And the PWM resolution is given by:

ResolutionPWM = log2(ARR+1) = log2(1800) = 10.8138 [Bits]

As you can see, it's way fewer degrees of control over the PWM's duty cycle. Only 1800 levels compared to the previous configurations which gave us 36000 discrete levels for duty cycle control. That's the meaning of and the motivation behind achieving higher PWM resolution.

STM32 PWM Resolution Control

As I've stated earlier, in order to control the PWM resolution, you'll have to give up on the FPWM parameter. At least if you've got a strict PWM resolution requirement. Like having to have a 12-bit PWM output in order to match the ADC's resolution and directly map the ADC readings to the PWM duty cycle's output. In that case, it's fine as long as you don't care about the PWM frequency being at an exact value (1kHz, 10kHz, etc).

However, a more realistic situation is to have a PWM resolution requirement of at least (n bits) or more but the FPWM has to be an exact value. In that case, you can easily use the PWM resolution control equation below and try plugging in the least acceptable resolution (n bits), and see if the FPWM is overshooting or undershooting, and adjust the PSC or FCLK (if possible).

STM32 PWM Resolution Control Equation

For example, I once had a customer in the lighting units industry who had a strict requirement for the FPWM = 20kHz. While also having to have at least 13 bits of resolution for each color PWM output channel (purely for competitive marketing purposes).

Let's use an STM32F103 @ 72MHz, plug the numbers into the design equation above, and see how it goes!

16 Bits = log2(72M/20k(X+1))

By solving for X (which is the needed prescaler value), we'll get a -0.56 (negative value)

Now, it's impossible to meet the design requirements using that target STM32 microcontroller. Let's move to an STM32H750 @ 200MHz and re-run the calculations once more.

16 Bits = log2(200M/20k(X+1))

By solving for X (the PSC value), we'll get a 0.22 (positive value), which means it's feasible to achieve the desired resolution at the FPWM of 20kHz using that target microcontroller. And here is how to set it up:

By rounding down the PSC value, we'll get (PSC = 0).

Using the FPWM equation, we'll get an ARR value of (ARR = 9999). To double-check the PWM resolution value at these settings, let's do it one last time:

ResolutionPWM = log2(9999+1) = 13.288 [Bits], target achieved!

And there we have it! This combination of PSC & ARR values will help us achieve both requirements of FPWM = 20kHz and PWM resolution of no less than 13 bits as desired.


STM32 PWM Frequency & Resolution Control Example

In this example, we'll configure the STM32 PWM Frequency & Resolution in such a way that meets the following requirements:

  • Use STM32F103 (@72MHz)
  • FPWM = 10kHz
  • PWM Resolution 12 Bits

We can proceed with the calculations in the same way as we did in the previous sections' examples. However, I've made it even easier for you, so you can just use our STM32 PWM Calculator Tool to figure out the configuration values needed to achieve the requirements above.

STM32 PWM Frequency Configuration Example (Calculator)

As you can see in the results above, our STM32F103 target MCU is able to achieve 12.8 bits of resolution while generating a PWM signal with an FPWM = 10kHz. Therefore, you can just plug in the resulting (ARR & PSC) values in the CubeMX code configurator and you're good to go.

Sometimes you might need to precisely control the PWM resolution regardless of the output PWM's frequency (FPWM). For example, if you need exactly 12 Bits of resolution to match an ADC's resolution and directly map the results from ADC->PWM. While having a non-strict requirement for the FPWM 5kHz, for example.

In that case, you can use the other tab in the calculator tool (PWM Resolution Control), as shown below.

STM32 PWM Resolution Configuration Example (Calculator)

To achieve an exact PWM resolution of 12 Bits, the ARR value has to be exactly 4095. At these settings, the calculated PWM output frequency (FPWM = 8.79kHz). Which is 5kHz as required, so it's fine to go ahead and use those (ARR & PSC) values in your CubeMX configurations.


Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★


Wrap Up

In conclusion, we've explored the STM32 PWM Frequency & Resolution calculation and configuration. We've also discussed why it's important to have as high a PWM resolution as possible and how both the FCLK and the FPWM (PWM output frequency) dictate the maximum obtainable PWM resolution.

You can build on the provided examples and use this information in your system. To learn more about other features of the STM32 PWM, you can also check out the rest of the tutorials in this series.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment