Arduino development boards are great devices for embedded control. At the low end are the low cost ATMega328 based Arduinos, such as the Uno, Nano, and Pro Mini. At the other end of the scale there are the high priced but more powerful Arm based devices, such as the Due. But sometimes your application may call for a board with the power of an Arm based device but with the low end cost of a Pro Mini or Nano. In these cases there are of course a large range of alternative microcontrollers; however, these tend to be aimed at the more experienced user and require a deeper understanding of the inner workings of both the microcontroller and their programming tools (IDE).
In recent updates to the Arduino IDE one big improvement they have made to their software is the ability to add unofficial third party development board support via the new â€˜board managerâ€™ feature. This feature has now opened up the ease and simplicity of the Arduino IDE to alternative microcontrollers. Weâ€™ve already seen a big increase in popularity of the Espressif ESP8266 based boards due to support being added to the Arduino IDE and now the same is true for the powerful ST Microelectronics STM32 Arm based microcontrollers. These microcontrollers are not only far more powerful than a standard Arduino, but a basic development board can be purchased for around the price a low end Arduino, such as pro micro. In the table below you can see for yourself how the two similarly priced boards compare:
In this tutorial weâ€™ll show how to quickly set up your Arduino IDE, test your setup and upload the standard â€˜blink sketchâ€™ to the STM32. Note that although this guide can be applied to many ST32 variants, it has been written for the STM32 HCDVBD0033 (aka â€˜black pillâ€™) development board.
To follow the guide you will need:
An STM32 development board such as this one:Â HCMODU0033 STM32F103C8T6 (Black pill) Development Board available here.
A USB to serial to 3.3V TTL adaptor such as this one:Â HCMODU0051 CP2102 3.3V USB to serial port adapter available here.
The Arduino IDE which is available to download for free here.
Step 1) Adding support to the Arduino IDE:
The first thing weâ€™ll need to do is to tell the Arduino IDE where to download the support files for the STM32. In your IDE go to Fileâ†’Preferencesâ€¦
A new window will open up. In this window you will see a text box labelled â€˜Additional Boards Manager URLsâ€™. In this text box we need to paste the URL from where the Arduino IDE can download the additional board support files for the STM32.
Credit: These files have been kindly provided by Dan Drown (http://dan.drown.org).
Cut and paste the line below into this text box:
If there are any previous URLs listed, just add the above line and separate it with a comma.
You can now close this window by clicking the OK button.
In the top menu bar, select Toolsâ†’ Board â†’ Board manager
A new window will open with a list of supported add-ons. Find the package titled ‘STM32F1xx/GD32F1xx boards by stm32duino version xxxx.x.xxâ€™
Next, click on this package and in the bottom right hand corner you will see a version drop-down selection box and an install button appear. Make sure the latest version is selected and then click the install button.
The Arduino IDE will now start installing the package. Depending on your internet connection speed this may take a few minutes.
Once completed you can close the board manager window and return back to the main Arduino IDE.
Important: You will need to restart the IDE otherwise you’ll get a compile error. Once restarted go to Tools â†’ Board and you will now see additional board options for STM32 based development boards under the heading â€˜STM32 Boards (STM32Duino.com)â€™. Select the appropriate board option for your development board. For the board used in this guide select â€˜Generic STM32F103C seriesâ€™. Under â€˜Toolsâ€™ your board settings should now look like this:
Board: “Generic STM32F103C series”
Variant: “STM32F103C8 (20k SRAM, 64k Flash)”
CPU Speed (MHz): “72MHz (Normal)”
Upload method: “Serial”
Optimize: “Smallest (default)”
Step 2) Connecting your hardware:
The Arduino IDE can program the ST32 development board via its serial port just like programming a Pro Micro. To do this you will first need to connect your USB adapter to the board. For the development board used in this example the serial adapter can be connected as follows:
Now plug your USB adapter into your computer and select its COM port via Tools â†’ Port. One final step before we can upload a sketch is to put the board into its bootloader mode so that it can accept the uploaded sketch via its serial port. To do this put the yellow B0 jumper into the B0+ position (see above image). The jumper must be kept in this position when programming the board. Once the sketch has finished uploading it will automatically run. However if at any point the board is reset or power removed whilst the jumper is in this position the uploaded sketch will be erased. To stop this from happening you must put the B0 jumper back in its B0- position.
Step 3) Uploading the blink sketch:
With the Arduino IDE set up and the development board connected to your computer you can now upload the blink test sketch. So that we can see the sketch running weâ€™ll use the pin connected to the on-board LED which is connected to digital pin PB12. You can use the modified blink sketch below. Just cut and paste it into the Arduino IDE window.
#define LED_PIN PB12
If everything is set up correctly it should now be a simple case of uploading the sketch by clicking the IDEs â€˜Upload buttonâ€™. If all goes well the IDE will compile the sketch and upload it to the board and the on-board LED will start blinking.
You have now successfully configured and programmed your development board via the Arduino IDE. Note that this sketch is making use of the standard Arduino digital pin commands to configure and control the GPIO pin 2. You can use many of the built in commands contained within the Arduino IDE to control features of this development board in the same way you would if you were using an ordinary Arduino development board.