Arduino and STM32. A gentle start with ARM.

views 12:00 pm 0 Comments February 19, 2019

Arduino conquered the market and the hearts of hobbyist electronics enthusiasts in a storm. Thanks to this platform, it was possible to start the adventure with microcontrollers. However, there are applications where the power provided by the popular UNO or Leonardo is too little. An enticing alternative is to use the STM32 processor in an extremely cheap version of a development board, known as the Blue Pill. Yes, even cheaper than the Arduino UNO clones, because it is available on a popular domestic auction portal for less than 20 u20ce, delivery included.

The heart of the module is the STM32F103 microcontroller, with excellent parameters. The features that distinguish this chip are:

– 32-bit ARM Cortex-M3 architecture, 

– it operates at frequencies of several dozen MHz, 

– it has a decent amount of memory: up to 32 KB flash, up to 10KB SRAM,

– plenty of communication interfaces: USART, I2C, SPI, CAN, USB 2.0.

– 12-bit A/D converters

– low power consumption modes

and many others. 

What will we need?

1. A computer with the Windows system. I prepared the following tutorial for the Windows 10 system.

2. Blue Pill (cost up to 20 u20ce). It is best to search by entering the query u201cstm32f103 blue pillu201d in the search engine.

3. USB-UART converter. If you donu2019t have one yet, I recommend buying a module based on the FT232 chip, because the driver installation is very simple. I did not discuss this installation, but if there is such a need, write to me and I will add it. To acquire such a module, enter for example in the search of an auction portal: u201cusb uart ft232u201d. 

The following steps assume that you have Arduino installed (in my case version 1.8.8), the USB-UART converter connected, and the converter drivers installed. 

Installation, configuration, and a simple program blinking the LED 

1. We disconnect the converter from the computer, in order not to damage any of the devices.

2. We connect the Blue Pill to the converter: 

GND from the converter -> GND on the Blue Pill board

5V from the converter -> 5V on the Blue Pill board

TX on the converter -> A10 on the Blue Pill board

RX on the converter -> A9 on the Blue Pill board

 

3. The next step will be installing support for STM32 in the Arduino environment. 

a) We start by adding an additional URL to the boards manager. We go to u201cFileu201d, then select u201cPreferencesu201d and in the u201cAdditional Boards Manager URLsu201d field we enter: 

http://dan.drown.org/arduino/package_STM32duino_index.json

We confirm by clicking u201cOKu201d.

b) We install support for our board. We go to u201cToolsu201d, expand the u201cBoardu201d submenu and select u201cBoards Manageru201d. Sometimes after the manager window opens, you need to wait a moment until the data is updated u2014 a progress bar will appear at the bottom of the window.

Then, in the search field we enter u201cSTM32F1u201d and select the STM32F1xx/GD32F1xx(…) entry. We press u201cInstallu201d and wait until the necessary files are downloaded and installed.

After this operation we close the Arduino environment and start it again. 

4. We configure Arduino to work with the Blue Pill.

In the u201cToolsu201d menu:

– as u201cBoardu201d we select u201cGeneric STM32F103C seriesu201d,

– u201cVariantu201d in our case will be set as u201cSTM32F103C8 (20k RAM, 64k Flash)u201d,

– we set the u201cUploadu201d method as u201cSerialu201d,

– we set the u201cPortu201d to the port number of our converter. Usually it will be COM3 or higher,

– we set the u201cProgrammeru201d as u201cParallel Programmeru201d. 

5. Now itu2019s time for the program. Of course weu2019ll start with blinking the LED, which the Blue Pill is equipped with.

In u201cFileu201d we select u201cExamplesu201d, then u201c01.Basicsu201d and u201cBlinku201d. Here we have to make a small modification to the code, because the LED_BUILTIN constant is not declared for this board.

To fix this, at the beginning of the code, right under the comment block at the start, we declare: 

#define LED_BUILTIN PC13

The complete code (omitting the top comment block) looks as follows: 

#define LED_BUILTIN PC13

 

// the setup function runs once when you press reset or power the board

void setup() {

  // initialize digital pin LED_BUILTIN as an output.

  pinMode(LED_BUILTIN, OUTPUT);

}

 

// the loop function runs over and over again forever

void loop() {

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(1000);                       // wait for a second

  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW

  delay(1000);                         // wait for a second

}

6. Before programming the board we have to set the BOOT0 jumper to u201c1u201d. The BOOT0 jumper is located closer to the pin header, farther from the RESET button.

Then we press the RESET button and load the program into the board. Right after loading, the LED should start blinking. Now we set the BOOT0 jumper to u201c0u201d.

Done ! 

At this point we can reconnect our Blue Pill to power and we should see the blinking LED.

Note: Some chips bought directly from the u201cAsianu201d portal may be locked. The module with which I encountered this problem was distinguished from the others by a plastic RESET button. The aforementioned lock manifests itself, among other things, by an error during programming with the message u201cCanu2019t initiate chip erase!u201d. Such a chip requires prior unlocking, which is beyond the scope of this post. If you encounter such a problem and need help, let me know and I will publish a post with the solution to this problem.