STM32 CubeIDE Course
STM32 CubeIDE Course
PIN STM32F103C8T6:
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.
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:
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.
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.
Download the one as per your needs. In my case I am going to continue with Windows
Latest Version.
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.
The STM32CubeIDE has been installed.
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.
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.
Finally, connect the ST-Link V2 to your Laptop/PC. Throughout these examples, the ST-
Link V2 will remain connected to the STM32 board.
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.
Finally, click on the Launch Button. STM32CubeIDE will display the welcome screen,
indicating that the IDE has been successfully launched.
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.
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. Finally, click on the Finish Button.
On the Pinout & Configuration Tab click on the System Core and select SYS and click on
the Debug and select Serial Wire.
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:
1 /* USER CODE BEGIN Header */
2 /**
3 ******************************************************************************
4 * @file : main.c
5 * @brief : Main program body
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2023 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18 /* USER CODE END Header */
19 /* Includes ------------------------------------------------------------------*/
20 #include "main.h"
21
22 /* Private includes ----------------------------------------------------------*/
23 /* USER CODE BEGIN Includes */
24
25 /* USER CODE END Includes */
26
27 /* Private typedef -----------------------------------------------------------*/
28 /* USER CODE BEGIN PTD */
29
30 /* USER CODE END PTD */
31
32 /* Private define ------------------------------------------------------------*/
33 /* USER CODE BEGIN PD */
34
35 /* USER CODE END PD */
36
37 /* Private macro -------------------------------------------------------------*/
38 /* USER CODE BEGIN PM */
39
40 /* USER CODE END PM */
41
42 /* Private variables ---------------------------------------------------------*/
43
44 /* USER CODE BEGIN PV */
45
46 /* USER CODE END PV */
47
48 /* Private function prototypes -----------------------------------------------*/
49 void SystemClock_Config(void);
50 static void MX_GPIO_Init(void);
51 /* USER CODE BEGIN PFP */
52
53 /* USER CODE END PFP */
54
55 /* Private user code ---------------------------------------------------------*/
56 /* USER CODE BEGIN 0 */
57
58 /* USER CODE END 0 */
59
60 /**
61 * @brief The application entry point.
62 * @retval int
63 */
64 int main(void)
65 {
66 /* USER CODE BEGIN 1 */
67
68 /* USER CODE END 1 */
69
70 /* MCU Configuration--------------------------------------------------------*/
71
72 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
73 HAL_Init();
74
75 /* USER CODE BEGIN Init */
76
77 /* USER CODE END Init */
78
79 /* Configure the system clock */
80 SystemClock_Config();
81
82 /* USER CODE BEGIN SysInit */
83
84 /* USER CODE END SysInit */
85
86 /* Initialize all configured peripherals */
87 MX_GPIO_Init();
88 /* USER CODE BEGIN 2 */
89
90 /* USER CODE END 2 */
91
92 /* Infinite loop */
93 /* USER CODE BEGIN WHILE */
94 while (1)
95 {
96
97 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1);
98 HAL_Delay(100);
99 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);
100 HAL_Delay(100);
101
102 /* USER CODE END WHILE */
103
104 /* USER CODE BEGIN 3 */
105 }
106 /* USER CODE END 3 */
107 }
108
109 /**
110 * @brief System Clock Configuration
111 * @retval None
112 */
113 void SystemClock_Config(void)
114 {
115 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
116 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
117
118 /** Initializes the RCC Oscillators according to the specified parameters
119 * in the RCC_OscInitTypeDef structure.
120 */
121 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
122 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
123 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
124 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
125 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
126 {
127 Error_Handler();
128 }
129
130 /** Initializes the CPU, AHB and APB buses clocks
131 */
132 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
133 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
134 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
135 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
136 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
137 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
138
139 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
140 {
141 Error_Handler();
142 }
143 }
144
145 /**
146 * @brief GPIO Initialization Function
147 * @param None
148 * @retval None
149 */
150 static void MX_GPIO_Init(void)
151 {
152 GPIO_InitTypeDef GPIO_InitStruct = {0};
153 /* USER CODE BEGIN MX_GPIO_Init_1 */
154 /* USER CODE END MX_GPIO_Init_1 */
155
156 /* GPIO Ports Clock Enable */
157 __HAL_RCC_GPIOC_CLK_ENABLE();
158 __HAL_RCC_GPIOA_CLK_ENABLE();
159
160 /*Configure GPIO pin Output Level */
161 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
162
163 /*Configure GPIO pin : PC13 */
164 GPIO_InitStruct.Pin = GPIO_PIN_13;
165 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
166 GPIO_InitStruct.Pull = GPIO_NOPULL;
167 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
168 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
169
170 /* USER CODE BEGIN MX_GPIO_Init_2 */
171 /* USER CODE END MX_GPIO_Init_2 */
172 }
173
174 /* USER CODE BEGIN 4 */
175
176 /* USER CODE END 4 */
177
178 /**
179 * @brief This function is executed in case of error occurrence.
180 * @retval None
181 */
182 void Error_Handler(void)
183 {
184 /* USER CODE BEGIN Error_Handler_Debug */
185 /* User can add his own implementation to report the HAL error return state */
186 __disable_irq();
187 while (1)
188 {
189 }
190 /* USER CODE END Error_Handler_Debug */
191 }
192
193 #ifdef USE_FULL_ASSERT
194 /**
195 * @brief Reports the name of the source file and the source line number
196 * where the assert_param error has occurred.
197 * @param file: pointer to the source file name
198 * @param line: assert_param error line source number
199 * @retval None
200 */
201 void assert_failed(uint8_t *file, uint32_t line)
202 {
203 /* USER CODE BEGIN 6 */
204 /* User can add his own implementation to report the file name and line number,
205 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
206 /* USER CODE END 6 */
207 }
208 #endif /* USE_FULL_ASSERT */
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
Click on the Debugger Tab, Select ST-LINK, and Click on Show Generator options.
Set the Reset Mode to Software system reset, click the Apply button, and then click the
OK button.
After pressing the OK button, if you see the shutdown command invoked.
Then you can remove the ST-Link V2, and Plug it again to restart the STM32 Controller
board.
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.
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:
1 /* USER CODE BEGIN Header */
2 /**
3 ******************************************************************************
4 * @file : main.c
5 * @brief : Main program body
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2023 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18 /* USER CODE END Header */
19 /* Includes ------------------------------------------------------------------*/
20 #include "main.h"
21
22 /* Private includes ----------------------------------------------------------*/
23 /* USER CODE BEGIN Includes */
24
25 /* USER CODE END Includes */
26
27 /* Private typedef -----------------------------------------------------------*/
28 /* USER CODE BEGIN PTD */
29
30 /* USER CODE END PTD */
31
32 /* Private define ------------------------------------------------------------*/
33 /* USER CODE BEGIN PD */
34
35 /* USER CODE END PD */
36
37 /* Private macro -------------------------------------------------------------*/
38 /* USER CODE BEGIN PM */
39
40 /* USER CODE END PM */
41
42 /* Private variables ---------------------------------------------------------*/
43
44 /* USER CODE BEGIN PV */
45
46 /* USER CODE END PV */
47
48 /* Private function prototypes -----------------------------------------------*/
49 void SystemClock_Config(void);
50 static void MX_GPIO_Init(void);
51 /* USER CODE BEGIN PFP */
52
53 /* USER CODE END PFP */
54
55 /* Private user code ---------------------------------------------------------*/
56 /* USER CODE BEGIN 0 */
57
58 /* USER CODE END 0 */
59
60 /**
61 * @brief The application entry point.
62 * @retval int
63 */
64 int main(void)
65 {
66 /* USER CODE BEGIN 1 */
67
68 /* USER CODE END 1 */
69
70 /* MCU Configuration--------------------------------------------------------*/
71
72 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
73 HAL_Init();
74
75 /* USER CODE BEGIN Init */
76
77 /* USER CODE END Init */
78
79 /* Configure the system clock */
80 SystemClock_Config();
81
82 /* USER CODE BEGIN SysInit */
83
84 /* USER CODE END SysInit */
85
86 /* Initialize all configured peripherals */
87 MX_GPIO_Init();
88 /* USER CODE BEGIN 2 */
89
90 /* USER CODE END 2 */
91
92 /* Infinite loop */
93 /* USER CODE BEGIN WHILE */
94 while (1)
95 {
96
97 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, 1);
98 HAL_Delay(500);
99 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, 0);
100 HAL_Delay(500);
101
102 /* USER CODE END WHILE */
103
104 /* USER CODE BEGIN 3 */
105 }
106 /* USER CODE END 3 */
107 }
108
109 /**
110 * @brief System Clock Configuration
111 * @retval None
112 */
113 void SystemClock_Config(void)
114 {
115 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
116 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
117
118 /** Initializes the RCC Oscillators according to the specified parameters
119 * in the RCC_OscInitTypeDef structure.
120 */
121 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
122 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
123 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
124 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
125 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
126 {
127 Error_Handler();
128 }
129
130 /** Initializes the CPU, AHB and APB buses clocks
131 */
132 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
133 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
134 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
135 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
136 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
137 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
138
139 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
140 {
141 Error_Handler();
142 }
143 }
144
145 /**
146 * @brief GPIO Initialization Function
147 * @param None
148 * @retval None
149 */
150 static void MX_GPIO_Init(void)
151 {
152 GPIO_InitTypeDef GPIO_InitStruct = {0};
153 /* USER CODE BEGIN MX_GPIO_Init_1 */
154 /* USER CODE END MX_GPIO_Init_1 */
155
156 /* GPIO Ports Clock Enable */
157 __HAL_RCC_GPIOA_CLK_ENABLE();
158 __HAL_RCC_GPIOB_CLK_ENABLE();
159
160 /*Configure GPIO pin Output Level */
161 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9,
162 GPIO_PIN_RESET);
163
164 /*Configure GPIO pins : PB6 PB7 PB8 PB9 */
165 GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
166 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
167 GPIO_InitStruct.Pull = GPIO_NOPULL;
168 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
169 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
170
171 /* USER CODE BEGIN MX_GPIO_Init_2 */
172 /* USER CODE END MX_GPIO_Init_2 */
173 }
174
175 /* USER CODE BEGIN 4 */
176
177 /* USER CODE END 4 */
178
179 /**
180 * @brief This function is executed in case of error occurrence.
181 * @retval None
182 */
183 void Error_Handler(void)
184 {
185 /* USER CODE BEGIN Error_Handler_Debug */
186 /* User can add his own implementation to report the HAL error return state */
187 __disable_irq();
188 while (1)
189 {
190 }
191 /* USER CODE END Error_Handler_Debug */
192 }
193
194 #ifdef USE_FULL_ASSERT
195 /**
196 * @brief Reports the name of the source file and the source line number
197 * where the assert_param error has occurred.
198 * @param file: pointer to the source file name
* @param line: assert_param error line source number
199
* @retval None
200
*/
201
void assert_failed(uint8_t *file, uint32_t line)
202
{
203
/* USER CODE BEGIN 6 */
204
/* User can add his own implementation to report the file name and line number,
205
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
206
/* USER CODE END 6 */
207
}
208
#endif /* USE_FULL_ASSERT */
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.
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.
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.
Click on the connectivity and select I2C2. You can see the PB_10 and PB_11 pins are
selected.
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.
Write the file name liquidcrystal.h.
You can see the liquidcrystal.h header file has been added.
Now copy and paste the following code.
In this 5th example, I am going to use an analog sensor with the STM32 board. I will read
its value and then print its value on the I2C supported 16×2 LCD. You can see, I am using
a potentiometer as the analog sensor. For this experiment you can use any analog
sensor like for example an LDR, etc. Anyway, I am going to continue with this
Potentiometer, let me explain the interfacing.
The 16×2 LCD wiring remains exactly the same as explained in the previous example.
The Potentiometer leftmost and rightmost legs are connected to the STM32 3.3V and
GND pins. And the middle leg of the Potentiometer is connected to the PB1. All the
Analog pins are 3.3V standard pins so make sure the voltage on the analog pins doesn’t
exceed 3.3 volts.
Connect the STM32 microcontroller board via ST-Link V2 to your Laptop/PC.
I am going to modify the previous project. Go to the I2CLcd.ioc. Click on the Analog and
select ADC1… and under the ADC1 Mode and configuration select IN9, which is the GPIO
PB1 pin on the STM32. Click on the Continuous Conversion and Enable it.
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.
Copy the following code and paste it in your main.c file.
You can see, the value changes as I rotate the knob of the Potentiometer. You can use
this same program with all types of analog sensors for reading and displaying the raw
data. Next, I am going to explain how to use an I2C supported Oled Display Module with
the STM32 controller board.
In this 6th example, I am going to show you, how to print text on this I2C supported
SSD1306 Oled display Module. In most of your upcoming projects, you will need this
Oled display module for printing text messages and Sensors values. In this example, I
will print the HELLO WORLD message on this Oled display module. Anyway, let’s take a
look at the connections.
The SSD1306 Oled display module VCC and GND pins are connected to the STM32 3.3V
and GND pins. The SCL and SDA pins are connected to the STM32 GPIO pins PB10 and
PB11. PB10 is the SCL and PB11 is the SDA.
Connect your STM32 via ST-Link V2 to your Laptop/PC.
Click on the I2C Speed Mode and select Fast Mode.
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.
Next, download the below SSD1306 oled display module libraries and add .c and .h files
exactly the same way as we did in the example #4. We will add .c files in the Src and .h
files in the Inc.
Download SSD1306 Oled display Libraries:
You can see all the required .h and .c files have been added. Now, we can close all these
files because these are already added in the Src and Inc folders. Click on the main.c file
and copy the following code and paste it in your main.c file.
You can see the Hello World message on the SSD1306 Oled display Module. Now, you
can add this Oled display module in all those projects where you need to print text or
sensor values. In the next example, we are going to measure the distance using the
Ultrasonic sensor and then we will print the measured distance on this Oled display
Module.
Example #7: STM32F103C8T6 Ultrasonic Sensor
In this 7th example, we are going to make a distance measurement system using the HC-
SR04 Ultrasonic sensor with the STM32 microcontroller board. We will measure the
distance in Centimeters and then we will print it on the SSD1306 Oled display Module.
Anyway, let’s go ahead and take a look at the connections.
The Oled display module wiring with the STM32 remains exactly the same. The
Ultrasonic Sensor VCC and GND pins are connected to the STM32 board 5V and GND
pins. The Echo and Trigger pins are connected to the STM32 GPIO Pins PB8 and PB9.
PB8 and PB9 are 5V tolerant pins. So, it doesn’t matter if I use 5V to power up the
Ultrasonic Sensor.
Connect your STM32 via the ST-Link V2 to your Laptop/PC as explained in example #1.
I am going to continue with the same Oled display module programming. Go to the
Oled.ioc, I am going to use the same SCL and SDA pins for the Oled display Module. Go to
the System Core and click on the RCC and Select Crystal/Ceramic Resonator.
Next, go to Timers and select TIM1. Select the Clock Source as Internal Clock. Next, click
on the PA8 pin and select TIM1_CH1.
Next, click on the PB9 and set it as OUTPUT. Then click on the PB8 and set it as INPUT.
Then go to Clock Configuration and change the HCLK to 72Mhz.
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.
I have already written this code for you, so simply copy and paste it in the main.c file.
We just built ourselves a distance measurement system using the HC-SR04 Ultrasonic
Sensor, an Oled display module, and the STM32 microcontroller board. So, that’s all for
now.