STM32 Guide: PWM


This is tutorial is not intended to be a guide for learning C language or about the STM32 platform. It’s primary target is to provide developers a concise guide about integrating peripheral modules and features into active applications.

If you are a beginner, I would recommend you look into an STM32 Project Setup guide like this one.

Pulse Width Modulation (PWM)

Pulse Width Modulation is a technique that is implemented by varying the duration for which a signal is On or Off.

The duration of the pulse width may correspond to the amplitude, frequency or phase of an analog signal. This method reduces the average power consumed in the transmission when compared to an analog signal with the same data.

PWM is also used for controlling Motors and LED’s, and this allows a much better control of these devices.

STM32 TIMER and HAL Library

PWM as explained is an implementation of a timer. The variation of pulse width is achieved through the implementation of timer in STM32.


Hardware Abstraction Library(HAL) is a part of developer libraries provided by STMicroelectronics for ease of development. The abstraction library is a set of functions and definitions which make it possible for developers to focus on getting their codes running instead of being bogged down with setting all the plethora of registers and bits to get a simple I/O operation possible. Using the functions in the HAL library, developers can simply call a single function to perform operations like read I/O data, or even perform complex processes like SPI or I2C.


Most STM32 microcontroller have a 16-bit auto reload counter and a 16-bit prescaler. The prescaler is used to modify the incoming signal/clock frequency from the external oscillator and convert it to the frequency that the user requires.

Thus, a user can obtain a processing frequency of 64-MHz even if the crystal on the circuit is 8-MHz. Consequently, prescaler can also step down the frequency from the source clock.

They also act as counters in the microcontrollers and can be used to keep track of operations. A 16-bit timer can count up to 255. Once this is reached, it rolls-over and starts from 0 again.

PWM on STM32F4

Since I have an 32L071 Discovery devkit, I will orient this tutorial for this dev-kit. However, since we are making use of the HAL library, it can be expected that the steps highlighted below will work for any STM32 board or processor.

We shall make use of the STM32Cube IDE for assigning functions and pins to our microcontroller.

As is seen on the tab on the left side, we have options to initialize timers. We can choose any one of these and the corresponding instance will be high-lighted in the pinout view.

Once this is done, we can proceed to Project Manager tab and generate code.

With the code now generated, we can directly upload and test the code. The pin PA6 will have the PWM output, while the configurations for the PWM were done in the pinout section.

Additionally, if you need to update the settings for the PWM, all you need to do is open the .ioc file and update the settings.


In this tutorial, we discussed the methods for using Timers in our hardware setup. The same code will work of any Timer Configuration or Operation.