Panel Cookies
STM32 and Arduino IDE
03/06/2021 | Views: 16900 | Arduino | by: ELECTRONOOBS      

Today I’ll start working with the STM32, actually the STM32F103C series or the so-called the blue pill. I’ve already prepared a few projects with this microcontroller for future tutorials. This board is way more powerful than the Arduino NANO for example, having more memory, has a 32 bits architecture and could run up to 72MHz, or more. In this tutorial I will show you how to program this microcontroller using the Arduino IDE, install the boards and so on.

Part 1 - About the STM32

The STM32F103C is a 32 bits architecture microcontroller. Compared with the Arduino UNO NANO or the MEGA for example, which are 8 bits microcontrollers, this chip has 32 bits so it could move 4 times more bits on a single clock pulse so just with that it would be at least 4 times faster. On top of that, this could run by default at 72Mhz and if you remember the Arduinos UNO, NANO or the MEGA are running at just 16MHz, so that is once again 4.5 times faster. You could also overclock the STM32 up to 128MHz which is crazy. This is the pinout of this microcontroller and it has 26 I/O pins with interruptions for all, it has 10 analog inputs and the ADC is of 12bits so it has 4 times more resolution than the ATmega328 for example which has 10 bits ADCs. Internally it has 7 timers, two I2C ports, 3 UART ports, 2 SPI ports, and the PWM pins are of 16 bits, so 65 thousand values, so yeah, this microcontroller is awesome.

Arduino IDE stm32

Part 2 - Connections to PC

Ok, so let’s see how to program this microcontroller using the ARDUINO IDE platform. First, get one of those external FTDI programmers. Mine is from eBay and cost me just 2 dollars. Make sure you switch the jumper of the programmer from 5V to 3.3V because the STM32 works at that voltage. Make the connections below between the programmer and the STM32 connecting ground to ground, 3.3 to 3.3, TX from the programmer to pin A10 and RX to pin A9 and those will be the UART port pins.

Arduino STM32 FTDI programmer

Part 3 - Install STM32 boards

Now open Arduino IDE. Copy this link from below and go to preferences, and in the additional boards paste the link. If you already have a link there, just add a comma and then paste the new link. Now go to tools, board and select boards manager and wait a few seconds. Then search for STM32F1 and install these boards. Once you do that, if you now go to tools, boards, you should have the STM32 boards installed wo we could work with the blue pill. Ok, let’s make a simple code.

Arduino STM32 FTDI programmer

Part 4 - Simple code

From the pinout before we know that we have and LED connected to pin PC13. So, in Arduino I make a blink code where I set PC13 as output and set it high and low for one second. Go to tools, board and select the generic STM32F103C board. Select this variant and for the upload method select serial since we are using a serial FTDI programmer and also the 72Mhz speed for the clock.

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(PC13, OUTPUT);

// the loop function runs over and over again forever
void loop() {
  digitalWrite(PC13, HIGH);           // turn the LED on (HIGH is the voltage level)
  delay(1000);                        // wait for a second
  digitalWrite(PC13, LOW);            // turn the LED off by making the voltage LOW
  delay(1000);                        // wait for a second

Part 5 - Upload the code

Now, before you connect the USB cable, make sure you move the jumper from position 0 to 1 as you can see in the schematic above. That will put the microcontroller into programming mode. Then you can connect the USB connector to the FTDI programmer. Go back to Arduino and in tools select the com of that programmer and click upload. The LEDs of the programmer will blink and there you go, the LED on PC13 is now blinking each second. We have successfully uploaded a code to this chip.

Arduino STM32 FTDI how to uplaod code

Part 6 - Permanent codes

Now if you reset the PCB at this point, the code will get erased and you will see that the LED won't blink anymore. If you want to keep the code, before you reset the board, switch back the jumper from 1 to 0 and then reset and the code will run forever. When you power back the PCB, the code is still running. You can use the STM32 pretty much the same as the Arduino using the Serial to print data, using the analog Read or digital Read to detect values on the digital pins, you could set pullup or pulldown resistors in the code, and so on, just remember to change the name of the pins according to the pins of the STM32. So, till here was the introduction of this microcontroller.

Arduino STM32 IDE tutorial

Part 7 - Bootloader Burn

Ok, how about programming the STM32 using the USB connector that is has. For that we need to burn a bootloader to the chip. For that download the 3 files from below, the Arduino STM32 master, the Flash loader software and the STM32 bootloader master folder. Unzip the files. First go in to Arduino STM32 master folder, to drivers, win, and here, run the install drivers.bat. Wait a few minutes and this will install the drivers so your PC would detect the STM32 as a new COM input.

THen make the same connections between the external FTDI programmer and the STM32 as before. Switch th jumper from 0 to 1. Then connect the USB connector to the programmer. Now go to the flash loader folder and install that software and run it. In that program select the COM of the FTDI programmer and click next. Click next again, and again. On that page mark the download to device dot and press the 3 dots button. Go to the STM32 bootloader master folder, to binaries and select the generic boot 20 PC13 file. Press next and a bootloader will be burned to the microcontroller. Important, once finished, before you remove power you must change the jumper back to 0. Then connect the USB to the STM32 and it should appear on your PC as Maple mini. In the Arduino, just change the upload method from serial to STM32duino bootloader and upload the codes without the external programmer, that’s it.

Part 8 - See full Video

That's how to program the STM32 using the Arduino IDE platform. I hope you like this tutorial and maybe you have learned something new. If my videos help you, consider supporting my work on my PATREON or a donation on my PayPal. Thanks again and see you later guys.

03/06/2021 | Views: 16900 | Arduino | by: ELECTRONOOBS      

Last tutorials

3D Printed Headphones with PLANAR PCB
FOC control with Arduino + magnetic encoder feedback
Laser Power Meter DIY PCB
All about Arduino PWM frequencies
Smallest ESC based on ARDUINO


Affiliate Disclosure


PCBWAY PCB service

Curso Arduino Online nivel Intermedio