Quick start and application development guide of STM32F103 development board
Getting Started with the STM32F103 Development Board
The STM32F103 development board, based on the ARM Cortex-M3 microcontroller, has become one of the most popular choices for embedded system development. Whether you are a beginner or a seasoned developer, this Power ful yet cost-effective platform provides all the necessary tools to create innovative applications. In this guide, we’ll explore how to quickly get started with the STM32F103 board, including setting up the development environment and understanding its core components.
Why Choose STM32F103?
Before jumping into the setup, it’s worth understanding why the STM32F103 is such a widely used board. The STM32F103 microcontroller offers:
ARM Cortex-M3 Processor: A 32-bit processor with a great balance between performance and power efficiency.
Wide Range of Peripherals: Including UART, SPI, I2C, ADCs, and GPIOs for easy integration with external devices.
Low Power Consumption: Ideal for battery-powered applications.
Robust Ecosystem: Extensive software libraries, example projects, and active community support.
Step 1: Preparing Your Development Environment
The first step in STM32F103 development is to set up the necessary software and hardware environment. Here’s what you need:
Install an IDE: The STM32F103 can be programmed using several Integrated Development Environments (IDEs). The most popular choice is STM32CubeIDE, which is an all-in-one development tool that integrates code editing, debugging, and project management. You can download it from STMicroelectronics’ official website.
Install the STM32CubeMX Software: This tool helps you configure the microcontroller’s peripherals and generate initialization code. It simplifies the process of configuring clocks, pins, and peripherals for your project.
Connect Your Development Board: The STM32F103 board typically connects via a USB to Serial adapter or ST-Link V2 programmer/debugger. This is necessary for flashing your code onto the board and debugging in real-time.
Install Required Drivers : Make sure to install drivers for the ST-Link or USB-to-serial interface to establish a proper Communication link between your development environment and the STM32F103 board.
Step 2: Exploring the STM32F103 Board
The STM32F103 development board has a wide variety of pins and peripherals that can be used for a range of applications. Here are the key components you should familiarize yourself with:
Power Supply: The board typically uses a 5V power supply (via USB or external source).
Microcontroller: The STM32F103 microcontroller is at the heart of the board, featuring 32KB to 1MB flash Memory and 20KB SRAM.
Input/Output Pins: These are general-purpose I/O pins that can be configured as inputs, outputs, or analog channels.
Peripherals: The STM32F103 supports multiple peripherals, including:
Serial Communication: UART, SPI, I2C for connecting to Sensor s, displays, or other microcontrollers.
Analog-to-Digital Converter (ADC): Useful for reading sensor data.
Timers and PWM: For precise time management and controlling devices like motors or LED s.
Step 3: Writing Your First Program
Now that you’ve set up your environment and explored the hardware, it’s time to write your first application. We’ll start with a simple LED blink program, one of the most common examples for beginners.
Open STM32CubeIDE and create a new STM32 project.
Configure the GPIO Pins: Using STM32CubeMX, configure one of the pins as an output. Typically, onboard LEDs are connected to a GPIO pin such as PA5 on the STM32F103 board.
Generate Initialization Code: CubeMX will generate the necessary code for setting up the GPIO pin as an output.
Write the Main Application: In STM32CubeIDE, open the generated project and write the application code. The code for toggling the LED might look like this:
#include "main.h"
int main(void)
{
HAL_Init();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
while (1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(500); // Delay for 500ms
}
}
Build and Flash the Code: After writing the code, click on the Build button in STM32CubeIDE to compile the program, and then flash it to your board using the ST-Link or USB-to-serial adapter.
Test the Program: If everything is set up correctly, the LED on your board should start blinking at a 500ms interval, indicating that the program is working.
Step 4: Debugging and Troubleshooting
Debugging is an essential part of embedded development. STM32CubeIDE comes with integrated debugging tools that allow you to set breakpoints, step through code, and monitor variables in real-time. If your LED isn’t blinking, check the following:
Correct Pin Configuration: Make sure the correct pin is configured as an output.
Hardware Connections: Ensure the board is powered, and the ST-Link or USB-to-serial connection is properly established.
Expanding Your Application Development with STM32F103
Once you have a simple LED blink program up and running, you can explore more advanced features and applications with the STM32F103 development board. In this part of the guide, we’ll delve into handling external peripherals, working with communication protocols, and leveraging more advanced functionalities of the microcontroller.
Step 5: Interfacing with Sensors
One of the key advantages of STM32F103 is its ability to interface with a wide variety of sensors, including temperature, humidity, and motion sensors. Let’s explore how to interface an analog temperature sensor (like the LM35) with the STM32F103 board.
Connect the Sensor: The LM35 temperature sensor has an analog output that varies with temperature. Connect the sensor’s Vcc and GND pins to the 3.3V and GND of the STM32F103 board, respectively. The analog output pin should be connected to one of the ADC channels of the microcontroller (e.g., PA0).
Configure the ADC: Using STM32CubeMX, configure the pin as an analog input and enable the ADC peripheral. CubeMX will generate the necessary initialization code for the ADC.
Write the Application Code: In the main application, you can use the ADC to read the analog value from the sensor and convert it to a temperature value. Here’s a sample code snippet:
#include "main.h"
int main(void)
{
HAL_Init();
__HAL_RCC_ADC1_CLK_ENABLE();
ADC_ChannelConfTypeDef sConfig = {0};
ADC_HandleTypeDef hadc1;
// Configure ADC
hadc1.Instance = ADC1;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
HAL_ADC_Init(&hadc1);
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
while (1)
{
HAL_ADC_Start(&hadc1);
if (HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK)
{
uint32_t adc_value = HAL_ADC_GetValue(&hadc1);
float temperature = (adc_value * 3.3 / 4095) * 100; // Convert to temperature in Celsius
HAL_Delay(1000);
}
}
}
This code reads the analog value from the LM35 sensor, converts it to a temperature value, and continues to monitor it in a loop.
Step 6: Using Communication Protocols (UART, SPI, I2C)
STM32F103 also supports various communication protocols, making it ideal for building complex systems with multiple devices. Let’s focus on UART communication, a common protocol for serial communication.
Setup UART: In STM32CubeMX, configure the UART peripheral and select the appropriate TX and RX pins (e.g., PA9 and PA10).
Transmit Data: You can use UART to send and receive data from other devices, such as a PC or another microcontroller. Here’s an example of how to transmit a string via UART:
HAL_UART_Transmit(&huart1, (uint8_t*)"Hello, STM32!", 14, 1000);
Receive Data: Similarly, you can receive data via UART by using functions like HAL_UART_Receive to read bytes from the serial port.
Step 7: Advanced Features and Optimization
For more advanced projects, you can make use of additional STM32F103 features like DMA (Direct Memory Access ) for faster data transfer, real-time operating systems (RTOS) for multitasking, or low-power modes for energy-efficient applications. With STM32CubeMX and CubeIDE, you can easily configure these features and experiment with them.
Conclusion
The STM32F103 development board provides a robust platform for both beginner and professional embedded developers. From simple LED blink programs to complex sensor interfacing and communication protocols, the STM32F103 offers limitless possibilities for creating real-world applications. By following this quick start guide, you can unlock the full potential of the STM32F103 and start developing your own innovative projects today.
If you are looking for more information on commonly used Electronic Components Models or about Electronic Components Product Catalog datasheets, compile all purchasing and CAD information into one place.