Home / Tutorials / STM32 Tutorial / Getting Started with Blue Pill and STM32Cube

Getting Started with Blue Pill and STM32Cube

Arduino is easy-to-use but that's at the expense of numerous features, most glaring of them is speed. Hence you would rarely see an Arduino in an embedded, industrial setting. ARM Cortex-M microcontrollers are low-cost, energy-efficient 32-bit devices with up to 2 DMIPS/MHz. They are the microcontroller of choice if you are looking into image processing, machine learning or digital signal processing.

Introduction to Blue Pill

This article features the Blue Pill, a STM32F103C8-powered breakout board. Some notable features of this microcontroller are:

  • 1.25 DMIPS/MHz (72 MHz frequency)
  • 64 kB of Flash memory
  • 20 kb of RAM
  • 37 I/O Ports
  • 2 x 12-bit ADC with 10 channels each

The Blue Pill is designed to be similar to the Arduino Nano with 32 of the 37 I/O ports readily accessible. It can draw power from the microUSB port or the 3.3 V pins. It also has two on-board LEDs, BOOT jumpers, SWD connector and reset button.

STM32 Blue Pill

You can program the Blue Pill using the Arduino IDE, just like how you can program the ESP8266 and ESP32 breakout boards. That will be for a separate tutorial as we will be using STM32's own development platform, the STM32Cube.

Setting Up STM32Cube

To start, download and install STM32Cube.

Open STM32Cube then select location for your workspace. Ticking the check box will skip this window on the next launch.

Next, select startup project type:

STM32Cube information enter

For first time users, choose "Start new STM32 project". The Device Selection window then appears:

We type in our device, the STM32F103C8 and proceed:

Blue Pill features a STM32F103C8 microcontroller

Next, give the project a name. Here I named it BluePill_Blinky:

A pop-up window opens that asks you to open associated perspective. Click Yes:

You should now be on the Device Configuration Tool window. Here you can configure the function of each of the pin of the STM32F103C8. This is a really neat feature of STM32Cube as it makes setting up your project faster and easier.

Blue Pill microcontroller STM32F103C8

Our example project is an on-board LED flasher. In the Blue Pill you can see one of the LEDs with a label PC13. This means it's wired directly to pin PC13.

STM32 Blue Pill pinout

Click on the PC13 pin on the Device Configuration Tool. A menu appears with all the possible functions of the pin. Since we're just flashing a LED, we make PC13 a GPIO_Output pin.

We can view more options for the PC13 pin by expanding System Core and then choosing GPIO. Here we can configure the initial state of PC13, its mode, pull-up/pull-down resistors and label.

Here, I gave PC13 a label "ONBOARD_LED":

After this, go to File -> Save (or CTRL+S). A message window appears.

If you click Yes, a template code will be generated with all the configurations already there. A really great feature!

Another message window appears, this time asking you to open a perspective:

Answering yes to this message will insert a pane on the right side which shows an outline of the code:

That's it! You will now see your main.c. file on the IDE. On the left is the Project Explorer pane where you can view all the project files. If you want to view the Device Configuration Tool again, just click the one with an .ioc extension.

Building the Blinky Code

We will now insert some lines in the main.c file in order to flash the on-board LED. First, look for a while(1) loop inside the main() function. This is the part that executes infinitely while the microcontroller is on. This loop should have the following lines of code for flashing the on-board LED:

The HAL_GPIO_WritePin() function makes a pin high or low. Its parameters are the I/O Port, the name of the pin and the state of that pin. This line makes the pin high:

While this line makes the pin low:

Note that the configurations for the GPIO are in the function MX_GPIO_Init() which was auto-inserted by the Device Configuration Tool.

You notice that we have a delay() function between the set and resetting of the pin state. This ensures that our eyes can catch the flashing. However, this is not a built-in function. We insert this function right after the last function Error_Handler():

A prototype of this function should also be added along with the other generated prototypes:

After this, the project is ready for building. Go to Project > Build All (or Ctrl+B):

If there's no error, you should see this message at the bottom panel:

Blue Pill code compiled successfully

Next we'll be sending the code to our Blue Pill. Connect the ST-Link programmer to the Blue Pill board:

Blue Pill to ST-Link V2 programmer

Then make sure the BOOT0 jumper is on the right. This makes the Blue Pill enter programming mode.

Blue Pill on BOOT mode

Click the Run button. The launch config window appears. Just click OK.

If the code has been successfully sent to the device, the console message is like below:

Blue Pill code downloaded

If the code download fails, press the reset button on the board and try again.

Bring the BOOT0 back to its original position to go back to normal operation mode. Press the reset button and the on-board LED should now be flashing.

Check Also

STM32 Nucleo

Getting Started with STM32 Nucleo and Mbed

The STM32 Nucleo boards by ST packs a lot more power than an Arduino UNO …

3 comments

  1. Hi,
    I had used the blue pill board in some of my projects and just yesterday I bought one more board but very strangely the controller is not going into boot mode even after setting the jumpers properly. I tried erased the memory using ST link dongle and programmed a test code even this does not work. What could be the problem? Also is it possible for yu to post the readout of option bytes of a working board? Thank you.

    • Hello. I also experienced that sometimes. Try resetting the board just before uploading the code. I'll try to post my option bytes as soon as I can.

  2. Thank you very much for your time and reply. I happened to get one of my old working boards and I read the option bytes using ST-Link. I have a screenshot but not able to attach it with this post (it may help someone looking for these bytes)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.