STM32

STM32 CubeIDE Course for beginners with Examples

STM32 CubeIDE Course:

 

STM32 CubeIDE Course for beginners with Examples- Welcome to my STM32CubeIDE Course specifically designed for Beginners. In this course, we will take a step-by-step approach to ensure that you gain a solid understanding of how to use CudeIDE for programming the STM32 Microcontroller.

Throughout this course, I will be using the most popular STM32F103C8T6 microcontroller board which is also known as Blue Pill, and for uploading the programming I will use the ST-Link V2.

Since, this course is for the absolute beginners, so, I will try my level best to explain each and every detail including,

  1. STM32 Blue Pill Board Technical specifications and Pinout.
  2. What is CubeIDE? and
  3. How to Download and Install STM32CudeIDE.

After covering the most basic things then I will practically show you, how to use the most commonly used electronics components with the STM32. Since this course is for the beginners, so first I will start with the easiest example which is controlling the STM32 onboard LED.

Then in the 2nd example, I will show you how to connect and control external LEDs. These LED blinking examples will help you in understanding; how to turn ON and turn OFF any GPIO pins on the STM32.

In 3rd example, I will show you, how to read a digital input on any GPIO pin of the STM32; for this, I will use a Pushbutton. We will be reading and controlling both at the same time. The STM32 board will sense the button click and then accordingly will turn ON or turn OFF the LED.

In 4th example, I will show you how to interface an I2C supported 16×2 LCD. This is one of the most commonly used LCDs; We will simply print the Hello World text message on the LCD module.

In the 5th example, I will show you how to use an analog sensor with the STM32 and display its value on the I2C supported 16×2 LCD. For demonstration purposes, I will be using a Potentiometer as the analog sensor. After understanding this example then you would be able to use all types of Analog sensors, like for example, LDRs, Flex Sensors, Force Sensors, etc. There is a long list of the Analog Sensors.

In the 6th example, I will show you how to connect an I2C Supported Oled display module with the STM32. I will write a very basic program to print some text on the Oled display module. This is really an important example because in most of the projects you will need an Oled display module to print text messages and sensor values.

In 7th example, I will explain how to make a simple distance measurement system using the most popular HC-SR04 Ultrasonic Sensor. We will simply measure the distance and then print it on the i2c supported SSD1306 Oled display module.

These examples will help you in getting started with the STM32 and CubeIDE. So, without any further delay, let’s get started!!!




Amazon Links:

STM32 Blue Pill Board

SSD1306 Oled Display Module

HC-SR04 Ultrasonic Sensor

Push Button

I2C supported 16×2 LCD

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

STM32F103C8T6:

Throughout this Course, I will use the STM32 “Blue Pill” board which is based on the STM32F103C8T6 microcontroller unit from STMicroelectronics STM32F1 series. The STM32 Blue Pill controller board is based on the ARM Cortex-m3 and is commonly known as the “Blue Pill” development board.

Let me share with you some of its key information.

Microcontroller Core: It is based on the ARM Cortex-M3 32-bit RISC processor, which offers a good balance between performance and power efficiency.

Clock Speed: The STM32F103C8T6 MCU typically operates at a maximum clock speed of 72 MHz.

Flash Memory: It comes with different variants offering various amounts of Flash memory. The most common one is 64 KB of Flash memory, which is used to store the program code.

RAM: It has 20 KB of SRAM (Static Random Access Memory) that is used for data storage during program execution.

Peripherals: The MCU includes a wide range of peripherals, including GPIO (General Purpose Input/Output) ports, timers, USART, SPI, I2C, ADC (Analog-to-Digital Converter), and more.

Operating Voltage: The operating voltage typically ranges from 2.0V to 3.6V, making it suitable for low-power applications.

Development Environment: To program and develop applications for the STM32F103C8T6 MCU, you can use the STM32CubeIDE, which is an integrated development environment provided by STMicroelectronics. Additionally, you can use various other development tools and programming languages like C/C++ and Assembly.

