How to Display Numbers on 4-Digit 7-Segment Display with Raspberry Pi Pico in Wokwi - Complete Step-by-Step Tutorial

 

Project Overview

This comprehensive guide demonstrates how to interface a 4-digit 7-segment display with a Raspberry Pi Pico microcontroller using the Wokwi online simulator. Learn to display numbers, create counters, show time, temperature readings, and build digital dashboard projects without any physical hardware.

What You'll Learn

  • 7-segment display operation principles
  • Multiplexing technique for multi-digit displays
  • Raspberry Pi Pico GPIO control
  • MicroPython programming for displays
  • Counter and timer applications
  • Wokwi simulator for display projects

Components Required in Wokwi

Hardware (Virtual):

Diagram.json:

{
  "version": 1,
  "author": "Uri Shaked",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-pi-pico",
      "id": "pico",
      "top": 65.16,
      "left": -27.77,
      "rotate": 90,
      "hide": false,
      "attrs": { "env": "arduino-community" }
    },
    {
      "type": "wokwi-7segment",
      "id": "sevseg1",
      "top": -21.97,
      "left": -85.69,
      "rotate": 0,
      "hide": false,
      "attrs": { "digits": "4" }
    }
  ],
  "connections": [
    [ "pico:GP0", "$serialMonitor:RX", "", [] ],
    [ "pico:GP1", "$serialMonitor:TX", "", [] ],
    [ "sevseg1:A", "pico:GP2", "green", [ "v-21", "h145", "v115", "h-68" ] ],
    [ "sevseg1:B", "pico:GP3", "green", [ "v-15", "h100", "v103", "h-65" ] ],
    [ "sevseg1:C", "pico:GP4", "green", [ "v16", "h37" ] ],
    [ "sevseg1:D", "pico:GP5", "green", [ "v22", "h53" ] ],
    [ "sevseg1:E", "pico:GP6", "green", [ "v32", "h42" ] ],
    [ "sevseg1:F", "pico:GP7", "green", [ "v-13", "h-96", "v116", "h104" ] ],
    [ "sevseg1:G", "pico:GP8", "green", [ "v11", "h1" ] ],
    [ "sevseg1:DP", "pico:GP9", "green", [ "v7", "h-3" ] ],
    [ "sevseg1:DIG1", "pico:GP10", "blue", [ "v-19", "h-84", "v128", "h81" ] ],
    [ "sevseg1:DIG2", "pico:GP11", "blue", [ "v-28", "h-121", "v144", "h80" ] ],
    [ "sevseg1:DIG3", "pico:GP12", "blue", [ "v-39", "h-139", "v162", "h79" ] ],
    [ "sevseg1:DIG4", "pico:GP13", "blue", [ "v66", "h-82" ] ]
  ]
}
  • 1x Raspberry Pi Pico board
  • 1x 4-Digit 7-Segment Display (Common Cathode)
  • 4x Current-limiting resistors (220Ω-330Ω) - optional in simulation
  • Jumper wires (automatic in Wokwi)

Software:

  • MicroPython firmware
  • 7-segment display library

Understanding 7-Segment Displays

What is a 7-Segment Display?

A 7-segment display uses seven LED segments (labeled A-G) plus a decimal point to show digits 0-9 and some letters.


Segment Mapping:

  • A = Top horizontal
  • B = Top right vertical
  • C = Bottom right vertical
  • D = Bottom horizontal
  • E = Bottom left vertical
  • F = Top left vertical
  • G = Middle horizontal
  • DP = Decimal point
Segment pio.h:

// -------------------------------------------------- //
// This file is autogenerated by pioasm; do not edit! //
// -------------------------------------------------- //

#pragma once

#if !PICO_NO_HARDWARE
#include "hardware/pio.h"
#endif

// ------- //
// segment //
// ------- //

#define segment_wrap_target 0
#define segment_wrap 5

