A seven segment display is a simple and effective way to show numeric information in embedded projects and is widely used in counters, clocks, meters and simple user displays. The component consists of seven LED segments arranged like the figure eight and an optional decimal point which together can form digits 0 through 9 and some letters. Two major types exist: common anode and common cathode; in common cathode displays all cathodes are tied to ground and segments light when their anode is driven high. In microcontroller systems we map each segment to a port pin and write the correct bit pattern to illuminate the desired segments. This tutorial uses an ATmega16 and a common cathode display wired to PORTB pins PB0..PB6 so students can copy and test on real hardware. The post explains electrical connections, provides a segment code table for digits 0–9, includes C source to cycle digits, and presents an animated simulation that visualises the ATmega16 pins and segment outputs together. Using PORTB for segment outputs keeps the wiring simple and allows easy use of lookup tables to convert numbers into segment patterns. For long LED life always use current-limiting resistors (typically 220–330 Ω per segment) and if many segments are on at once consider using driver transistors or transceiver ICs. The code examples include both direct port writes for small projects and notes on multiplexing for multi-digit displays which is the next step after understanding a single-digit hookup. In the simulation below you will see the ATmega16 block with labelled pins and wires to each segment so the mapping becomes obvious and easy to replicate on a breadboard. Now let us study the wiring, hex codes, programs, and the animated visualiser that behaves like the real hardware.
Hardware wiring — Common cathode single digit to ATmega16
Connections used in examples (single-digit common cathode):
- Common cathode pin of the display → GND.
- Segment a → PB0 (with 220 Ω resistor)
- Segment b → PB1 (with 220 Ω resistor)
- Segment c → PB2 (with 220 Ω resistor)
- Segment d → PB3 (with 220 Ω resistor)
- Segment e → PB4 (with 220 Ω resistor)
- Segment f → PB5 (with 220 Ω resistor)
- Segment g → PB6 (with 220 Ω resistor)
Hex codes for digits 0–9 (common cathode, bit order g f e d c b a)
| Digit | Segments ON | Binary (gfedcba) | Hex |
|---|---|---|---|
| 0 | a b c d e f | 0b00111111 | 0x3F |
| 1 | b c | 0b00000110 | 0x06 |
| 2 | a b g e d | 0b01011011 | 0x5B |
| 3 | a b c d g | 0b01001111 | 0x4F |
| 4 | f g b c | 0b01100110 | 0x66 |
| 5 | a f g c d | 0b01101101 | 0x6D |
| 6 | a f g c d e | 0b01111101 | 0x7D |
| 7 | a b c | 0b00000111 | 0x07 |
| 8 | all | 0b01111111 | 0x7F |
| 9 | a b c d f g | 0b01101111 | 0x6F |
C program — show digits 0 to 9 on single common-cathode SSD (PORTB)
Paste this in your AVR project. Angle brackets are escaped for Blogger.
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
uint8_t seg[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
DDRB = 0xFF; // PB0..PB7 as output (we use PB0..PB6)
while (1) {
for (uint8_t i = 0; i < 10; i++) {
PORTB = seg[i]; // write pattern to PORTB
_delay_ms(500);
}
}
return 0;
}
Animated wiring diagram — ATmega16 to seven segment
Notes and practical tips
- Use a resistor for every segment; do not tie segments directly to the MCU pins without resistors.
- If many segments are ON, check total current; use a transistor driver or a transistor array (ULN2003) if required.
- For multi-digit displays use multiplexing: drive each digit common pin in turn while updating segment lines; keep refresh > 100 Hz to avoid flicker.
- Order of bits used in code (most examples use a-g mapped to bits 6..0) must match your wiring — adjust lookup table accordingly.
Conclusion
This article explained how to wire and drive a single common-cathode seven segment display from ATmega16 PORTB, supplied hex codes for digits 0–9, provided a simple AVR C program and an interactive animated wiring diagram that shows PB0..PB6 connected to segments a..g. Use this as a base for building counters, clocks, timers and readouts; next step is multiplexing to drive multiple digits with fewer pins.
Comments
Post a Comment
Subscribe to Post Comments [Atom]