Control LED Brightness with PWM

Hi tecsploiters!

This post is all about using Pulse Width Modulation on the STM32F4 Discovery board to control the brightness of some LED’s- It sounds complicated, but its actually very simple!

PWM Dimmer

PWM Dimmer using STM32F4 Discovery


Now if you dont know what pulse width modulation is (PWM) I’d suggest you look it up, Wikipedia gives a good introduction – but to put it simply PWM is used to control the power transmitted to a source, such as an LED or motor by simply turning the power on and off very quickly! The more the switch is on in a set time period, the higher the power and visa versa.

If you want to really get in to it there is some maths involved, and you would need to start considering other factors such as the speed (frequency) at which you do the switching – but we will leave that until another post! The rest of this post will show you how to program the STM32F4 Discovery board to ‘dim’ both the on board LED’s and an external LED.

As always the full code listing is available at the bottom of the article, and I have commented it throughout to try to explain whats going on.

Now if you are new to electronics you would definitely be forgiven for finding the documentation with the STM32F4 Discovery board a little intimidating – so I will do my best to explain a few points, as you will need to have a basic understanding of the documentation in order to link the configuration code below to the actual external pin used in the example.

Start by taking a look at the official documentation. The information of interest to us starts at page 21 and is a table that describes what each GPIO pin can be used for and goes on down to page 33. For now we are only interested in the first two columns ‘Main Function’ – which is essentially the Pin number on the board and ‘alternate functions’ that describes what else the pin can do other than stanard GPIO.

We are interested mostly in Pins that can be used as a timer, as you will see in the code later a timer is used when setting up PWM. I started by looking for TIMX_CHX in the ‘alternate functions’ column – where the ‘X’ is replaced by a number. If you scroll to page 26 you will find pin PB6 with the alternate function TIM4_CH1. This means the pin is can be used with timer number 4 on channel 1. (each timer has 4 channels)


Don’t worry if the concept of a timer or channel doesn’t make sense at the moment – all you really need to know is that it is used as part of the PWM setup, and that you can only output the PWM signal on certain timer compatible pins. The only way to know what pin to use is to find it in the documentation – and then ensure that your initialization code initializes the correct pin for the timer / channel you are using. In this example we are using timer 4, and we are using all 4 of the channels. 1 of the channels (channel 1) is being used to output a PWM signal on Pin PB6, the other 3 are being linked to the on board LEDs.

The full code listing is below, this can be pasted into the main.c file (of the hello world project) – If anyone would like me to upload the project in full please leave a comment.

Once you’ve got it running you should see 3 of the 4 on board LEDs cycle from bright to dim (at slightly different levels). If you get a volt meter an measure the voltage on pin PB6 (by putting the red lead on PB6 and the black lead on the GND pin) you should see the voltage cycling from low to high in a continuous loop!!!

All that remains then is to connect the pin to an external LED circuit as done in my previous post and you will see the LED going from bright to dim!!!

As always any questions please leave a comment or use the new contact us form, I’de be glad to help you out if I can!

See the video of the finished project on youtube

Have Fun!!


2 thoughts on “Control LED Brightness with PWM

    1. Hi thanks! Hope it was usefull, if u go to the downloads section you can get the complete project including the header file, you just need to register your email, have fun!

What are your thoughts?