Skip to main content

How to Connect 7-Segment Display to Raspberry Pi Pico in Wokwi: Complete Tutorial

7-Segment Display with Raspberry Pi Pico in Wokwi | MakeMindz
Beginner · MicroPython · Wokwi

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 Simulation
🔢

What 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.

⬆️ Switch UP — Ascending Mode
  • Counter counts 0 → 1 → 2 → … → F
  • Each digit displays for 0.5 seconds
  • Loops back to 0 automatically
⬇️ Switch DOWN — Descending Mode
  • Counter counts F → E → D → … → 0
  • Direction switches mid-sequence
  • Great for countdown timers
📐

Segment Layout & Pin Guide

A F B G E C D

Common Anode Type

SegPositionGPIO
ATop horizontalGP2
BTop right verticalGP3
CBottom right verticalGP4
DBottom horizontalGP5
EBottom left verticalGP6
FTop left verticalGP8
GMiddle horizontalGP7
Decimal point (DP)GP0
Common (COM) — Anode to 3V33V3
⚠️ Common Anode vs Common Cathode This project uses common anode. The shared pin goes to 3.3 V, and segments turn ON when their GPIO is pulled LOW (0). If you use a common cathode display, invert the 0/1 values in the digit patterns array.
🔧

Components Used

#ComponentRoleNote
1Raspberry Pi PicoMain microcontrollerMicroPython v1.19+
27-Segment DisplayVisual digit outputCommon anode type
3Slide SwitchCount direction selectorUP = ascend, DOWN = descend
4Mini BreadboardCircuit connectionswokwi-breadboard-mini
5220Ω ResistorsSegment current limitingOptional in simulation
6Jumper WiresWiringMultiple colors
📌

Wiring & Pin Map

Full segment-to-GPIO mapping and power connections for this project:

Segment / PinGPIOWire ColorNotes
A Top horizontalGP2Green
B Top-right verticalGP3Green
C Bottom-right verticalGP4Green
D Bottom horizontalGP5Green
E Bottom-left verticalGP6Green
F Top-left verticalGP8Green
G Middle horizontalGP7Green
DP Decimal pointGP0GreyNot connected (optional)
COM.1 / COM.2 — Common anode3V3RedVia breadboard rail
Slide switch centre pinGP13OrangeInput, reads direction
Slide switch GND sideGND.3Black
Slide switch 3V3 side3V3Red
💡 How the Slide Switch Works When the switch is in position UP, GP13 reads 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.

diagram.json
{
  "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.

0
0,0,0,0,0,0,1,1
1
1,0,0,1,1,1,1,1
2
0,0,1,0,0,1,0,1
3
0,0,0,0,1,1,0,1
4
1,0,0,1,1,0,0,1
5
0,1,0,0,1,0,0,1
6
0,1,0,0,0,0,0,1
7
0,0,0,1,1,1,1,1
8
0,0,0,0,0,0,0,1
9
0,0,0,1,1,0,0,1
A
0,0,0,1,0,0,0,1
b
1,1,0,0,0,0,0,1
C
0,1,1,0,0,0,1,1
d
1,0,0,0,0,1,0,1
E
0,1,1,0,0,0,0,1
F
0,1,1,1,0,0,0,1
🐍

Complete MicroPython Code

Paste this into main.py in your Wokwi project or on your physical Pico.

main.py — MicroPython
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

1
Open Wokwi and Start a New Project
Go to wokwi.com and click New Project. Select Raspberry Pi Pico as your board and choose MicroPython. A default main.py and diagram.json file will be created.
2
Paste diagram.json
Click the diagram.json tab in the Wokwi editor. Select all existing content and replace it with the full JSON above. When you save, the simulator will automatically place the Pico, 7-segment display, breadboard, and slide switch — and wire them all up.
3
Understand the 7-Segment Display Type
This project uses a common anode display. The "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.
4
Paste the MicroPython Code
Click the main.py tab and replace everything with the full code above. The code defines the 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.
5
Run the Simulation
Click the green ▶ Play button. The 7-segment display should start counting upward: 0 → 1 → 2 → … → F, changing every 0.5 seconds. If nothing shows, verify the diagram.json was pasted correctly and the display type is set to anode.
6
Test the Slide Switch
Click on the slide switch in the Wokwi canvas to toggle it. The counter should immediately switch to descending order: F → E → D → … → 0. The code checks the switch state at each digit, so direction changes take effect at the next step — within 0.5 seconds.
7
Add Resistors for Real Hardware
In Wokwi the simulation works without resistors. But if you build this on a physical Pico, add a 220Ω resistor in series with each segment pin (A–G, DP) to limit current and protect both the Pico's GPIO and the display LEDs. Without resistors, each segment can draw up to 20 mA — well over the Pico's 16 mA per GPIO limit.
8
Try on Physical Hardware (Optional)
Flash MicroPython to your Pico using Thonny IDE. Wire each segment through a 220Ω resistor to the corresponding GPIO, connect COM to 3.3 V, and wire the slide switch to GP13, GND, and 3.3 V. Copy main.py to the board and run.

© 2026 MakeMindz · Raspberry Pi Pico Tutorials · Built for makers and learners everywhere

Comments

try for free