The STM32F1 series has been widely used in various applications, including industrial automation, consumer electronics, IoT devices, robotics, and more. Its versatility, performance, and affordability make it a popular choice among developers and hobbyists for various embedded projects.



STM32 Blue Pill Board Pinout:

STM32 CubeIDE

It has a total of 40 pins, 20 on each side.

STM32 CubeIDE

Power Pins:

It has two 3.3V pins connected to the onboard 3.3V regulated power supply which is used to power up the onboard components including the microcontroller and other external components like sensors and breakout boards. So, if you need 3.3 volts to power up your sensor; you can simply connect 3.3V from any of these two pins.

Ground Pins:

It has 3 ground pins, one on this side, and the other two on this side. All these ground pins are interconnected so you can take a ground connection from any of these 3 pins.

5V Pin:

The 5V pin is the Input Power pin means you can connect an external regulated 5V power supply to this pin to power up the STM32 board. You don’t need to worry about the microcontroller, the onboard 3.3V regulator will convert the 5 volts into 3.3 volts.

While performing your initial experiments; you don’t need to connect an external 5V regulated power supply. You can just use your laptop as the power source. But sooner or later, you will need an external power supply, because its impractical to use your laptop or PC as the power source. So, I recommend you should build yourself a 5V and 3A power supply. In some examples, I have used my designed 5V and 3A power supply for powering up the STM32 Microcontroller board.



STM32F103C8T6 GPIOs:

It has a total of 32 GPIO pins numbered as B0 to B15, C13 to C15, and A0 to A15.

While in the Pinout diagram these are numbered as PB, PC, and PA.

STM32F103C8T6 Analog Pins:

It has a total of 10 analog pins starting from A0 to B1. These pins are connected to the built-in analog-to-digital converter (ADC) of the microcontroller and can be used to measure analog signals.

I2C pins:

It has two i2c supported ports. The I2C1 is available on pins B6 and B7. B6 is the SCL and B7 is the SDA. The I2C 2nd port is available on pins B10 and B11. B10 is the SCL and B11 is the SDA.

Serial communication Pins:

It has 3 Serial ports. The USART1 is available on pins A9 and A10 (A9 is the Tx and A10 is the Rx). USART2 is available on pins A2 and A3(A2 is the Tx and A3 is the Rx). USART3 is available on pins B10 and B11(B10 is the Tx and B11 is the Rx).

And it also has pins for the SPI, PWM, and CAN. All the pins are clearly labeled.

STM32 CubeIDE

Let me also tell you. When you hold the STM32 controller board like this, All the pins on the right side from B9 to B12 are 5V tolerant except the B5 pin. PB_5 or B5 is 3.3V standard Pin.

All the pins on the left side from Vbat “VB” to the Reset pin are 3.3V Standard pins except the B10 and B11 which are 5V tolerant Pins.

JTAG/SWD Pins:

These 4 pins on the STM32 Board are the SWD (Serial Wire Debug) pins that allow you to connect a debugger or a programmer like the ST-Link V2.



ST-Link V2:

STM32 CubeIDE

The ST-link V2 allows you to program the firmware onto STM32 microcontrollers and also provides debugging capabilities. It connects to the STM32 Microcontroller’s SWD Pins for programming and debugging operations. It is designed to work seamlessly with the STM32CubeIDE which is an integrated development environment; provided by STMicroelectronics. I think, I have shared enough valuable information with you guys and now its time to download and install the STM32CubeIDE.

Altium Designer + Altium 365 + Octopart:

Arduino LoRa Free SMS

Altium 365 lets you hold the fastest design reviews ever. Share your designs from anywhere and with anyone with a single click. it’s easy, leave a comment tagging your teammate and they’ll instantly receive an email with a link to the design. Anyone you invite can open the design using a web browser. Using the browser interface, you’re able to comment, markup, cross probe, inspect, and more. Comments are attached directly to the project, making them viewable within Altium designer as well as through the browser interface. Design, share, and manufacture, all in the same space with nothing extra to install or configure. Connect to the platform directly from Altium Designer without changing how you already design electronics. Altium 365 requires no additional licenses and comes included with your subscription plan.

