This tutorial shows you how to wire and program a 7-segment display with Raspberry Pi Pico using the Wokwi online simulator. Perfect for creating digital counters, clocks, and numerical displays!
What is a 7-Segment Display?
A 7-segment display is an LED arrangement that can show digits 0-9 and some letters. It has 7 individual LED segments (labeled A-G) plus a decimal point, arranged to form numbers.
Getting Started with Wokwi
Step 1: Open Wokwi and Create New Project
- Go to https://wokwi.com
- Click "New Project"
- Select "Raspberry Pi Pico" as your microcontroller
Add 7-Segment Display:
- Click the blue "+" button
- Search for "7-segment display"
- Select "7-Segment Display" (common cathode type)
- Click to add it to your workspace
Add Resistors (Optional but Recommended):
- For hardware protection, add 220Ω resistors
- In Wokwi simulation, resistors are optional since it's virtual
- Add 7-8 resistors if you want a realistic circuit
Step 3: Understanding the 7-Segment Display Pins
The display has segments labeled:
- A - Top horizontal segment
- B - Top right vertical segment
- C - Bottom right vertical segment
- D - Bottom horizontal segment
- E - Bottom left vertical segment
- F - Top left vertical segment
- G - Middle horizontal segment
- DP - Decimal point
- COM/GND - Common cathode (ground)
Step 4: Wire the Circuit in Wokwi
Based on your diagram, connect as follows:
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:GND.1", "led1:C", "black", [ "*", "v0" ] ],
[ "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": {}
}
Ground Connection:
- Connect the common cathode pin (usually marked COM or pin 3 and 8) to GND on the Pico
Segment Connections to GPIO Pins:
Connect each segment through the following GPIO pins:
| Segment | GPIO Pin | Color in Diagram |
|---|---|---|
| A | GP2 | Green |
| B | GP3 | Green |
| C | GP4 | Green |
| D | GP5 | Green |
| E | GP6 | Yellow |
| F | GP7 | Black |
| G | GP8 | Black |
| DP | GP9 | Red (optional) |
To wire in Wokwi:
- Click on a segment pin of the 7-segment display
- Drag to the corresponding GPIO pin on the Pico
- Repeat for all segments
- Connect common cathode to GND (use the red power rail if using breadboard)
Code:
from machine import Pin
from utime import sleep
# 7-segment display layout
# A
# ---
# F | G | B
# ---
# E | | C
# ---
# D
pins = [
Pin(2, Pin.OUT), # A
Pin(3, Pin.OUT), # B
Pin(4, Pin.OUT), # C
Pin(5, Pin.OUT), # D
Pin(6, Pin.OUT), # E
Pin(8, Pin.OUT), # F
Pin(7, Pin.OUT), # G
Pin(0, Pin.OUT) # DP (not connected)
]
# Common anode 7-segment display digit patterns
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
]
def reset():
"""Turns off all segments on the 7-segment display."""
for pin in pins:
pin.value(1)
reset()
switch = Pin(13, Pin.IN)
while True:
if switch.value() == 1:
# Ascending counter
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
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)

Comments
Post a Comment