7-Segment Display Counter with Raspberry Pi Pico
Wire a 7-segment display and build a bidirectional counter (0–F) controlled by a slide switch — simulated in Wokwi, no hardware required.
Open Free SimulationWhat is a 7-Segment Display?
A 7-segment display is an LED arrangement that can show digits 0–9 and hex letters A–F. It has 7 individual LED segments (labelled A–G) plus a decimal point (DP), arranged in a figure-8 pattern to form numbers and characters.
This project uses a common anode type — the common pin is connected to 3.3 V, and each segment is switched on by pulling its GPIO pin LOW.
- Counter counts 0 → 1 → 2 → … → F
- Each digit displays for 0.5 seconds
- Loops back to 0 automatically
- Counter counts F → E → D → … → 0
- Direction switches mid-sequence
- Great for countdown timers
Segment Layout & Pin Guide
Common Anode Type
| Seg | Position | GPIO |
|---|---|---|
| A | Top horizontal | GP2 |
| B | Top right vertical | GP3 |
| C | Bottom right vertical | GP4 |
| D | Bottom horizontal | GP5 |
| E | Bottom left vertical | GP6 |
| F | Top left vertical | GP8 |
| G | Middle horizontal | GP7 |
| • | Decimal point (DP) | GP0 |
| Common (COM) — Anode to 3V3 | 3V3 | |
Components Used
| # | Component | Role | Note |
|---|---|---|---|
| 1 | Raspberry Pi Pico | Main microcontroller | MicroPython v1.19+ |
| 2 | 7-Segment Display | Visual digit output | Common anode type |
| 3 | Slide Switch | Count direction selector | UP = ascend, DOWN = descend |
| 4 | Mini Breadboard | Circuit connections | wokwi-breadboard-mini |
| 5 | 220Ω Resistors | Segment current limiting | Optional in simulation |
| 6 | Jumper Wires | Wiring | Multiple colors |
Wiring & Pin Map
Full segment-to-GPIO mapping and power connections for this project:
| Segment / Pin | GPIO | Wire Color | Notes |
|---|---|---|---|
| A Top horizontal | GP2 | Green | — |
| B Top-right vertical | GP3 | Green | — |
| C Bottom-right vertical | GP4 | Green | — |
| D Bottom horizontal | GP5 | Green | — |
| E Bottom-left vertical | GP6 | Green | — |
| F Top-left vertical | GP8 | Green | — |
| G Middle horizontal | GP7 | Green | — |
| DP Decimal point | GP0 | Grey | Not connected (optional) |
| COM.1 / COM.2 — Common anode | 3V3 | Red | Via breadboard rail |
| Slide switch centre pin | GP13 | Orange | Input, reads direction |
| Slide switch GND side | GND.3 | Black | — |
| Slide switch 3V3 side | 3V3 | Red | — |
1 (HIGH) → counter ascends. When flipped DOWN, GP13 reads 0 (LOW) → counter descends. The code checks the switch value at each digit display cycle and can break mid-sequence to respond immediately.
Circuit Diagram (diagram.json)
Paste this into the diagram.json tab in your Wokwi project. It will automatically place all components and create all wired connections.
{
"version": 1,
"author": "Uri Shaked, Anderson Costa",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-mini", "id": "bb1", "top": -11, "left": 7.2, "attrs": {} },
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 125,
"left": 65.25,
"rotate": 90,
"attrs": { "env": "micropython-20220618-v1.19.1" }
},
{
"type": "wokwi-7segment",
"id": "sevseg1",
"top": 14.58,
"left": 72.28,
"attrs": { "common": "anode" }
},
{
"type": "wokwi-slide-switch",
"id": "sw1",
"top": -34,
"left": 22.3,
"attrs": { "bounce": "0" }
}
],
"connections": [
[ "pico:GP0", "$serialMonitor:RX", "", [] ],
[ "pico:GP1", "$serialMonitor:TX", "", [] ],
[ "pico:GP2", "bb1:16b.j", "green", [ "v0" ] ],
[ "bb1:10t.a", "bb1:16t.a", "green", [ "v0" ] ],
[ "bb1:11t.b", "bb1:15t.b", "green", [ "v0" ] ],
[ "pico:GP3", "bb1:15b.j", "green", [ "v0" ] ],
[ "bb1:15b.f", "bb1:15t.e", "green", [ "v0" ] ],
[ "bb1:16b.f", "bb1:16t.e", "green", [ "v0" ] ],
[ "pico:3V3", "bb1:17b.j", "red", [ "v18", "h61.45", "v-100.71", "h-42.25" ] ],
[ "bb1:17t.a", "bb1:9t.a", "red", [ "v-9.56", "h-76.8" ] ],
[ "pico:GP4", "bb1:10b.j", "green", [ "v-58", "h-38.4" ] ],
[ "pico:GP5", "bb1:8b.j", "green", [ "v-48", "h-48" ] ],
[ "pico:GP6", "bb1:7b.j", "green", [ "v-38", "h-38.4" ] ],
[ "pico:GP7", "bb1:6b.j", "green", [ "v-28", "h-38.4" ] ],
[ "pico:GP8", "bb1:5b.j", "green", [ "v-18", "h-38.4" ] ],
[ "pico:GP13", "bb1:3b.j", "orange",[ "v0" ] ],
[ "bb1:3b.f", "bb1:3t.e", "orange",[ "v0" ] ],
[ "bb1:4b.f", "bb1:4t.e", "black", [ "v0" ] ],
[ "pico:3V3", "bb1:2b.j", "red", [ "v18", "h-158.39", "v-99.76", "h241.39" ] ],
[ "bb1:5t.a", "bb1:8t.a", "green", [ "v0" ] ],
[ "bb1:6t.b", "bb1:7t.b", "green", [ "v0" ] ],
[ "bb1:17t.e", "bb1:17b.f", "red", [ "v0" ] ],
[ "bb1:2t.e", "bb1:2b.f", "red", [ "v0" ] ],
[ "bb1:5b.f", "bb1:5t.e", "green", [ "v0" ] ],
[ "bb1:6b.f", "bb1:6t.e", "green", [ "v0" ] ],
[ "sevseg1:COM.1", "bb1:9b.h", "", [ "$bb" ] ],
[ "sevseg1:COM.2", "bb1:9t.c", "", [ "$bb" ] ],
[ "sevseg1:A", "bb1:10t.c", "", [ "$bb" ] ],
[ "sevseg1:B", "bb1:11t.c", "", [ "$bb" ] ],
[ "sevseg1:C", "bb1:10b.h", "", [ "$bb" ] ],
[ "sevseg1:D", "bb1:8b.h", "", [ "$bb" ] ],
[ "sevseg1:E", "bb1:7b.h", "", [ "$bb" ] ],
[ "sevseg1:F", "bb1:8t.c", "", [ "$bb" ] ],
[ "sevseg1:G", "bb1:7t.c", "", [ "$bb" ] ],
[ "sevseg1:DP", "bb1:11b.h", "", [ "$bb" ] ],
[ "sw1:1", "bb1:2t.a", "", [ "$bb" ] ],
[ "sw1:2", "bb1:3t.a", "", [ "$bb" ] ],
[ "sw1:3", "bb1:4t.a", "", [ "$bb" ] ],
[ "bb1:4b.j", "pico:GND.3", "black", [ "v76.8", "h38.4" ] ]
],
"serialMonitor": { "display": "terminal" },
"dependencies": {}
}
Digit Bit Patterns (A–F hex)
Each digit is represented as an 8-bit array — one value per segment [A, B, C, D, E, F, G, DP]. For common anode: 0 = segment ON, 1 = segment OFF.
Complete MicroPython Code
Paste this into main.py in your Wokwi project or on your physical Pico.
from machine import Pin from utime import sleep # ────────────────────────────────────────────────── # 7-segment display layout (common anode) # # A # --- # F | G | B # --- # E | | C # --- # D # ────────────────────────────────────────────────── # GPIO pin assignment: [A, B, C, D, E, F, G, DP] pins = [ Pin(2, Pin.OUT), # A — top horizontal Pin(3, Pin.OUT), # B — top-right vertical Pin(4, Pin.OUT), # C — bottom-right vertical Pin(5, Pin.OUT), # D — bottom horizontal Pin(6, Pin.OUT), # E — bottom-left vertical Pin(8, Pin.OUT), # F — top-left vertical Pin(7, Pin.OUT), # G — middle horizontal Pin(0, Pin.OUT), # DP — decimal point (not used) ] # ────────────────────────────────────────────────── # Common anode: 0 = segment ON, 1 = segment OFF # Pattern order: [A, B, C, D, E, F, G, DP] # ────────────────────────────────────────────────── digits = [ [0, 0, 0, 0, 0, 0, 1, 1], # 0 [1, 0, 0, 1, 1, 1, 1, 1], # 1 [0, 0, 1, 0, 0, 1, 0, 1], # 2 [0, 0, 0, 0, 1, 1, 0, 1], # 3 [1, 0, 0, 1, 1, 0, 0, 1], # 4 [0, 1, 0, 0, 1, 0, 0, 1], # 5 [0, 1, 0, 0, 0, 0, 0, 1], # 6 [0, 0, 0, 1, 1, 1, 1, 1], # 7 [0, 0, 0, 0, 0, 0, 0, 1], # 8 [0, 0, 0, 1, 1, 0, 0, 1], # 9 [0, 0, 0, 1, 0, 0, 0, 1], # A [1, 1, 0, 0, 0, 0, 0, 1], # b [0, 1, 1, 0, 0, 0, 1, 1], # C [1, 0, 0, 0, 0, 1, 0, 1], # d [0, 1, 1, 0, 0, 0, 0, 1], # E [0, 1, 1, 1, 0, 0, 0, 1], # F ] # ────────────────────────────────────────────────── # Helper: turn all segments OFF # ────────────────────────────────────────────────── def reset(): """Turns off all segments (common anode = set HIGH).""" for pin in pins: pin.value(1) reset() # Slide switch on GP13 switch = Pin(13, Pin.IN) # ────────────────────────────────────────────────── # Main loop # ────────────────────────────────────────────────── while True: if switch.value() == 1: # ⬆️ Ascending counter: 0 → F for i in range(len(digits)): if switch.value() == 0: break for j in range(len(pins) - 1): pins[j].value(digits[i][j]) sleep(0.5) else: # ⬇️ Descending counter: F → 0 for i in range(len(digits) - 1, -1, -1): if switch.value() == 1: break for j in range(len(pins)): pins[j].value(digits[i][j]) sleep(0.5)
Step-by-Step Build Guide
main.py and diagram.json file will be created."common": "anode" attribute in the diagram sets this. In common anode mode, writing 0 to a GPIO turns the segment ON; writing 1 turns it OFF. The reset() function sets all pins HIGH (all segments off) on startup.pins list (8 GPIOs), a 2D digits array with all 16 hex patterns, and a main loop that reads the slide switch to choose direction.diagram.json was pasted correctly and the display type is set to anode.main.py to the board and run.
Comments
Post a Comment