DanielRajChristeen/STM32-LED-BUTTON-CTRL-Register-Coding-Method
A bare-metal STM32 project that demonstrates LED blink delay control using two push buttons, implemented using direct register access (no HAL GPIO APIs for logic control).
STM32 LED & Button Control โ Register Coding Method
A bare-metal STM32 project that demonstrates LED blink delay control using two push buttons, implemented using direct register access (no HAL GPIO APIs for logic control).
This project focuses on real-world embedded behavior: timing control, input handling, and boundary protection โ all at the register level.
๐ฏ Project Objective
The goal of this project is to dynamically control LED blink speed using two buttons:
- LED on Pin PA5 (Arduino D13 / Pin 13)
- Button 1 on Pin PA6 (Arduino D12 / Pin 12) โ Increase delay
- Button 2 on Pin PA7 (Arduino D11 / Pin 11) โ Decrease delay
Functional Behavior
| Action | Result |
|---|---|
| Button-1 pressed | Delay increases by 100 ms |
| Button-2 pressed | Delay decreases by 100 ms |
| Delay range | 0 ms to 1000 ms |
| LED behavior | Blinks continuously using current delay |
This mimics a human-controlled timing system, a pattern widely used in industrial HMI logic.
๐ง Why Register-Level Coding?
This project intentionally avoids HAL GPIO logic to help you:
- Understand how GPIO really works
- Gain full control over timing & pin state
- Eliminate abstraction overhead
- Debug hardware with absolute clarity
Everything is driven by:
RCC->AHB1ENRGPIOx->MODERGPIOx->IDRGPIOx->ODR- SysTick timing
No magic. No shortcuts.
๐งฐ Hardware Mapping
| Component | STM32 Pin | Arduino Label |
|---|---|---|
| LED | PA5 | D13 |
| Button โ Increment | PA6 | D12 |
| Button โ Decrement | PA7 | D11 |
Buttons should use pull-down or pull-up logic (external or internal).
๐ฆ Repository Structure
STM32-LED-BUTTON-CTRL-Register-Coding-Method/
โโโ Core/
โ โโโ Src/
โ โ โโโ main.c # Register-level LED & button logic
โ โโโ Inc/
โ โโโ main.h
โโโ Drivers/
โ โโโ CMSIS/ # Device headers & startup code
โโโ startup_stm32f446xx.s
โโโ STM32F446RETX_FLASH.ld
โโโ .project
โโโ .cproject
โโโ README.md
All logic lives inside main.c.
โ๏ธ Core Logic Explained (High Level)
1๏ธโฃ Clock Enable
Enable GPIOA and SysTick clocks using RCC.
2๏ธโฃ GPIO Configuration
- PA5 โ Output (LED)
- PA6, PA7 โ Input (Buttons)
Configured manually via MODER.
3๏ธโฃ Delay Control Logic
- Delay variable starts at 500 ms (default)
- Button presses update delay dynamically
- Delay is clamped between
0โ1000 ms
if (button_inc_pressed) {
delay += 100;
}
if (button_dec_pressed) {
delay -= 100;
}Boundary protection ensures:
if (delay > 1000) delay = 1000;
if (delay < 0) delay = 0;4๏ธโฃ LED Blink
LED toggles continuously using the current delay value:
GPIOA->ODR ^= (1 << 5);
delay_ms(current_delay);This creates real-time visual feedback based on button input.
๐ How to Run
1๏ธโฃ Clone Repo
git clone https://github.com/DanielRajChristeen/STM32-LED-BUTTON-CTRL-Register-Coding-Method.git2๏ธโฃ Open in STM32CubeIDE
CubeIDE is used only as compiler and debugger
- Import existing project
- Build once
3๏ธโฃ Flash & Test
- Connect board via ST-LINK
- Run / Debug
- Press buttons and watch LED speed change live
๐ What Youโll Learn
By completing this project, youโll understand:
- GPIO input & output at register level
- Button-driven control systems
- Timing control using SysTick
- Delay clamping & boundary conditions
- Real-time firmware behavior
This is core embedded thinking, not just coding.
๐งช Typical Use-Cases
- Beginner-friendly embedded timing demo
- Register-level GPIO practice
- Interview preparation project
- PCB button/LED validation
- Transition step from HAL โ Bare-metal
๐ License โ MIT
MIT License
Copyright (c) 2025 Daniel Raj Christeen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.