Zbotic Logo Zbotic Logo
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

  • Shop
  • About Us
  • Contact Us
  • Reseller
  • Blogs
020 69134444
1800 209 0998
[email protected]
Help Desk
Facebook Twitter Instagram Linkedin YouTube
Zbotic Logo Zbotic Logo
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

All departments
  • 3D Print Service
  • 3D Printer
  • Batteries & Chargers
  • Development Boards
  • Drone Parts
  • EBike parts
  • Sensor Modules
  • Electronic Components
  • Electronic Modules
  • IoT and Wireless
  • Mechanical Parts and Workbench Tools
  • Motors & Drivers & Pumps & Actuators
  • DIY and Robot Kits
  • Show more
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
Return to previous page
Home Development Boards & SBCs

STM32 Getting Started Guide: CubeMX and HAL Library Tutorial

STM32 Getting Started Guide: CubeMX and HAL Library Tutorial

March 11, 2026 /Posted byJayesh Jain / 0

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.

View Development Boards

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

  1. File > New STM32 Project
  2. Select your MCU or board (e.g. NUCLEO-F401RE)
  3. Name project, language: C
  4. In Pinout view: click a pin, select function (e.g. GPIO_Output for LED)
  5. Clock Configuration: set HCLK to target frequency (84 MHz for F401)
  6. Project Manager tab: Code Generator settings – check ‘Copy only necessary library files’
  7. 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.

View SBCs

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.

Shop Development Boards

Tags: CubeMX project setup, STM32 CubeMX tutorial, STM32 getting started, STM32 HAL library, STM32 India beginner
Share Post
  • Facebook
  • Linkedin
  • Whatsapp
Hidden Camera Detector: Build ...
blog hidden camera detector build with rf and lens scanner 599768
blog smart agriculture project for engineering students india 599777
Smart Agriculture Project for ...

Related posts

Svg%3E
Read more

Battery Charger Module TP4056: LiPo and 18650 Charging Guide

April 1, 2026 0
The TP4056 battery charger module is one of the most essential components for any battery-powered electronics project. Costing under ₹30,... Continue reading
Svg%3E
Read more

Buck Converter vs Boost Converter: Voltage Regulation Guide

April 1, 2026 0
Understanding buck converters vs boost converters is essential for every electronics project involving power management. Whether you are stepping down... Continue reading
Svg%3E
Read more

Google Coral TPU: Accelerating AI Projects on Raspberry Pi

April 1, 2026 0
The Google Coral TPU (Tensor Processing Unit) transforms a Raspberry Pi from a sluggish AI hobbyist tool into a real-time... Continue reading
Svg%3E
Read more

NVIDIA Jetson Nano Projects India: Getting Started Guide

April 1, 2026 0
The NVIDIA Jetson Nano is the most accessible GPU-accelerated AI computer for developers in India. With 128 CUDA cores, a... Continue reading
Svg%3E
Read more

ATtiny85 Projects: Tiny Microcontroller for Space-Constrained Builds

April 1, 2026 0
The ATtiny85 is the Swiss Army knife of tiny microcontrollers — just 8 pins, 8 KB of flash, and a... Continue reading

Add comment Cancel reply

Your email address will not be published. Required fields are marked

Facebook Twitter Instagram Pinterest Linkedin Youtube

Get the latest deals and more.

Download on Google Play Download on the App Store

Call us: 020 69134444 / 1800 209 0998

Monday - Saturday 09:30 AM - 06:00 PM
For Technical Supports Email: [email protected]
For Sales / Enquiries Email: [email protected]

  • My Account

    • Cart

    • Wishlist

    • Checkout

    • My Orders

    • Track Order

    • My Account

  • Information

    • FAQs

    • Blogs

    • Career

    • About Us

    • Contact Us

    • Payment Options

  • Policies

    • Privacy Policy

    • Terms & Conditions

    • GST Input Tax Credit

    • Shipping Return Policy

    • E-Waste Collection Points

    • Our Sitemap

© Zbotic.in is registered trademark of Moxie Supply Pvt Ltd – All Rights Reserved
Login
Use Phone Number
Use Email Address
Not a member yet? Register Now
Reset Password
Use Phone Number
Use Email Address
Register
Already a member? Login Now