Getting Started With The STM32

So this post is part tutorial, and part reminder to myself in the future when I come back to trying to do the same thing again. It describes the process of programming a SM32 L1 discovery board, specifically (for me) an STM32 L100C discovery board, but it should work with most STM32 devices with some changes. This was a free board that a friend got in a goody bag from an ARM conference. Essentially its how to create a project, how to run it in debug mode, and make a couple of LEDs flash. The basic process it the same for most STM32 boards/chips, using the STLink/V2 which most discovery boards have.

The STM32L100C-discovery board used in this tutorial

To start make sure you have the following downloaded and installed:


Go ahead and open STM32CubeMX, and wait for it to load. Once you get to the home screen, go to File->New Project, and a new window will pop up. Now is the time to find the MCU that you are using. In the MCU/MPU Selector tab, use the series/search options to find the MCU that you are using. As I am using the STM32L100C-discovery board, I have to find and select the STM32L100RCT. When you have selected the right part, click on the start project button in the top right.

Page searching for the STM32 MCU you are using

When the project then loads up it should look like this in the pinout view.

The pinout screen when using STM32CubeMX

At this point it is a good idea to save it under File->Save Project. Save it somewhere you will remember. I have a folder specifically for STM32 projects like this.

At this point it is a good idea to get the schematic up of the board you have. If you have chosen the part view the board choice menu instead of the MCU menu, then it should fill in all the pins for you, but this tutorial should show how to assign the pins manually. For this board it has two LEDs and a button that I need to assign to pins. Remember your board may be slightly different. Schematics are often available somewhere for these discovery boards by ST, they are just difficult to find sometimes.

  • USER button: PA0
  • Green LED (LED 3): PA9
  • Blue LED (LED 4): PA8

Firstly, on the left, under System Core, select the SYS configuration, and a widow should pop out. Make sure that the drop down next to debug is set to Serial Wire. Then you can close this window using the little arrows. some of the pins in the chip diagram should have changed to green now which means they have been set.

Next we want to assign what each pin needs to do. This is done by left clicking on the pin in question and choosing its mode. So for me I need to click on PA0 and set it as GPIO_Input (as its a button) and setting PA8 and PA9 as GPIO_Output (as they are LEDs). When they are selected, they should go green.

One thing that may make life easier is naming the pins differently. If you right click on an assigned pin you can “assign user label” which will allow you to assign a label. I have assigned names like LED 3 and User button to make life easier.

An example of what the pin out should look like when you have set the pins

You probably now see how this is very useful for very complex projects where almost all the pins are going to be taken up by being connected to different inputs and outputs. You can also do things like set different clock speeds, set up ADC’s and other peripherals such as USB’s.

Now in the Project manager section, I have made sure the toolchain/IDE is set to STM32CubeIDE, and make sure the rest looks like the locations you have set and the names as well.

Now click the “Generate Code” button at the top.


It will generate the code and open the STM32CubeIDE. I use the recommended workspace when it prompts for it. Then the window should open. On the left hand side, go to your project name->Src->main.c and double click on main.c.

If you have a look in this main.c it will look like a lot to start with, but we just need to find the while(1) loop, for me it is line 98. The rest of the file is doing useful things for us, like setting up the pins we designated before in MX, and also setting up all the required HAL libraries.

For this next part we are going to use the HAL libraries to toggle the GPIO pins for the LED’s, and lighting them up. in the while loop between the comments USER CODE BEGIN WHILE and USER CODE END WHILE I have put:


which will toggle each pin on and off every 100ms.

What my code looks like

To upload this to the board, first we build it by going to Project->Build project, and wait for the build, hopefully there should be no errors. Then, making sure the board you are uploading to is connected, go to Run->Debug. It may ask you to save the file, and go ahead and do so. Also it may ask you to switch to a new view, go ahead. Go to Run->Resume, and the LED’s on board should then flash fast!

When you wish to stop, go to Run->Stop and you should return to main.c to do some more editing.

Well done on getting this far, and if you didn’t through some parts then remember Google is your friend, but also the comments section below is open to ask questions. Equally get in touch on Twitter and share this if you found it useful. I may even be able to do more like this, for instance more on the HAL libraries.