How to program a STM32 “Blue Pill” with Arduino

In this tutorial we are going to program a stm32f103c8t6 development board, nicknamed “Blue Pill” with Arduino.

When looking at a few pages on the STM32; I was a bit skeptical on how easy it would be to program this board. I have seen tutorial where you need to fiddle with the onboards jumpers; then strategically press the reset button. That seemed really not user friendly. In fact; it is really easy to program the blue pill board, and none of this gymnastic is actually necessary. Here’s a simple, no frills tutorial on how to get you started!

Material Needed

A STM32 “Blue Pill” board

The Blue Pill on a standard breadboard
The Blue Pill on a standard breadboard

They can be acquired off Aliexpress, eBay and many others.

A ST-LINK/V2 in-circuit debugger/programmer for STM8 and STM32

ST-LINK/V2 in-circuit debugger/programmer for STM8 and STM32
ST-LINK/V2 in-circuit debugger/programmer for STM8 and STM32

STMicroelectronics sells their own ST-LINK/V2, which can be bought for under US$25 from official retailers such as Mouser and Digikey. However, this piece of hardware has been reverse engineered and you can find fully compatible “emulated” ST-Link for about US$2 from the typical Chinese resellers. Chances are the shop that is selling the blue pill board also sells a ST-Link.

From a hardware perspective it is nothing more than a USB to SWD (Serial Wire Debug) interface. These cheap copies do not provide a full 20-pin JTAG connector; but it is not needed for this Cortex M3 based micro-controller; so it is perfectly adequate for the job. If you’re coming from the Atmel/AVR Arduino world; just think about this ST-Link as an equivalent to the STK600 programmer.

 

Configuring Software

Under Arduino, click File/Preferences; then click on the button on the right of “Additional Boards Manager URLs”

Under File/Preferences, add the STM32 Arduino Core
Under File/Preferences, add the STM32 Arduino Core

Add the following address into the textbox then click OK:

https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json

Now click on Tools/Board/Board Manager. Under Type; select “Contributed” and install the latest version of “STM32 Core by STMicroelectronics”

Install support for STM32 micro-controllers
Install support for STM32 micro-controllers

The installation might take a while. The Arduino software will download a complete ARM debugging/compiling toolchain which spans a few hundred megabytes. In the meantime, you can check out the awesome Arduino core Github page project that gives a lot of details about what you’re actually installing. It’s also worth checking out out the licenses; which is a mixture of mostly free and copyleft but you should review it before trying to make any commercial application with this piece of software.

 

Loading a “Hello World” blinking LED

To make sure everything works properly; you can try loading the “blink” example. The Blue Pill is pretty much devoid of any on board peripherals, but it has at least an on board LED. From the Arduino menu, click on File/Examples/01Basics/Blink.

Loading the "Blink" example from the Arduino software
Loading the “Blink” example from the Arduino software

Now, from the Tools menu, make sure everything is set up properly:

Settings to load a program into the STM32 with a ST-LINK/V2
Settings to load a program into the STM32 with a ST-LINK/V2
  • Board: Generic STM32F103 Series
  • Board part number: BluePill F103C9
  • Serial Interface: Enabled with generic Serial
  • Flash Memory Size: 64k (you can try 128k; most boards are sold as 64k but in fact have 128k of flash)
  • Upload method: STLink

And that’s it, click on the upload button and the compilation and upload process should start. At the end you should have a message similar to the one below:

Uploading code to a STM32 "Blue Pill" success message
Uploading code to a STM32 “Blue Pill” success message

The fruit of all your efforts should now be visible:

Note: My STM32 board came pre-loaded with a blinking sketch. To make sure your upload was actually successful and not running the default program on your board, try varying the blinking speed by playing with the values in the “delay” calls.

 

Going further

Sky’s now the limit with your new board. To go further and make full use of the board and its hardware peripherals; be sure to refer to this pinout diagram; courtesy of Rasmus Friis Kjeldsen:

STM32 "Blue Pill" pinout
STM32 “Blue Pill” pinout

 

References:

https://github.com/stm32duino/Arduino_Core_STM32

http://wiki.stm32duino.com/index.php?title=Blue_Pill

http://reblag.dk/stm32/

css.php