Set up STM32 "blue pill" for Arduino IDE

 Author:   Posted on:   Updated on:  2017-11-30T14:44:28Z
The "blue pill" is an STM32F103 based development board. Although it is less popular, the board is cheaper than an Arduino Nano. More than that, STM32F103 is a device with Cortex-M3 ARM CPU that runs at 72 MHz, 20 kB of RAM and 64 or 128 kB of flash memory. The microcontroller (MCU) has USB port, two serial ports, 16 bit PWM pins and 12 bit ADC pins. It runs at 3.3V, but some of its pins are 5V tolerant.

Programming the board can be simplified using the popular Arduino IDE. But before this, an Arduino-like bootloader must be flashed to the board. This can be done via serial port or using the debug interface of the MCU with ST-Link tool. There are also some small hardware issues with Chinese boards that need to be fixed. This post will show a straightforward guide for setting up this board to work with Arduino IDE on Linux and Windows. The instructions below are based on STM32duino wiki documentation.
Set up STM32 "blue pill" for Arduino IDE

Hardware issues

STM32duino wiki contributors have reported that some of the boards came with poor soldering of the micro-USB connector. Also some of them have USB D+ pulled up with 4.7k or 10k resistors which may cause the USB port not to work on some computers. My board had both these issues. I soldered the USB connector. Luckily, my computer recognized the USB device, so I didn't have to pull D+ up with 1.5k according to USB specifications.

Look on the back of the board and locate R10. If it's 10k (marked 103) you can add 1.8k parallel resistor to obtain 1.5k equivalent resistance. If it's 4.7k (marked 472), a 2.2k resistor is suitable for parallel connection. Don't worry, you don't have to do SMD soldering as D+ is connected to PA12.

Another thing that is good to know, is what pin the LED connects to. Depending on this you will choose the correct bootloader.
STM32 Blue pill hardware check
Blue pill hardware check
Go to STM32duino Github repo and look fot the bootloader. Roger Clark offers many bootloaders for different boards. If your board has the green LED connected at PC13, you need the bootloader built for this configuration: generic_boot20_pc13.bin. Download the bootloader for your board and keep reading.

Arduino IDE

The IDE doesn't come with default support for STM32 boards. This step is common to all operating systems, so let's do it now. Launch Arduino IDE, go to File - Preferences, and add the following Boards Manager URL:
Click OK to close the dialog.

Arduino IDE - add Boards Manager URLs
Arduino IDE - add Boards Manager URLs
Next, go to Tools - Boards - Boards Manager. In the dialog that appears, search for STM32 and choose STM32F1xx for "blue pill". If your board uses a different chipset (i.e. STM32F3) select that one. Install it.

Arduino IDE, Boards Manager - install new board
Arduino IDE, Boards Manager - install new board
Before actually uploading sketches to the board, it needs the STM32duino bootloader. I will be using a ST-Link v2 clone to burn the bootloader. The connection to the development board is straightforward, using SWCLK, SWDIO, GND and 3.3V pins. Connect them using the included jumper cables that came with the ST-Link clone. Do not power the development board from other source or USB port! It will get its power from the programmer. Move the jumper to set BOOT0 to 1. You can plug the ST-Link into an USB port. From now on, configuration process is different depending on operating system.


Needed tools can be found at ST. You need STSW-LINK009 (this is the Windows driver) and STSW-LINK004 (the programming utility). You need to make an account to download them. Use the included dpinst tools or the bat script to install the driver. After the driver is installed and the ST-Link adapter is recognized by the computer, install and launch STM32 ST-LINK Utility.

ST-Link Utility
ST-Link Utility
Use the File menu to open the bootloader bin file you downloaded earlier. Next, connect to programmer (use the plug icon on the main toolbar, or Target - Connect) then choose Erase and Program option from the same menu. The bootloader will be burned to MCU Flash memory.

Downloading bootloader
Downloading bootloader
While the bootloader is downloading to MCU, you will see the programmer LED flashing. When it is done, you can disconnect the board from programmer, change back BOOT0 to 0 and connect it using board's USB. Of course, it needs Windows drivers which can be found in the Arduino_STM32 package prepared by Roger Clark. You don't need to download the entire repository just for the drivers. Most of the files contained in this repository were automatically downloaded by Arduino IDE when you added the board support (but not the driver). Here is a driver only archive. Extract it and run install_drivers script. When you reset the board, for a short moment of time while bootloader is running, it appears as DFU device, then switches to serial port.