static const uint16_t segment_program_instructions[] = {
            //     .wrap_target
    0x8080, //  0: pull   noblock         side 0    
    0xa027, //  1: mov    x, osr          side 0    
    0x6208, //  2: out    pins, 8         side 1    
    0x6408, //  3: out    pins, 8         side 2    
    0x6808, //  4: out    pins, 8         side 4    
    0x7008, //  5: out    pins, 8         side 8    
            //     .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program segment_program = {
    .instructions = segment_program_instructions,
    .length = 6,
    .origin = -1,
};

static inline pio_sm_config segment_program_get_default_config(uint offset) {
    pio_sm_config c = pio_get_default_sm_config();
    sm_config_set_wrap(&c, offset + segment_wrap_target, offset + segment_wrap);
    sm_config_set_sideset(&c, 4, false, false);
    return c;
}

#include "hardware/clocks.h"
static inline void segment_program_init(PIO pio, uint sm, uint offset, uint first_segment_pin, uint first_digit_pin) {
    const int segment_pins = 8;
    const int digit_pins = 4;

    // Machine configuration: out pins, sideset pins, and shift direction.
    pio_sm_config c = segment_program_get_default_config(offset);
    sm_config_set_out_pins(&c, first_segment_pin, segment_pins);
    sm_config_set_sideset_pins(&c, first_digit_pin);
    sm_config_set_out_shift(&c, false, false, 32); // Shift to the left

    // Attach all pins to the PIO peripheral
    for (int pin = first_segment_pin; pin < first_segment_pin + segment_pins; pin++) {
        pio_gpio_init(pio, pin);
    }
    for (int pin = first_digit_pin; pin < first_digit_pin + digit_pins; pin++) {
        pio_gpio_init(pio, pin);
    }

    // Configure all pins as outputs
    pio_sm_set_consecutive_pindirs(pio, sm, first_segment_pin, segment_pins, true);
    pio_sm_set_consecutive_pindirs(pio, sm, first_digit_pin, digit_pins, true);

    // Run at 2000Hz:
    float div = (float)clock_get_hz(clk_sys) / 2000;
    sm_config_set_clkdiv(&c, div);

    // Start the machine
    pio_sm_init(pio, sm, offset, &c);
    pio_sm_set_enabled(pio, sm, true);
}

#endif

Common Cathode vs Common Anode

Common Cathode (this tutorial uses this type):

  • All cathodes connected together to GND
  • Segments light up when pin is HIGH (3.3V)
  • More common and easier to use

Common Anode:

  • All anodes connected together to VCC
  • Segments light up when pin is LOW (0V)

4-Digit Display Multiplexing

A 4-digit display has:

  • 8 segment pins (A, B, C, D, E, F, G, DP)
  • 4 digit select pins (D1, D2, D3, D4)

Multiplexing Concept: Only one digit is active at a time, but switching happens so fast (>50 Hz) that all digits appear to be on simultaneously due to persistence of vision.

How it works:

  1. Enable Digit 1, display its segments, disable
  2. Enable Digit 2, display its segments, disable
  3. Enable Digit 3, display its segments, disable
  4. Enable Digit 4, display its segments, disable
  5. Repeat cycle continuously

Step-by-Step Wokwi Setup Instructions

Step 1: Create New Raspberry Pi Pico Project

  1. Navigate to https://wokwi.com
  2. Click "New Project"
  3. Select "Raspberry Pi Pico"
  4. Pico board appears on canvas

Step 2: Add 4-Digit 7-Segment Display

  1. Click the blue "+" (Add Part) button
  2. Search for "7-segment" or "4-digit"
  3. Select "4-Digit 7-Segment Display" or "TM1637"
  4. Place it above the Pico board

Note: Wokwi may have different display modules. Look for common cathode 4-digit displays.

Step 3: Understanding Pin Configuration

4-Digit 7-Segment Display Pins:

Segment Pins (8 pins):

  • A, B, C, D, E, F, G, DP (Decimal Point)

Digit Select Pins (4 pins):

  • D1 (Digit 1 - leftmost)
  • D2 (Digit 2)
  • D3 (Digit 3)
  • D4 (Digit 4 - rightmost)

