Video: STM32F4 Discovery Beginners Tutorial – Blink LEDs

Video Transcript of Blink LED tutorial


Hello and welcome to a short video which will cover how to write a C program in Keil Micro vision 5 which will blink the LEDs on the STM32F4 Discovery Board on and off. There is a written version of this tutorial available along with the final source code to download, the link is in the description.

Firstly, before we start anything, we need to know a few things about the LEDs on the discovery board and how they are connected to the microcontroller. The four LEDs are connected to pins on the microcontroller and in order to switch them on, the microcontroller must provide a voltage output from each respective microcontroller pin. If we look at the STM32F4 Discovery user manual, on page 18, we can see that the LEDs are connected to the following pins on the GPIOD port: Green is connected to Pin 12, Orange to Pin 13, Red to Pin 14 and Blue to Pin 15.

Let’s map these on the actual discovery board for reference.

Okay let’s get into the programming. Assuming you have set up your project, if you haven’t you can follow this video in the card above, we will create a new C file and call it main. Right click the source group 1 folder and select add new item. Select a C file and name it main. Press add.

The first line of code that must be included is to tell the compiler where to find some of the functions that will be used throughout this tutorial. We use the hashtag include directive with the header file we want to include in quotation marks. In this case we want to include the stm32f4xx.h file.

Then we need to write our main function. This is written using the int main open and closed brackets followed by open and closed braces. The main program code goes in between the braces of the main function.

We can add comments to our code by using a double forward slash. It is always important to comment your code to tell a reader what your code does. This is especially important if you are watching this for some kind of school assignment as marks will most likely be allocated to code documentation.

Initialising LEDs

We will add a comment to describe our next lines of code which will be initialising the general-purpose input output ports or GPIO ports.

In order for the pins to function correctly, the clock of GPIOD must be enabled. This is done using the following code. RCC arrow operator AHB1ENR bitwise OR function then RCC_AHB1ENR_GPIODEN.

This line of code is essentially a predefined function that is included in the header file that we previously mentioned. These functions allow a more straightforward method to set certain bits in the control registers of the microcontroller.

Next, we need to tell the microcontroller that pins 12 to 15 are outputs. This is done by using some other helpful predefined functions such as: GPIOD arrow operator MODER bitwise OR then GPIO_MODER_MODER[PIN NUMBER] underscore 0. We repeat this for each pin. Now the LEDs are configured for use. We need to create an infinite loop in order to make the LEDs blink indefinitely. If we don’t make a loop like this, the program will just run through once before terminating. We will use a while loop with a 1 in its condition brackets so it always loops.

Turning on LEDs

In order to turn on an LED, its respective pin in the GPIOD bit set reset register must be set to one. The Bit set reset register or BSRR is a 32-bit output register where writing a one to a pin in the lower 16 bits sets that output high, and writing a one to the same pin in the upper register will reset the output to low. Hence bit set and reset register. It is more understandable when written in code.

To turn an LED on, say the green LED on pin 12, we write: GPIOD arrow operator BSRR equals 1 shifted to bit 12.

To turn off the green LED, we simply do the same again but shift a one to the 12th pin in the upper register, which is done by adding 16. You may get a little yellow warning indication but this is not a problem, Keil is just informing you that the + will be evaluated before the shifting which is fine. You can remove this if you put a brackets around the 12 plus 16.

We can repeat this for the other LEDs by changing the respective pin number the we set ones to. Now the problem with this code is that the LEDs will switch on and off much too fast for the LEDs to respond, they will just appear off. To fix this we need to incorporate a delay after turning the LEDs on and again after turning them off. This can be achieved in many different ways but for simplicity we will use a basic for loop.


At the beginning of our code, outside of our infinite loop, we need to define a variable that we will use in our for loop. We will do this by defining a 32bit unsigned integer with the variable name i. Back in our infinite loop and after we have turned on each LED we will make our for loop which will iterate many times, in my case, with the clock speed I have set my discovery board to I will set it to 2 million iterations. You can tweak this if required, I will have another tutorial about configuring the clock speeds at a later date, look out for that if you are interested.

You can simply copy and paste the delay loop after switching off the LEDs to complete the code.

Build the program using the build button or the F7 keyboard shortcut. You should see it build without errors. Now let’s upload it to the discovery board. Press the download button or the F8 shortcut. The code has now been uploaded to the discovery board indicated by the Programming Done output in the build output window.

The LEDs on your STM32F4 discovery board should now be blinking on and off.

Thank you for watching, if this video has helped you, please like the video and consider subscribing.


If this project was helpful to you, please check out the other projects or tutorials here.

Leave a comment

Your email address will not be published. Required fields are marked *