STM32 microcontrollers are the professional choice for embedded systems engineers across India’s aerospace, automotive, industrial, and IoT sectors. Getting started with STM32 CubeMX and HAL Library has become much easier with ST’s free tools. This tutorial covers STM32CubeMX project generation, understanding the HAL (Hardware Abstraction Layer) API, building your first GPIO and UART projects, and flashing via ST-Link – everything an Indian engineering student or professional needs to begin STM32 development.
Table of Contents
- Why Choose STM32
- Tools Setup: CubeMX and STM32CubeIDE
- Creating a Project with CubeMX
- HAL GPIO: LED Blink
- HAL UART: Serial Communication
- HAL Timer: PWM Output
- Debugging with ST-Link
- FAQ
Why Choose STM32
STM32 microcontrollers (ARM Cortex-M based) dominate professional embedded development for several reasons:
- Ecosystem: Hundreds of STM32 variants covering Cortex-M0 to M7, from Rs 60 (STM32F030) to Rs 2,000 (H7 series)
- Free tools: STM32CubeIDE (Eclipse-based IDE + HAL libraries) is completely free
- India industry demand: Required for ISRO, HAL, BEL, Tata Electronics, and most embedded systems roles
- Low-cost dev boards: Blue Pill (STM32F103) available in India for Rs 150-200. Nucleo boards: Rs 1,500-3,000
- FreeRTOS support: STM32CubeMX integrates FreeRTOS configuration out of the box
STM32 Development Boards at Zbotic
Browse STM32 Nucleo, Discovery, and Blue Pill development boards. ST Nucleo-64 boards include an on-board ST-Link debugger – the easiest way to start STM32 development without extra hardware. Available with STM32F401, F411, L476, and G0 series microcontrollers.
Tools Setup: CubeMX and STM32CubeIDE
Download STM32CubeIDE from st.com/stm32cubeide (free registration required). STM32CubeIDE bundles CubeMX, GCC toolchain, HAL libraries, and ST-Link drivers in one installer. On Windows 11: extract to C:STSTM32CubeIDE_1.x.x. On Ubuntu 22.04 (common on Indian developer laptops):
chmod +x st-stm32cubeide_*.sh
sudo ./st-stm32cubeide_*.sh
# Install ST-Link udev rules
sudo cp /opt/st/stm32cubeide_*/plugins/com.st.stm32cube.ide.mcu.externaltools.stlink-gdb-server.*/tools/bin/udev/rules.d/*.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules
Creating a Project with CubeMX
- File > New STM32 Project
- Select your MCU or board (e.g. NUCLEO-F401RE)
- Name project, language: C
- In Pinout view: click a pin, select function (e.g. GPIO_Output for LED)
- Clock Configuration: set HCLK to target frequency (84 MHz for F401)
- Project Manager tab: Code Generator settings – check ‘Copy only necessary library files’
- Generate Code (Alt+K)
CubeMX generates initialisation code for all enabled peripherals. Your user code goes in the marked sections between /* USER CODE BEGIN */ and /* USER CODE END */ – these are preserved when you regenerate code after changing configuration.
HAL GPIO: LED Blink
/* main.c - Inside while(1) loop */
/* USER CODE BEGIN WHILE */
while (1) {
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); // Toggle onboard LED
HAL_Delay(500); // 500ms delay
/* USER CODE END WHILE */
}
/* Key HAL GPIO Functions:
* HAL_GPIO_WritePin(GPIOx, Pin, GPIO_PIN_SET/RESET)
* HAL_GPIO_ReadPin(GPIOx, Pin) - returns GPIO_PIN_SET or GPIO_PIN_RESET
* HAL_GPIO_TogglePin(GPIOx, Pin)
* HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) - override for interrupt handling
*/
HAL UART: Serial Communication
Enable USART2 in CubeMX (Connectivity > USART2 > Asynchronous). Set baud rate to 115200. Connect to PC via USB to serial adapter (CP2102, Rs 80-150 in India).
/* Polling UART transmit */
char msg[] = "Hello from STM32!rn";
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
/* UART receive single byte */
uint8_t rxByte;
HAL_UART_Receive(&huart2, &rxByte, 1, 100);
/* Printf via UART (add to main.c, retarget fwrite) */
#ifdef __GNUC__
int __io_putchar(int ch) {
HAL_UART_Transmit(&huart2, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
return ch;
}
#endif
// Now you can use: printf("ADC value: %drn", adcValue);
HAL Timer: PWM Output
In CubeMX: Timers > TIM3 > PWM Generation CH1. Set Prescaler and Period to get desired frequency. For 1kHz PWM with 84MHz HCLK: Prescaler=83, Period=999 gives 1kHz at 84/84/1000 = 1kHz.
/* Start PWM */
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
/* Set duty cycle (0-1000 for Period=999) */
void setPWMDuty(uint32_t duty) {
TIM3->CCR1 = duty; // Direct register write for efficiency
// Or: __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, duty);
}
/* Servo control example (50Hz PWM): Period=19999, Prescaler=83 */
/* 1ms pulse = 1000, 1.5ms = 1500, 2ms = 2000 */
void setServoAngle(uint8_t angle) {
uint32_t pulse = 1000 + (angle * 1000 / 180);
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, pulse);
}
Raspberry Pi 4 Model B – For Linux Development Environment
Raspberry Pi 4 makes an excellent low-cost Linux development workstation for STM32 projects. Run STM32CubeIDE under Linux, compile ARM binaries, and use a USB ST-Link dongle for flashing. A complete STM32 development setup for under Rs 10,000 total.
Debugging with ST-Link
Nucleo boards have built-in ST-Link – just connect USB. For Blue Pill / custom boards, use a standalone ST-Link V2 dongle (Rs 200-400 from Robu.in, Lamington Road).
In STM32CubeIDE: Run > Debug As > STM32 Cortex-M C/C++ Application. Set breakpoints by clicking line numbers. Use the Expressions view to watch variables in real time.
Useful debug features:
- SWV (Serial Wire Viewer): printf output without UART, via SWO pin (MCU speeds up to 72MHz)
- Live Expressions: Watch variable values update in real time without stopping execution
- Fault analysis: CubeIDE’s fault analyser shows which fault occurred (HardFault, MemFault, BusFault)
FAQ
Which STM32 board should a beginner buy in India?
Start with STM32 Nucleo-64 (F401RE or G071RB). Built-in ST-Link means no separate debugger purchase. Available from Zbotic, Robu.in, and local electronics markets for Rs 1,500-2,500. Avoid cheap Blue Pill clones initially – they often have issues with USB and oscillator quality.
Is HAL slower than direct register programming?
HAL adds 5-30% overhead compared to direct register access for most operations. For tight timing requirements (above 1MHz operations), bypass HAL and write to registers directly. For most projects, HAL speed is more than sufficient.
Can I use Arduino IDE with STM32?
Yes, via the STM32duino core. Adds STM32 support to Arduino IDE. However, HAL + CubeIDE is the professional approach used in industry. Learn both: Arduino for rapid prototyping, CubeIDE for production work.
What STM32 certification courses are available in India?
ST Microelectronics runs authorised training through AECL and training partners in Bangalore, Hyderabad, and Pune. Online: NPTEL’s ‘Embedded Systems’ courses cover ARM Cortex-M. STM32 is heavily featured in GATE ECE and embedded systems job interviews in India.
Add comment