Wednesday, 21 June 2017

Getting started with STM32

After writing many tutorials about LPC2148, I decided to write few for STM32 microcontroller. I bought a Nucleo STM32f401RE board. It cost me 1500 INR (around 23 $). This is cheap and yet very powerful.
This is a 32 bit cortex-M4 device with


  • 8 MHz oscillator, can be clocked up to 84 MHz
  • 512 KB of Flash Memory
  • 96 KB of SRAM 
  • inbuilt RTC, temperature sensor and many more
  • mini usb can be used to program and debug. so additional debugger is not required.
You can check datasheet for more info.

Although STM32 devices are great but the only problem with them is when it comes to programming. Register based programming is not easy, because clock setting is very messed up,  and there aren't any reliable libraries. ST microelectronics provides HAL libraries but that is also not so good. Nevertheless I am going to use HAL libraries as they are officially supported by ST and also it have a good community support.

So before starting the programming part, you need to install few things. I am using KEIL uVISION 5 for coding, STM32cubemx to generate code and STM studio is for observing variables during debugging.

Download STM32CubeMxstm-studio and  Keil

1.) First you need to set up keil. 
Click on software pack

  • Next select the board, type in your board name and install all the packs.
2.) Install the cubeMx. 

3.) Install the STM studio.

Create your project-

1.) open cubemx
2.) Select new project 
3.) select your board by double clicking it


4.) clear the pinout

5.) Select the led pin and mark it as output also select the RCC as crystal oscillator

6.) Select the clock configuration to 84 MHz as shown below

7.) click generate project and follow along

8.) Now click ok and open project. It will open the keil.

9.) Open main.c file in keil

10.) Write the program. You can check the available functions in function tab
        HAL_Delay is in milliseconds.

Make sure you write the code after "user code begin". This is because if you want to regenerate same code with cubemx with some editing, it will not affect your program.

11.) That's it. Now click compile. It will take little extra time in the first attempt.

12.) Go to 'options for target', in the 'target' menu select 'Xtal freq' as 8MHz, and go to debug and select the st-link debugger
13. Now Click LOAD button and it will load the program to your board. Make sure you have installed all necessary drivers before doing this or else it will give an error. You can install the drivers from device manager.

14.) Click reset on board, and you can see the led blinking every 1 second.




This is it for blinking LED. I'll do more tutorials on this. i have created a youtube channel to demonstrate the detailed working of microcontrollers. You can check it HERE. Subscribe to the blog and channel for more tutorials.

To download full code of LED blinking, go HERE



Servo motor with STM32

I have already covered a tutorial about Pulse Width Modulation in STM32  HERE  and in this tutorial i am going to cover one of its applicat...