Skip to main content
Associate II
October 8, 2026
Question

Using PWM to control LED brightness on an STM32

I'm looking into the best way to control the brightness of an LED load using PWM from an STM32 and would appreciate some advice on the practical side of the implementation.

My understanding is that the average brightness can be controlled by changing the PWM duty cycle while keeping the LED supply voltage constant. For example, increasing the duty cycle should increase the average current and therefore the perceived brightness.

What I'm less certain about is choosing an appropriate PWM frequency. A very low frequency could potentially result in visible flicker, while a much higher frequency increases the number of switching events.

For a longer LED load, I'm also wondering whether it is better to keep the STM32 PWM output isolated from the actual LED current path using a dedicated MOSFET or LED driver.

If anyone has implemented PWM-controlled LED lighting with an STM32, what PWM frequency and timer configuration have you found to be a good starting point?

I'm particularly interested in the STM32 timer/PWM configuration and the interface between the MCU and the LED driver.

15 replies

jiangfan
ST Employee
October 9, 2026

comments for your reference:

  • PWM frequency around 1kHz may be good
  • to drive multi-LED in series, it may be good to use dedicated MOSFET or LED driver to keep the STM32 PWM output isolated from the actual LED current path
cedrickAuthor
Associate II
October 13, 2026

Thanks, Jiangfan. I appreciate the advice on PWM frequency and using a dedicated driver for multiple LEDs. That gives me a clearer idea of the approach to consider.

Ozone
Principal
October 12, 2026

There are no really STM32-specifics in your question, so you could consult any MCU tutorial or application note in this regard.

> What I'm less certain about is choosing an appropriate PWM frequency. A very low frequency could potentially result in visible flicker, while a much higher frequency increases the number of switching events.

Don’t go below recent TV and video monitor refresh rates, and you will be fine.
Critical factor here is the human eye.

For improved quality, consider superposition effects of neighboring channels.
Even if you cannot measure anything in the EMI spectrum, the human eye often perceives “down-mixed” components below 50Hz.

cedrickAuthor
Associate II
October 13, 2026

Thanks, Ozone. I appreciate the explanation, especially the point about the eye perceiving lower-frequency effects from neighbouring channels. I’ll keep that in mind when considering PWM frequency and perceived flicker.

Ozone
Principal
October 13, 2026

On a related note …

I very well remember when the signal lighting of traffic lights were changed in my area almost two decades ago, from incadescent bulbs to LED lights.
The PWM frequency of these light was about 50Hz, which was OK when you had them in focus.
But as soon as they moved into the peripheral visual field, I noticed a horrible flicker.

This is a common feature of mammal eyes, by the way.

Andrew Neil
Super User
October 12, 2026

 I'm also wondering whether it is better to keep the STM32 PWM output isolated from the actual LED current path using a dedicated MOSFET or LED driver.

This is just standard electronic design - nothing special or specific to the STM32.

You look at the datasheet for whatever device you’re using to drive the LED (eg, the STM32); this will tell you its current & voltage capabilities. If those are sufficient for your particular LED, then you can connect direct; if not, then you’ll need some sort of external driver.

 

How To Interface Anything To Anything Else.

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.
cedrickAuthor
Associate II
October 13, 2026

Thanks, Andrew. That makes sense. Checking the datasheet against the LED’s voltage and current requirements is a good starting point. I appreciate the clarification.

unsigned_char_array
Lead III
October 12, 2026

Higher is always better. I would not go below 100Hz. And at least 200Hz. But 1KHz or higher is better
Low frequency means you perceive flicker, and POV effects when moving your head.
Alternatively you can use PDM. This spreads out the pulses more and creates a higher perceived frequency for a clock frequency.

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."
cedrickAuthor
Associate II
October 13, 2026

Thanks for the input. The point about visible flicker and POV effects is helpful, and I’ll keep the higher PWM frequency option in mind.

Peter BENSCH
ST Technical Moderator
October 12, 2026

It is not only the length of the path of the LEDs connected in series that matters, but also the LED current flowing. In exceptional cases, several GPIOs can also be connected in parallel, so that larger currents can be switched as well, provided that the maximum total GND/VCC current is not exceeded. With more than 1 LED per LED string and about 20mA, a switching transistor is absolutely necessary. 

And one more thing: if these are power LEDs, whose current is supplied by a constant-current source with a switching regulator, then a very small duty cycle in such series dimming leads to ugly flickering, because the control loop of the constant-current source can no longer operate properly. In this case, the solution is Shunt-FET Dimming, also known as Bypass Dimming, in which the switching transistor does not drive the LED string in series, but instead shorts the string of LEDs: the constant-current source continuously supplies the constant current, only that current is diverted past the LEDs when switched off. As for the resulting power dissipation, apart from the losses in the DC-DC converter, it is determined by the conduction losses, i.e. the effective Rdson of the switching MOSFET times the square of the constant current, and can be strongly influenced by choosing a suitable switching MOSFET.

As already mentioned, however, these are not ST-specific problems and have already been discussed very often in many forums and app notes in general form.

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 13, 2026

Thanks, Peter. I appreciate the detailed explanation, particularly the point about shunt-FET dimming with constant-current drivers. That distinction helps clarify why the appropriate dimming method depends on the LED driver and load.

Andrew Neil
Super User
October 13, 2026

​@cedrick it seems you’ve now started a follow-on thread:

 

If you’re now done with this thread, please mark the solution

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.
Visitor
October 13, 2026

For a starting point, I’d try a PWM frequency around 1– 20 kHz and adjust it based on the LED driver and whether any flicker is noticeable. Using an STM32 timer in PWM mode makes duty-cycle control pretty straightforward. I’d also use a suitable MOSFET or LED driver for the actual load rather than powering a long LED strip directly from an MCU pin. The exact timer settings will depend on your STM32 clock and the driver you’re using.

Andrew Neil
Super User
October 13, 2026

 I’d also use a suitable MOSFET or LED driver for the actual load 

That, of course, depends on the LED - as already noted.

 

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.