Total: 12 pins (8 segment + 4 digit select)

Step 4: Pin Assignment Strategy

To control the display efficiently, we'll use:

Segment Pins → GPIO Pins:

SegmentGPIO PinDescription
AGPIO 2Top segment
BGPIO 3Top-right segment
CGPIO 4Bottom-right segment
DGPIO 5Bottom segment
EGPIO 6Bottom-left segment
FGPIO 7Top-left segment
GGPIO 8Middle segment
DPGPIO 9Decimal point

Digit Select Pins → GPIO Pins:

DigitGPIO PinPosition
D1GPIO 10Leftmost digit
D2GPIO 11Second digit
D3GPIO 12Third digit
D4GPIO 13Rightmost digit

Common Connection:

  • All digit cathodes → Pico GND (for common cathode)

Step 5: Wire the Circuit in Wokwi

Segment Connections (Blue/Green wires):

  1. Display A → Pico GPIO 2
  2. Display B → Pico GPIO 3
  3. Display C → Pico GPIO 4
  4. Display D → Pico GPIO 5
  5. Display E → Pico GPIO 6
  6. Display F → Pico GPIO 7
  7. Display G → Pico GPIO 8
  8. Display DP → Pico GPIO 9

Digit Select Connections (Blue wires):

  1. Display D1 → Pico GPIO 10
  2. Display D2 → Pico GPIO 11
  3. Display D3 → Pico GPIO 12
  4. Display D4 → Pico GPIO 13

Ground Connection:

  • Display GND (common cathode) → Pico GND

Wokwi Tips:

  • Click each display pin and drag to corresponding Pico GPIO
  • Wokwi auto-assigns wire colors
  • Hover over pins to see labels
  • Use zoom controls for precision
CODE:
/**
 * Pi Pico PIO driving a 4-digit seven segment display example.
 *
 * Copyright (C) 2021, Uri Shaked
 */

#include "segment.pio.h"

uint8_t digits[] = {
  0b11000000, // 0
  0b11111001, // 1
  0b10100100, // 2
  0b10110000, // 3
  0b10011001, // 4
  0b10010010, // 5
  0b10000010, // 6
  0b11111000, // 7
  0b10000000, // 8
  0b10011000, // 9
};

const uint8_t first_segment_pin = 2;
const uint8_t first_digit_pin = 10;

void setup() {
  Serial1.begin(115200);
  Serial1.println("Raspberry Pi Pico PIO 7-Segment Example");

  // Load the PIO program and initialize the machine
  auto offset = pio_add_program(pio0, &segment_program);
  segment_program_init(pio0, 0, offset, first_segment_pin, first_digit_pin);
}

void displayNumber(uint value) {
  pio_sm_put(pio0, 0,
    digits[value / 1000 % 10] << 24 |
    digits[value / 100 % 10] << 16 |
    digits[value / 10 % 10] << 8 |
    digits[value % 10]
  );
}

int i = 0;
void loop() {
  displayNumber(i++);
  delay(200);
}


Step 9: Run the Simulation

  1. Click green "Start Simulation" play button
  2. Watch all four digits light up showing "0000"
  3. See counter increment automatically
  4. Observe decimal point in demo 2
  5. Check Serial Monitor for demo progression

Expected Display Output:


First demo: 0000 → 0001 → 0002 → ... → 0099

Second demo: 00.0 → 01.0 → 02.0 → ... → 99.0
Third demo: 0010 → 0009 → 0008 → ... → 0000
Fourth demo: 1234 (static)
Fifth demo: Continuous counting

Step 10: Verify Display Operation

Visual Checks:

  • All four digits should be visible simultaneously
  • Numbers should be clear and bright
  • No flickering (refresh rate is adequate)
  • Decimal points work when enabled
  • Transitions are smooth

Serial Monitor:

7-Segment Display Test Starting...

Demo 1: Counting 0-9999
Demo 2: Decimal numbers
Demo 3: Countdown from 10
Demo 4: Display 1234
Demo 5: Continuous counter

Comments