Some links in this article are affiliate links. If you buy through them, LabprojectsBD may earn a small commission at no extra cost to you.
In this article, you will learn how to drive a 4-digit 7-segment display with an STM32 microcontroller. We cover how the display works, how to wire it safely and how to program it in STM32CubeIDE. It is a beginner-friendly project with a working example.
⚠️Disclaimer:
Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.
All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.
If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.
Need help or have questions? Leave a comment below — the author is always happy to assist!
Table of Contents
About 7-Segment display
A seven-segment display is a simple display for showing numbers. It is much easier to drive than a dot-matrix or graphic display.
You will find 7-segment displays in digital clocks, meters, calculators and many other devices that show numbers.
The display has seven LED bars arranged in a figure “8”: two vertical bars on each side and three horizontal bars at the top, middle and bottom. By lighting different combinations of bars, it can show any digit from 0 to 9.
The segments are named a to g. Many displays also have a decimal point, called dp. Since there are eight LEDs, one byte can describe the whole digit. For example, in the common “gfedcba” encoding, the value 0x06 lights segments b and c, which shows the digit “1”.
Types of Seven-Segment Displays
There are two types of 7-segment display: common anode and common cathode.
Common Anode Display
In a common anode display, the anodes of all eight LEDs are joined and connected to the positive supply. To light a segment, the microcontroller pulls its pin low. To turn it off, the pin goes high. In other words, the logic is inverted.
Common Cathode Display
In a common cathode display, the cathodes of all LEDs are joined and connected to ground. To light a segment, the microcontroller drives its pin high, and a low level turns it off. In this project, we use a common cathode display.
Now, how multiplexing works
A 4-digit display would need 32 segment pins if every LED had its own wire. Instead, multi-digit displays use multiplexing. The same segment (for example “a”) of every digit is wired together, so there are only 8 segment lines. In addition, each digit has its own common pin.
The microcontroller then lights one digit at a time. It sets the segment pattern for digit 1 and enables only that digit. Next, it moves to digit 2, then 3 and 4, and repeats. As a result, a 4-digit display needs only 8 + 4 = 12 pins.
Only one digit is on at any moment. However, the controller switches between them so fast (over 100 times per second) that your eye sees all four digits lit at once.
Interfacing with STM32
For this project, we use the STM32 “Blue Pill”, a small and cheap board with an STM32F103C8 microcontroller. It is a good board for students and hobbyists. Below, we connect a 4-digit common-cathode display and program it with STM32CubeIDE.
Prerequisites
You will need the following items:
STM32CubeIDE is ST’s free development tool. It includes CubeMX for pin and clock set-up, a code editor, a compiler and a debugger. You will also need an ST-LINK programmer, since the Blue Pill has no built-in one.
Hardware Setup
The 4-digit display has 12 pins: 8 segment pins and 4 digit pins (the common cathodes). Each segment pin connects to the STM32 through a 330 Ω resistor:
- Segment A -> PB3
- Segment B -> PB4
- Segment C -> PB5
- Segment D -> PB6
- Segment E -> PB7
- Segment F -> PB8
- Segment G -> PB9
- Segment DP -> PB10
Never connect the digit pins directly to the STM32. When a digit shows “8.”, all eight segments drain into one pin, which is far more than the 25 mA a GPIO pin can sink. Instead, switch each digit with an NPN transistor (BC547 or 2N3904): collector to the digit pin, emitter to ground and base to the STM32 through a 1 kΩ resistor:
- Digit 1 → transistor base ← PA0
- Digit 2 → transistor base ← PA1
- Digit 3 → transistor base ← PA2
- Digit 4 → transistor base ← PA3
Software Setup
Next, create the project in STM32CubeIDE, configure the pins and add the display code.
- Create a new project:
- Open STM32CubeIDE.
- Click on File -> New -> STM32 Project.
- Choose the microcontroller that you are using (in our case, STM32F103C8).
- Enter a name for the project and choose a location to save it.
- Click on Finish.
- Configure the pins in CubeMX:
- Open the Pinout & Configuration tab.
- Under System Core > SYS, set Debug to “Serial Wire”. This frees PB3 and PB4, which are JTAG pins by default.
- Configure the pins as follows:
- Set PA0–PA3 as GPIO_Output (push-pull).
- Set PB3–PB10 as GPIO_Output (push-pull).
- Save the file, and let CubeMX generate the code.
- Add the display code:
- Open main.c in the Core/Src folder.
- Add the following code. Keep the CubeMX-generated
SystemClock_Config()andMX_GPIO_Init()functions as they are:
/* main.c (excerpt) for an STM32F103C8 "Blue Pill" project made with STM32CubeMX.
Wiring:
Segments a, b, c, d, e, f, g, dp -> PB3 ... PB10, each through a 330 Ω resistor
Digit 1..4 common cathodes -> collectors of four NPN transistors (BC547, 2N3904);
emitters to GND, bases to PA0 ... PA3 through 1 kΩ resistors
Logic: segment pin HIGH = segment on, digit pin HIGH = digit on.
CubeMX: PA0-PA3 and PB3-PB10 = GPIO_Output, push-pull.
PB3 and PB4 are JTAG pins: set SYS > Debug = "Serial Wire" to free them. */
#include "main.h"
/* Segment patterns for 0-9, bit 0 = a ... bit 6 = g (gfedcba encoding) */
static const uint8_t SEG_CODE[10] = {
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F
};
static const uint16_t SEG_PIN[8] = {
GPIO_PIN_3, GPIO_PIN_4, GPIO_PIN_5, GPIO_PIN_6,
GPIO_PIN_7, GPIO_PIN_8, GPIO_PIN_9, GPIO_PIN_10
};
static const uint16_t DIGIT_PIN[4] = { GPIO_PIN_0, GPIO_PIN_1, GPIO_PIN_2, GPIO_PIN_3 };
#define SEG_MASK (GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | \
GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10)
#define DIGIT_MASK (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3)
/* Light one digit position (0-3) with a segment pattern */
static void show_digit(int pos, uint8_t pattern)
{
uint16_t on = 0;
HAL_GPIO_WritePin(GPIOA, DIGIT_MASK, GPIO_PIN_RESET); /* blank all digits first */
HAL_GPIO_WritePin(GPIOB, SEG_MASK, GPIO_PIN_RESET);
for (int s = 0; s < 8; s++)
if (pattern & (1u << s)) on |= SEG_PIN[s];
HAL_GPIO_WritePin(GPIOB, on, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, DIGIT_PIN[pos], GPIO_PIN_SET); /* turn this digit on */
}
/* Show a 4-digit number for 'ms' milliseconds by multiplexing the digits */
static void display_number(uint16_t number, uint32_t ms)
{
uint8_t pattern[4] = {
SEG_CODE[(number / 1000) % 10],
SEG_CODE[(number / 100) % 10],
SEG_CODE[(number / 10) % 10],
SEG_CODE[number % 10]
};
uint32_t start = HAL_GetTick();
while ((HAL_GetTick() - start) < ms) {
for (int d = 0; d < 4; d++) {
show_digit(d, pattern[d]);
HAL_Delay(1); /* about 2 ms per digit: the whole display refreshes at ~125 Hz */
}
}
HAL_GPIO_WritePin(GPIOA, DIGIT_MASK, GPIO_PIN_RESET);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1) {
display_number(1234, 1000);
display_number(5678, 1000);
}
}
- Build and run:
- Connect the ST-LINK to the Blue Pill (SWDIO, SWCLK, GND and 3.3 V) and to your computer.
- Click Build to compile the project.
- Click Run to flash the board.
- The display now alternates between 1234 and 5678 every second. The code lights each digit for about 2 ms in turn, so the whole display refreshes about 125 times per second and looks steady.
Conclusion
In this article, you learned how a 7-segment display works, how multiplexing saves pins and how to drive a 4-digit display safely from an STM32 Blue Pill. From here, you can show sensor readings, build a clock or move the refresh into a timer interrupt so the main loop is free for other work.
Read more on:
- GLCD 128×64 ST7920 interfacing with STM32
- How to interface a 16×2 LCD with an STM32
- How to interface ESP8266 with STM32
- How to interface DHT11 with STM32
- STM32 Bluetooth Low Energy & its application
- How to interface the EM18 RFID module with STM32
Liked this article? Subscribe to our newsletter:
or,
Visit LabProjectsBD.com for more inspiring projects and tutorials.
Thank you!
Have a project in mind? LabProjectsBD designs power electronics, embedded firmware, IoT devices and production-ready PCBs for clients. Message us on WhatsApp, use the contact form, or see our services and portfolio.




0 Comments