ST offers no GUI tools to program the board from Linux. But, when you downloaded the board with Arduino IDE, you also downloaded the tools to program the board using ST-Link adapter. In your home folder, open hidden .arduino15 folder (nautilus ~/.arduino15). Browse to packages/stm32duino/tools/stm32tools. Here you will find a folder named like the STM32 package version (i.e. 2017.11.14). Open it, then linux subfolder. There should be an script here. Run it from terminal with root permissions (sudo ./ You just installed udev rules for ST-Link and board. It is now recommended to log off then back in or restart computer.

Open again ~/.arduino15/packages/stm32duino/tools/stm32tools/2017.11.14/linux, go to stlink folder. Make sure BOOT0 is set to 1 and run ./st-info --probe. You should see something like this:
Found 1 stlink programmers
 serial: 573f...
openocd: "\x57\x3f\x..."
  flash: 131072 (pagesize: 1024)
   sram: 20480
 chipid: 0x0410
  descr: F1 Medium-density device
This is an indication that everything is OK and you can proceed burning the bootloader with the following command:
./st-flash --reset write ~/Desktop/generic_boot20_pc13.bin 0x8000000
Adjust the command with the correct path to bootloader file. Here is the output of this command:
2017-11-19T12:32:28 INFO src/common.c: Loading device parameters....
2017-11-19T12:32:28 INFO src/common.c: Device connected is: F1 Medium-density device, id 0x20036410
2017-11-19T12:32:28 INFO src/common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x20000 bytes (128 KiB) in pages of 1024 bytes
2017-11-19T12:32:28 INFO src/common.c: Attempting to write 21140 (0x5294) bytes to stm32 address: 134217728 (0x8000000)
Flash page at addr: 0x08005000 erased
2017-11-19T12:32:29 INFO src/common.c: Finished erasing 21 pages of 1024 (0x400) bytes
2017-11-19T12:32:29 INFO src/common.c: Starting Flash write for VL/F0/F3 core id
2017-11-19T12:32:29 INFO src/common.c: Successfully loaded flash loader in sram
 20/20 pages written
2017-11-19T12:32:31 INFO src/common.c: Starting verification of write complete
2017-11-19T12:32:31 INFO src/common.c: Flash written and verified! jolly good!
That's it. Disconnect the programmer, move BOOT0 back to 0 and connect the board using its USB port.

Here is what happens when you reset (plug in) the board. First the DFU device appears then the USB serial port (/dev/ttyACM0).
[ 1146.407442] usb 7-1: USB disconnect, device number 2
[ 1172.671329] usb 7-1: new full-speed USB device number 3 using uhci_hcd
[ 1173.058344] usb 7-1: New USB device found, idVendor=1eaf, idProduct=0003
[ 1173.058347] usb 7-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 1173.058349] usb 7-1: Product: Maple 003
[ 1173.058351] usb 7-1: Manufacturer: LeafLabs
[ 1173.058352] usb 7-1: SerialNumber: LLM 003
[ 1173.687380] usb 7-1: USB disconnect, device number 3
[ 1174.407316] usb 7-1: new full-speed USB device number 4 using uhci_hcd
[ 1174.587353] usb 7-1: New USB device found, idVendor=1eaf, idProduct=0004
[ 1174.587357] usb 7-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 1174.587359] usb 7-1: Product: Maple
[ 1174.587361] usb 7-1: Manufacturer: LeafLabs
[ 1174.628070] cdc_acm 7-1:1.0: ttyACM0: USB ACM device
[ 1174.629510] usbcore: registered new interface driver cdc_acm
[ 1174.629513] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters
Sketches are uploaded while in DFU mode (device 1eaf:0003).

Uploading a sketch

Launch the IDE and choose the board. For a generic "blue pill", here is the default configuration:

STM32 Generic board in Arduino IDE
STM32 Generic board in Arduino IDE
On Windows, only the port is different (COMx). Open a sample sketch and compile it. From my experience, on Windows you can just press Upload button to reset the board and burn the sketch binary. But, on Linux I had no luck with automatic reset. I make sure the sketch is compiled then I reset the board by pushing the button and I click quickly on Upload button in Arduino IDE.

This is the output in Arduino IDE:
Sketch uses 13028 bytes (9%) of program storage space. Maximum is 131072 bytes.
Global variables use 2824 bytes (13%) of dynamic memory, leaving 17656 bytes for local variables. Maximum is 20480 bytes.
No valid DFU suffix signature
Warning: File has no DFU suffix
dfu-util 0.7

Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2012 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to

Filter on vendor = 0x1eaf product = 0x0003
Opening DFU capable USB device... ID 1eaf:0003
Run-time device DFU version 0110
Found DFU: [1eaf:0003] devnum=0, cfg=1, intf=0, alt=2, name="STM32duino bootloader v1.0  Upload to Flash 0x8002000"
Claiming USB DFU Interface...
Setting Alternate Setting #2 ...
Determining device status: state = dfuIDLE, status = 0
dfuIDLE, continuing
DFU mode device DFU version 0110
Device returned transfer size 1024
Copying data from PC to DFU device
Starting download: [##################################################] finished!
state(8) = dfuMANIFEST-WAIT-RESET, status(0) = No error condition is present
Resetting USB to switch back to runtime mode
Waiting for /dev/ttyACM0 serial...Done
If dfu-util reports no DFU devices, you didn't reset the board at the right time. This means sketch was not uploaded to board.

The Linus issues seems to be related to driver implementation because shortly after a failed upload (without reset), dmesg reports a failure to set dtr/rts on ttyACM0.

Using Arduino IDE with STM32duino bootloader to program the "blue pill" development board isn't the most effective way to develop STM32 based systems, but it's easier than with other development kits. It's recommended to read the API differences if you will use this method.

1 comment :

  1. Great post, it's the most accurate I found around.

    I'm on Arch Linux, USB upload using STMduino always fails (45-maple.rules are active). I had success only using STLink (clone).

    Similar issues on VSCode+PlatformIO: it doesn't work using DFU but it works with upload_protocol = stlink

    I tryed dfu-util --list (v0.7 up to v0.9) and it doesn't show any adapter. dmesg is the same as yours (1eaf:0003 and then 1eaf:0004).

    I solved "st-flash: error while loading shared libraries: cannot open shared object file" installing 32bit libusb: pacman -S lib32-libusb

    Thanks for sharing your post.


Please read the comments policy before publishing your comment.