Skip to main content
Associate II
October 13, 2026
Question

Maintaining a Stable PWM Frequency When Changing LED Brightness on STM32

When using an STM32 timer to control LED brightness, is it generally better to keep the timer period fixed and adjust the compare value, rather than changing the timer period as well?

My understanding is that changing the compare value allows the duty cycle to vary while keeping the PWM frequency constant. Are there any practical considerations when selecting the timer prescaler and auto-reload value, particularly when aiming for smooth dimming across the full brightness range?

I’d appreciate any guidance on the timer configuration and potential trade-offs.

4 replies

Andrew Neil
Super User
October 13, 2026

I guess this is a follow-on to your previous thread:

If that question is now answered, please mark the solution on the appropriate post in that thread.

 

is it generally better to keep the timer period fixed and adjust the compare value, rather than changing the timer period as well?

Again, that’s a general question - nothing specific to STM32.

“PWM” stands for Pulse Width Modulation - meaning that it’s the width of the pulses that changes.

The frequency is generally not changed.

What benefit would there be to changing the frequency?

As noted, the frequency is chosen for human perception - I don’t think that varies with brightness?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Peter BENSCH
ST Technical Moderator
October 13, 2026

​@cedrick

Basics on PWM can be found, for example, in the relevant Wikipedia article.

In-depth details on timers can be found, for example, in AN4013 and AN4776.

Regards
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
cedrickAuthor
Associate II
October 15, 2026

Thanks, Peter. I’ll go through AN4013 and AN4776 to better understand the timer configuration and its practical trade-offs.

Andrew Neil
Super User
October 15, 2026

And don’t forget to look at the examples provided by ST - there’s loads of them:

 

and videos: https://www.youtube.com/@stmicroelectronics/search?query=pwm

Not to mention 3rd-party stuff!

 

When your question is answered, please mark the solution on the post which provided the answer - not this one!

You still haven’t marked a solution in your previous thread - don’t just abandon it!

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.