The STM32F103 Blue Pill tutorial UART GPIO guide you’ve been looking for — the Blue Pill is arguably the most capable sub-₹200 development board available in India, packing a 72 MHz Cortex-M3 with rich peripherals. This tutorial covers GPIO control, UART communication, and timer configuration with working code.
Table of Contents
- Setting Up Blue Pill Development
- GPIO Configuration and Control
- UART Serial Communication
- Timers and PWM
- I2C Peripheral Interface
- Blue Pill Tips for Indian Makers
- Frequently Asked Questions
Setting Up Blue Pill Development
The STM32F103C8T6 Blue Pill board uses an ST-Link V2 programmer for flashing. In India, ST-Link clones are available for ₹150–300. Install STM32CubeIDE (free from ST) or use Arduino IDE with the STM32duino board package. For professional work, STM32CubeIDE with HAL libraries is recommended.
Key specs: 72 MHz Cortex-M3, 64 KB flash, 20 KB SRAM, 37 GPIO pins, USB FS, CAN, 3× USART, 2× I2C, 2× SPI, 7× timers, 10× 12-bit ADC channels. Operating voltage 3.3V — use a logic level converter when interfacing with 5V Arduino modules.
GPIO Configuration and Control
STM32 GPIO modes differ from Arduino’s simple pinMode. Each pin can be configured as Input (floating/pull-up/pull-down), Output (push-pull/open-drain), Alternate Function, or Analog. Speed settings (2/10/50 MHz) control slew rate.
// STM32 HAL GPIO blink example
#include "stm32f1xx_hal.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1) {
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); // Built-in LED
HAL_Delay(500);
}
}
static void MX_GPIO_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
UART Serial Communication
STM32F103 has three USART peripherals. USART1 is on PA9 (TX) and PA10 (RX). Configure UART with HAL for reliable serial communication with sensors, GSM modules (SIM800L, very common in India), and GPS modules.
// UART transmit with HAL
UART_HandleTypeDef huart1;
void MX_USART1_UART_Init(void) {
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
HAL_UART_Init(&huart1);
}
// Send a string
char msg[] = "Hello from STM32!
";
HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
Timers and PWM
STM32F103 has seven timers. Timer 2 on PA0 can generate PWM for motor speed control, LED dimming, or servo control. Configure the prescaler and auto-reload register for desired frequency.
// PWM on TIM2 Channel 1 (PA0) at 50Hz for servo
TIM_HandleTypeDef htim2;
TIM_OC_InitTypeDef sConfigOC = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 71; // 72MHz / 72 = 1MHz timer clock
htim2.Init.Period = 19999; // 1MHz / 20000 = 50Hz
HAL_TIM_PWM_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 1500; // 1.5ms = centre position
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
I2C Peripheral Interface
STM32F103 has two I2C peripherals. I2C1 uses PB6 (SCL) and PB7 (SDA). Common I2C devices available in India include OLED displays (SSD1306), temperature sensors (BMP280, DS18B20 via 1-wire), and the MPU6050 IMU.
// I2C read from MPU6050
I2C_HandleTypeDef hi2c1;
uint8_t data[6];
uint8_t reg = 0x3B; // ACCEL_XOUT_H
// Request 6 bytes starting at register 0x3B
HAL_I2C_Master_Transmit(&hi2c1, 0x68<<1, ®, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Receive(&hi2c1, 0x68<<1, data, 6, HAL_MAX_DELAY);
int16_t ax = (data[0]<<8) | data[1]; // Raw accelerometer X
Blue Pill Tips for Indian Makers
- Blue Pill boards from Robu/Amazon vary in quality — check if the USB resistor (R10) is 1.5kΩ (correct) or 10kΩ (wrong) for USB to work
- The onboard 3.3V LDO can only supply ~100 mA — power external sensors from a separate supply
- Always use a 3.3V logic level converter when connecting to 5V modules like HC-05 Bluetooth
- STM32CubeMX generates peripheral init code automatically — highly recommended for beginners
- Back up your BOOT0 jumper setting — accidentally setting it to 1 boots into bootloader mode
Frequently Asked Questions
Can Blue Pill be programmed with Arduino IDE?
Yes. Install the STM32duino board package (by STMicroelectronics) in Arduino IDE. Select “Generic STM32F1 series” board and upload via ST-Link or serial bootloader.
What is the maximum GPIO current on Blue Pill?
Each GPIO pin can source/sink up to 25 mA, with a total chip limit of 150 mA. Always use current-limiting resistors for LEDs and transistors for relay/motor control.
Is Blue Pill 5V tolerant?
Some pins are 5V tolerant (marked “FT” in the datasheet), but the supply voltage is 3.3V. Check the datasheet carefully before connecting 5V logic directly.
Which is better for motor control — Blue Pill or Arduino?
Blue Pill with STM32’s hardware timers is significantly better for motor control (multiple PWM channels, encoder interface, dead-time insertion for complementary outputs). Arduino’s timers are simpler but less capable.
Add comment