DanielRajChristeen/STM32-7-Segment-Display-HAL-Coding-Method
A beginner-friendly STM32 project that demonstrates how to interface a 7-segment display using STM32 HAL (Hardware Abstraction Layer) in STM32CubeIDE.
๐ข STM32 7-Segment Display Interfacing (HAL Coding Method)
A beginner-friendly STM32 project that demonstrates how to interface a 7-segment display using STM32 HAL (Hardware Abstraction Layer) in STM32CubeIDE.
This repository focuses on:
- Clean GPIO configuration using HAL
- Clear segment-to-pin mapping
- Logical digit control (0โ9)
- Industry-style embedded project structure
๐ฏ Project Objective
To display digits 0 โ 9 on a 7-segment display by controlling individual LED segments using STM32 GPIO pins via HAL APIs.
This project builds a strong foundation for:
- GPIO fundamentals
- Display interfacing concepts
- Transitioning from Arduino-style coding to professional STM32 workflows
๐ง What Youโll Learn
By completing this project, you will understand:
- How a 7-segment display works internally
- How STM32 GPIO pins control external hardware
- How HAL abstracts register-level complexity
- Pin planning and wiring best practices
- Embedded logic mapping (digit โ segment pattern)
๐งฐ Hardware & Software Requirements
Hardware
- STM32 Board (e.g., STM32F446RE Nucleo)
- Single-digit 7-segment display
- 220ฮฉโ330ฮฉ resistors (7 numbers)
- Breadboard & jumper wires
Software
- STM32CubeIDE
- STM32 HAL drivers
- ST-Link (onboard or external)
๐ Pinout & Hardware Connections
โ ๏ธ Display Type Assumption: COMMON CATHODE
(If you use a Common Anode display, logic must be inverted)
๐ Segment-to-STM32 Pin Mapping
| Segment | Description | STM32 GPIO | Nucleo Pin |
|---|---|---|---|
| a | Top | PA0 | D7 |
| b | Top-Right | PA1 | D8 |
| c | Bottom-Right | PA2 | D9 |
| d | Bottom | PA3 | D10 |
| e | Bottom-Left | PA4 | D11 |
| f | Top-Left | PA5 | D12 |
| g | Middle | PA6 | D13 |
Common Cathode Pin โ GND
๐ Why D7โD13?
- Digital-only pins (no analog confusion)
- Physically continuous on Nucleo boards
- Cleaner breadboard wiring
- Beginner-friendly visibility
This is intentional hardware design, not random pin picking.
๐ฅ Current-Limiting Resistors (Mandatory)
Each segment must use a resistor.
STM32 Pin โ 220ฮฉ โ Segment Pin
Skipping resistors risks:
- Burning LED segments
- Permanent STM32 GPIO damage
โ๏ธ STM32CubeMX GPIO Configuration
Configure PA0โPA6 as:
- Mode:
GPIO_Output - Output Type:
Push-Pull - Pull-up/Pull-down:
No Pull - Speed:
Low / Medium
HAL will generate the GPIO init code automatically.
๐งฎ Display Logic (Common Cathode)
| GPIO State | Segment |
|---|---|
| HIGH (1) | ON |
| LOW (0) | OFF |
๐งโ๐ป Core Code Concept
Each number (0โ9) is represented by a bit pattern, deciding which segments turn ON.
Example:
- Digit 0 โ a b c d e f ON
- Digit 1 โ b c ON
- Digit 8 โ all segments ON
The logic is implemented cleanly using HAL GPIO writes.
๐ Program Flow
- Initialize HAL
- Configure system clock
- Initialize GPIO pins
- Select digit pattern
- Set GPIO pins accordingly
- Delay
- Repeat
๐งช Common Issues & Fixes
| Issue | Likely Cause |
|---|---|
| Nothing lights | COM pin not grounded |
| Wrong number | Segment wiring mismatch |
| Inverted logic | Using Common Anode |
| Dim display | Missing resistors |
๐ฆ Project Structure
Core/
โโโ Src/
โ โโโ main.c
โโโ Inc/
โโโ main.h
Simple. Clean. Beginner-friendly.
๐งญ Next Enhancements (Ideas)
- Button-controlled digit increment/decrement
- Multi-digit multiplexing
- Timer-based display refresh
- Register-level version (HAL โ Bare-metal)
๐ License
This project is licensed under the MIT License.
You are free to use, modify, and distribute it.