This article explains how to program the STM32 Black Pill board with the Arduino IDE.
Required Parts
You will need a STM32 Black Pill development board and a USB-C cable for programming the board. A breadboard and some cables will be handy as well, for trying things out.
STM32 Black Pill
USB C Cable
Dupont Wire Set
Breadboard
Makerguides is a participant in affiliate advertising programs designed to provide a means for sites to earn advertising fees by linking to Amazon, AliExpress, Elecrow, and other sites. As an Affiliate we may earn from qualifying purchases.
Download and Install STM32CubeProgrammer
The Arduino IDE does not have the functionality to program STM32 microprocessors. You need extra software for that. Specifically, we need the STM32_Programmer_CLI command line tool that comes as part of the STM32CubeProgrammer software.
To install it, go to STM32CubeProgrammer website and click on the red button saying “Get Software”:
The website will scroll to list with STM32CubeProgrammer software installations available for different operating systems:
Click on the red button “Get latest” for your operating system or “Select version” to select a specific version of the STM32CubeProgrammer software you want to install. In both cases a dialog box appears where you have to accept the Licence agreement:
After that, a second dialog box appears, where you can log into an existing MySt account, create a new MyST account or “Download as guest”:
You can try clicking on the small “Download as guest” link at the right bottom of the dialog. A new form opens, where you can enter your details and most importantly an email address, where you will receive a download link.
However, that didn’t work for me. I received the download link but got the message that the link is not valid anymore.
Instead, I created an MyST account and then was able to download the software as zip file, e.g. en.stm32cubeprg-win64-v2-19-0.zip
. Unzip this file and under Window you will find a file named SetupSTM32CubeProgrammer_win64.exe
in it. Run this .exe file to install the software:
If the installation was successful, we can then install the STM32 boards.
Install STM32 boards
To install the STM32 core go to File -> Preferences and open the Preferences Dialog
In the Preferences Dialog go to the Settings Tab. At the bottom you will see “Additional boards manager URLs:”. Click on the button to the right (marked yellow) to open the URL editor
In the URL editor add the following URL to the list:
https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
The picture below shows the edit field of the Additional Boards Manager with the added URL for the STM32 boards:
This will let the Arduino IDE know from where to download the board files for STM32 devices. If this editor already has some URLs in it that is fine. Just add the new URL to the existing ones in a new line.
Next open the BOARDS MANAGER, type “STMicroelectronics” in the search bar and then install the “STM32 MCU based boards” as shown below:
This will initiate the download and installation of the STM32 core files, which will take a bit:
I encountered an error when I tried it the first time but after pressing “INSTALL” a second time, it worked. Now we are ready to select a STM32 board and program it.
Selecting the Black Pill board
Open the BOARDS MANAGER and type “f4” in the search bar. You should see a board named “Generic STM32 F4 Series”. That is the board you want, if you have the Black Pill board listed under Required Parts. The picture below shows the board selection dialog:
If you have the board connected to your PC via a USB cable, you should be also able to select the Serial Port, e.g. COM4 as in the example above. If you have problems with the USB connections see the Issue section.
Setting Board Parameters
Before we can program the board we need to set the correct parameters. Go to “Tool” Menu:
At the end of this menu you will find a list of parameters, which you need to set to the following values:
Now we are finally ready to program the board.
Program the Black Pill
If you have successfully connected the board via USB, you then need to put the board into programming mode by first pressing and holding the BOOT0
button and then pressing and releasing the NRST
button. The picture below shows you where on the Black Pill board these buttons are located:
Note that you have to do this every time you want to download a new program onto the Black Pill. You can start by trying the usual Blink program:
void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(1000); digitalWrite(LED_BUILTIN, LOW); delay(1000); }
If successful, the blue LED on the board should blink every second. If not, have a look at the next section.
Issues
Flashing the Black Pill board can be tricky. There are a few common issues that we will discuss in this section.
USB device not recognized
The fist common issue when using Windows is the “USB device not recognized” error. You connect the Black Pill board to your computer via USB but when you press the BOOT0
and the NRST
button to flash it, you see the following error message:
Apparently for older boards this might be due to a thermal issue and if you warm up the microcontroller a bit (by pressing your thumb on it), this error may disappear. I cannot confirm that this works, since this did not work for me.
Otherwise the recommendation ist to connect A10 to ground (G) and this worked indeed for me as well. The picture below shows, which connection you have to make:
If you don’t want to loose A10 as GPIO pin for other purposes, you can use a 10 KΩ pull-down resistor instead and the A10 pin remains usable and flashing still works.
Target Device not found
If you see the error message “Target Device not found” in the Serial Monitor, it is because you forgot to press the BOOT0
and NRST
buttons.
Press and hold “BOOT” then press and release “NRST”. This puts the Black Pill into flashing mode. You need to do this every time you want to download a program to the Black Pill.
STM32CubeProgrammer not found
If you see the error message “STM32CubeProgrammer not found” in your Serial Monitor, when trying to download a program to the Black Pill, then the reason is typically that the STM32_Programmer_CLI.exe program is not on the system path.
Under Windows go to Settings, search for “env”, and click on “Edit the system environment variable” as shown below:
This opens a dialog box, where you can find your environment variables. You need to add the path to the STM32_Programmer_CLI.exe program to the Path
variable. Select it and click on Edit..
:
Then press “New” to add your path to the STM32_Programmer_CLI.exe program to the list. In my case this is “C:\Maet\Software\STM32CubeProgrammer\Bin”, as shown below. But yours will be different!
You may have to restart your computer to make this update of the system path active. But after that the error should not occur anymore.
Conclusions
In this article you learned how to program the STM32 Black Pill board with the Arduino IDE.
If you want to know how to install boards for other common microcontrollers in the Arduino IDE have a look at the Programming the Pico W5 with Arduino IDE, the How to Set Up a Raspberry Pi and the A Beginner’s Guide To ESP32 Programming tutorials.
If you have any questions feel free to leave them in the comment section.
Happy Tinkering ; )
Stefan is a professional software developer and researcher. He has worked in robotics, bioinformatics, image/audio processing and education at Siemens, IBM and Google. He specializes in AI and machine learning and has a keen interest in DIY projects involving Arduino and 3D printing.