🐭 Tutorial 27 - 4-Digit 7 Segment

The 4-Digit 7 Segment Display is a common type of display used to show numerical information. It consists of four seven-segment displays that can display numbers from 0 to 9. Each segment can be turned on or off to display the desired number. In this project, we will be using Micropython on a Raspberry Pi Pico to control the display.

This type of display can be used in a wide range of applications such as digital clocks, timers, and even in electronic measuring instruments. It is commonly used in the electronics industry due to its simple design, low cost, and ease of use. With the ability to display multiple digits, it provides a compact and efficient way to display numerical data.

By programming the Raspberry Pi Pico to control the 4-Digit 7 Segment Display, we can create a range of projects that require numerical displays. With the power and flexibility of Micropython, we can easily program the Pico to display any desired information on the 4-Digit 7 Segment Display.

Components Needed

ComponentQuantity
Raspberry Pi Pico W1
Micro USB Cable1
Breadboard1
WiresSeveral
Resistor4 - 220Ω
4 - 7 Segment Display1
74HC5951

Fritzing Diagram

Code

import machine
import time

SEGCODE = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f]

sdi = machine.Pin(18,machine.Pin.OUT)
rclk = machine.Pin(19,machine.Pin.OUT)
srclk = machine.Pin(20,machine.Pin.OUT)

placePin = []
pin = [10,13,12,11]
for i in range(4):
    placePin.append(None)
    placePin[i] = machine.Pin(pin[i], machine.Pin.OUT)

timerStart=time.ticks_ms()

def timer1():
    return int((time.ticks_ms()-timerStart)/1000)

def pickDigit(digit):
    for i in range(4):
        placePin[i].value(1)
    placePin[digit].value(0)

def clearDisplay():
    hc595_shift(0x00)

def hc595_shift(dat):
    rclk.low()
    time.sleep_us(200)
    for bit in range(7, -1, -1):
        srclk.low()
        time.sleep_us(200)
        value = 1 & (dat >> bit)
        sdi.value(value)
        time.sleep_us(200)
        srclk.high()
        time.sleep_us(200)
    time.sleep_us(200)
    rclk.high()
    time.sleep_us(200)

while True:
    count = timer1()
    #print(count)

    pickDigit(0)
    hc595_shift(SEGCODE[count%10])

    pickDigit(1)
    hc595_shift(SEGCODE[count%100//10])

    pickDigit(2)
    hc595_shift(SEGCODE[count%1000//100])

    pickDigit(3)
    hc595_shift(SEGCODE[count%10000//1000])yt

Code Explanation