Blinky and Hello World! for STM32 “Black Pill” tutorial

In this article I will guide you through to make LED on Black Pill blink and also to send “Hello World!” through USB to terminal on your computer.

First we need the schematic diagram to know what goes where, this is the only site where I found it, this is link directly to PDF with schematic.

We can see that LED is connected to pin PB12:

We also know that the board has microcontroller’s USB connected to Micro USB connector:

 

Now we can start CubeMX and configure everything. Select New Project and fom list of microcontrollers select STM32F103C8:

Now we need to configure peripherals.

First, the crystal oscillator:

Second, LED port and name:

And the USB, enable and select class:

We also need to configure clock, we enter 72MHz as HCLK and USB speed should automaticly be set to 48MHz:We also need to enable SWD for programming, otherwise, we will only be able to program our microcontroler once. Don’t ask me how I know:

Now we go to Menu -> Project -> Settings… enter our program name and select SW4STM32 as Toolchain/IDE and click Ok:

All we have to do here is to generate our code, Menu -> Project -> Generate Code. After it is done, click on Open Project:

This will import our code into SW4STM32:

All we need to do now is to crete loop for blinking LED. Just add code below into main.c:

Now compile the code and program it into your microcontroller. After it is programmed, connect target board to computer. LED should blink with rate of 1s and if you check the Device Manager you should see new COM port:

Now we know that USB is properly configured, let’s send something over serial communication. We need some kind of serial terminal on PC. I use this one from Bray, you may have some other.

Let’s go to SW4STM32 to figure out the code. We need some function for sending data through serial communication and the one I will use is CDC_Transmit_FS. We only need to add one line of code for it to work:

Now if we start our terminal it will show that our program sends out “Hello World!” every second:

You might have to unplug and plug back USB connector for serial communication to work.

 

This is all for today. I hope it helped you.

’till next time…

 

Slemi

2 thoughts on “Blinky and Hello World! for STM32 “Black Pill” tutorial

  1. Thanks a lot.

    I have not been able to get led to blink.
    Before i read your post here.
    (I’m new…very new, to this world)
    I could not get the cube to give med USB. I’ll have to work on that.
    I hope you post more in the future.

    I enjoy reading all your work examples, even i only got blinky leds to blink….yet 😀

    Best regards
    Torben

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.