Get real-time component insights as you design with Octopart built into Altium 365. Octopart is the fastest search engine for electronic parts and gives you the most up-to-date part data like specs, datasheets, cad models, and how much the part costs at different amounts etc. Right in the design environment so you can focus on your designs. Start with Altium Designer and Activate Altium 365. Search for electronic parts on Octopart.




STM32CubeIDE Download & Installation:

While you are on the stm32cubeide official page go to the SMT32 Developer Zone and from the drop down list select STM32CubeIDE.

STM32 CubeIDE

Click on the Download STM32CubeIDE button.

STM32 CubeIDE

Download the one as per your needs. In my case I am going to continue with Windows Latest Version.

STM32 CubeIDE

If you are already a registered use then the STM32CubeIDE software download with start right away. But if you are not a registered user then first you will have to create an account. Just go through the following steps. If in case, you face any difficulty then you can watch my Video Tutorial given at the end of this Article.

  • Accept the License Agreement…
  • Click on the Register button…
  • Click on the Create Account…
  • Enter the required details…
  • Finally, check the box and click on the Register button…
  • Next, open your email account and click on the Validate Now…
  • Complete your registration by entering your password…
  • Click the submit button and you will be notified “The registration has been completed. Please login here…
  • Enter your registered email id and password and click on the Login button…
  • Now, you can click on the Download STM32CubeIDE and start downloading the STM32CubeIDE…
  • Extract the downloaded Zip Folder…
  • Open the folder and run the .exe file to start the installation process…

There is nothing complicated, just follow the instructions, and if any message pops up click on the accept button “It might be a driver”, anyway you will know.

STM32 CubeIDE

The STM32CubeIDE has been installed.



Example #1: STM32F103C8T6 Onboard Led Blinking

STM32 CubeIDE

In this first example, we are going to control the STM32 onboard LED which is connected to PC13 Pin. Before, we start the programming, first we will need to connect the STM32 board to the laptop or PC. For this, we are going to use the ST-LINK V2.

STM32 CubeIDE

Simply connect the ST-LINK V2 3.3V and GND pins to the 3.3V and GND pins on the STM32 board. Connect the SWCLK and SWDIO pins of the ST-Link to the STM32 SWCLK and SWDIO Pins.

STM32 CubeIDE

Finally, connect the ST-Link V2 to your Laptop/PC. Throughout these examples, the ST-Link V2 will remain connected to the STM32 board.

STM32 CubeIDE

Each time, you upload program to the STM32 board, you will have to connect the ST-Link V2 to the Laptop/PC.

Open the STM32CubeIDE Software. If you are running this for the first time you might get a notification or message “Select a directory as workspace” where you want to store all your projects or you can go with the default address.

STM32 CubeIDE

Finally, click on the Launch Button. STM32CubeIDE will display the welcome screen, indicating that the IDE has been successfully launched.

STM32 CubeIDE

To start a new project, go to the File Menu, then to New, and click on the STM32 Project. STM32CubeIDE may take a moment to load and download the necessary libraries. Please be patient during this process as it may take some time depending on the speed of your internet connection.

STM32 CubeIDE

On the Target Selection Window and while the MCU Selector Tab is active; select the Commercial Part Number that is STM32F103C8T6, Select it from the MCUs list and then click on the Next Button.

STM32 CubeIDE

Write the project name, select the Targeted Language, target Binary Type, and Targeted Project Type. Finally, click on the Finish Button.

STM32 CubeIDE

On the Pinout & Configuration Tab click on the System Core and select SYS and click on the Debug and select Serial Wire.

STM32 CubeIDE

Click on PC13 and select GPIO_Output as the onboard LED is connected to this pin. Finally, click on the save button if it asks you Do you want to generate Code?  click yes, again press the yes button.

