PWM dimming of LEDs with MCU [STM32 Nucleo]

How can we use the digital output of MCU to dim LEDs? I will explain the magic trick of using the digital output of MCU to control the brightness continuously in an analog manner, as if dimming were a magic trick.

As an application of the timer, let's try dimming LED lights using the PMW output. Normally, LEDs can be turned on and off simply by switching MCU output ON/OFF, which is typical of digital outputs.

How can we dim LEDs using the digital output of MCU? Using the digital output of MCU to continuously control the brightness of a dimmer in an analog manner is like a magic trick, but it can be done easily with MCU. Now, let's reveal the secret.

Normally, LEDs light up when MCU output is turned on, connecting the circuit. Pulse output is used to light LEDs in order to adjust the brightness of LED products to save power, or to adjust the light intensity of LEDs built into sensors, for example.

Even when the output to the LED is pulsed, the LED appears to light up almost continuously at about 100 Hz. The advantage of this pulse output is that the brightness intensity can be adjusted by changing the ON/OFF period ratio (duty ratio) of the pulse, and power can be saved by adjusting the OFF time.

The PWM output of the timer function has been explained so far, and the dimming of LEDs is an application of this PWM. In this application example, the brightness of the LED is adjusted by adjusting the ON/OFF period of the 100 Hz output pulse.

LED dimming circuit

In this application example, the RTOS is used to perform the tasks of turning the LED on and off every 1500ms and setting the PWM duty ratio every 10ms.


For example, if an analog VR is added to the input and the duty value is changed in proportion to the analog value obtained, the LED can be dimmed by the Analog VR. If the duty value corresponds to a numerical value given externally via serial communication, it is possible to dim the light by remote control. With a little ingenuity and a combination of basics, you can develop this into various applications.


PWM of the STM32 timer is explained in PWM mode output of Timer/Counter[STM32 Advanced Functional and General-Purpose Timers Details].

Blinking every 1.5s

Lights up only when user SW is pressed.

Follow me!