STM32CubeIDE will generate the necessary code based on your pin configuration and open the associated perspective. This perspective provides you with the appropriate tools and views for further development and customization of your project. While you are on the main.c file, scroll down. Now, in the while(1) function we will write code for blinking the LED.



STM32CubeIDE Led Blinking Code:

Once the code is completed click on the Hammer Icon or Build, There are no errors and warnings now we can click on the Play button

STM32 CubeIDE

Click on the Debugger Tab, Select ST-LINK, and Click on Show Generator options.

STM32 CubeIDE

Set the Reset Mode to Software system reset, click the Apply button, and then click the OK button.

STM32 CubeIDE

After pressing the OK button, if you see the shutdown command invoked.

STM32 CubeIDE

Then you can remove the ST-Link V2, and Plug it again to restart the STM32 Controller board.

STM32 CubeIDE

You can see the Onboard LED is blinking. We just built our first Led blinking project.

In next example, I am going to explain how to control external LEDs.



Example #2: STM32F103C8T6 External Leds

In this 2nd example, we are going to control external LEDs. But to keep things simple and easy to follow, first let’s start with a single LED and afterwards we will control multiple LEDs. Anyway, let me explain the connections.

STM32 CubeIDE

The Cathode legs of all the Leds are connected together and then connected to the GND pin on the STM32 board. The Anode Legs of all the LEDs are connected to the GPIO pins PB6, PB7, PB8, and PB9 through these current limiting resistors (330 ohms).

Connect the ST-Link V2 to your Laptop/PC.

The following steps are exactly the same as explained in the first example.

While the STM32CubeIDE is open, go to the File, then to New, and click on the STM32 Project. STM32CubeIDE may take a moment to load and download the necessary libraries. Please be patient during this process as it may take some time depending on the speed of your internet connection.

On the Target Selection Window and while the MCU Selector Tab is active; select the Commercial Part Number that is STM32F103C8T6. Select it from the MCUs list and then click on the Next Button. Write the project name, Select the Targeted Language,Target Binary Type, and Targeted Project Type.

On the Pinout & Configuration Tab click on the System Core and select SYS and Click on the Debug and select Serial Wire.

STM32 CubeIDE

Click on PB9 and select GPIO_Output as the external LED is connected to this pin.

STM32 CubeIDE

Repeat the same steps for the other three GPIO pins to which the external LEDs are connected. Finally, click on the save button if it asks you Do you want to generate Code?  click yes, again press the yes button.

STM32CubeIDE will generate the necessary code based on your pin configuration and open the associated perspective. This perspective provides you with the appropriate tools and views for further development and customization of your project. When the main.c file is opened, Scroll down, and in the while(1) function we will write code for blinking one LED that is connected to B9 or PB9.

Single External Led STM32CubeIDE Code:

STM32 CubeIDE



Multiple External Led STM32CubeIDE Code:

Copy the above code, and paste it in your main.c file. I am sure now you know how to save the file, and how to run the project, I already explained this in the Example #1. Anyway, Let’s click on the Play button.

STM32 CubeIDE

You can see this time I am able to control multiple LEDs. Now, you can modify this program to control less or more LEDs. In next example, I am going to explain how to read digital input.

Example #3: STM32F103C8T6 Digital Input

In this 3rd example, you are going to learn how to read a digital input on any GPIO pin of the STM32 microcontroller board. We will be monitoring and controlling both at the same time. Each time the button is pressed the Led is going to change its state. Anyway, Let’s take a look at the wiring.

STM32 CubeIDE

The Anode Leg of the Led is connected to the STM32 GPIO pin PA1 through a 330 ohm current limiting resistor and the Cathode Leg of the Led is connected to the GND.

One side of the Push Button is connected to the GND and the other side of the Push Button is connected to 3.3V through a 10k ohm Pull-Up resistor. A wire from the middle is connected to the STM32 GPIO Pin PB1. When the button is open the PB1 pin reads 3.3V and when the button is pressed it reads 0V.

Just like example number1 and example number2 connect the STM32 board to the Laptop/PC via the ST-Link V2.

The following steps are exactly the same as explained in the first example. Incase if there is any confusion; watch the video tutorial given at the end of this article.

While the STM32CubeIDE is open, go to the File, then to New, and click on the STM32 Project. STM32CubeIDE may take a moment to load and download the necessary libraries. Please be patient during this process as it may take some time depending on the speed of your internet connection.

On the Target Selection Window and while the MCU Selector Tab is active; select the Commercial Part Number that is STM32F103C8T6. Select it from the MCUs list and then click on the Next Button. Write the project name, Select the Targeted Language,Target Binary Type, and Targeted Project Type.

On the Pinout & Configuration Tab click on the System Core and select SYS and click on the Debug and select Serial Wire.

STM32 CubeIDE

On the Pinout View,

Click on PA1 and select GPIO_Output.

Click on PB1 and select GPIO_Input.

Finally, click on the save button if it asks you Do you want to generate Code?  click yes; and again press the yes button.

STM32CubeIDE will generate the necessary code based on your pin configuration and open the associated perspective. This perspective provides you with the appropriate tools and views for further development and customization of your project.

I have already written some code, so I am going to copy and paste it in the existing code; in the main.c file.



Push Button STM32CubeIDE Code:

Copy the above program and paste it into your main.c file. Save the file, and click on the hammer icon, and play button as we did in the example number1, just repeat the same steps.

STM32 CubeIDE

You can see, each time I press the button, the Led changes its state. You can replace the push button with a digital IR Sensor and the LED with a Relay to make a contactless AC/DC load control system. You can use any type of Digital Sensor. Even you can convert this simple project into a security system using a Digital PIR Sensor. Anyway, in next example we are going to use I2C supported 16×2 LCD with the STM32.



Example #4: STM32F103C8T6 16×2 LCD

STM32 CubeIDE

In this 4th example, I am going to show you, how to print text on this I2C supported 16×2 LCD. This is one of the most commonly used LCD used for displaying text messages and sensor values. Let me explain the connections.

STM32 CubeIDE

Connect the VCC and GND pins of the I2C supported 16×2 LCD to the STM32 board 5V and GND pins. Connect the SCL and SDA pins of the 16×2 LCD to the STM32 GPIO Pins PB10 and PB11. You can follow this circuit diagram.

Connect your STM32 board to the Laptop/PC via ST-link as explained in the previous examples.

The following steps are exactly the same as explained in the first example. Incase if there is any confusion; watch the video tutorial given at the end of this article.

While the STM32CubeIDE is open, go to the File, then to New, and click on the STM32 Project. STM32CubeIDE may take a moment to load and download the necessary libraries. Please be patient during this process as it may take some time depending on the speed of your internet connection.

On the Target Selection Window and while the MCU Selector Tab is active; select the Commercial Part Number that is STM32F103C8T6. Select it from the MCUs list and then click on the Next Button. Write the project name, Select the Targeted Language,Target Binary Type, and Targeted Project Type.

As usual, on the Pinout & Configuration Tab click on the System Core and select SYS and click on the Debug and select Serial Wire.

STM32 CubeIDE

Click on the connectivity and select I2C2. You can see the PB_10 and PB_11 pins are selected.

STM32 CubeIDE

Finally, click on the save button if it asks you Do you want to generate Code?  click yes…again press the yes button.

STM32CubeIDE will generate the necessary code based on your pin configuration and open the associated perspective. This perspective provides you with the appropriate tools and views for further development and customization of your project.

On left side, click on Core then Inc, right click and select new File.

STM32 CubeIDE

Write the file name liquidcrystal.h.

STM32 CubeIDE

You can see the liquidcrystal.h header file has been added.

STM32 CubeIDE

Now copy and paste the following code.



Liquidcystal_i2c.h STM32CubeIDE Header File Code:

Save this file.

Then go to the Src and create a new File. Write the File name liquidcrystal_i2c.c.

STM32 CubeIDE

Copy the following code and paste it in the liquidcrystal_i2c.c file.

liquidcrystal_i2c.c code: