7 Segment Display + Multiplexing (2024)

A tutorial on how to use the 7 segment display with the Raspberry Pi Pico

YouTube Video #

A 7-segment display is an electronic device that is used to display numeric information in a digital format. It consists of seven LED (Light Emitting Diode) segments arranged in a rectangular shape, with an eighth segment for displaying decimal points. Each of the seven segments can be lit up individually, allowing for the display of numbers 0 through 9, as well as some letters and symbols.

The 7-segment display is commonly used in digital clocks, calculators, and other electronic devices that require numeric displays. It can be interfaced with a microcontroller or other digital device, and controlled using simple digital signals.

There are two types of 7-segment displays: common anode and common cathode. In a common anode display, all the anodes of the LEDs are connected together and to a positive voltage source, while each of the cathodes is connected to a digital output pin. In a common cathode display, the cathodes of the LEDs are connected together and to a negative voltage source, while each of the anodes is connected to a digital output pin.

To display a number on the 7-segment display, the appropriate digital outputs are turned on or off, causing the corresponding segments to light up.

Overall, the 7-segment display is a versatile and widely-used display module that provides a simple and effective way to display numeric information in a digital format. Its ease of use and low power consumption make it a popular choice for hobbyists and professionals alike.

Components Needed #

ComponentQuantity
Raspberry Pi Pico W1
Micro USB Cable1
Breadboard1
WiresSeveral
Resistor1 - 300Ω
7 Segment Display2

Example 1 - Fritzing Diagram #

7 Segment Display + Multiplexing (1)

Code #

from machine import Pinimport utime# Define the GPIO pins for each segmentsegments = (8,9,4,3,2,7,6,5)# Define the digit patterns for 0-9 on the 7-segment display# 0 means the segment is on, 1 means the segment is offpatterns = [ (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]# Initialize the GPIO pinspins = [Pin(seg, Pin.OUT) for seg in segments]# Initialize the common cathode for the displaydisplay = Pin(16, Pin.OUT, value=1)def display_digit(digit): # Get the pattern for the digit pattern = patterns[digit] # Set each segment to the correct state for i in range(8): pins[i].value(pattern[i]) # Turn on the display display.value(0) # Turn off the display display.value(1)# Test the displaywhile True: for i in range(10): start = utime.ticks_ms() while utime.ticks_diff(utime.ticks_ms(), start) < 1000: display_digit(i) 

Code explanation #

GPIO Pin and Digit Pattern Definitions #

from machine import Pinimport utime# Define the GPIO pins for each segmentsegments = (8,9,4,3,2,7,6,5)# Define the digit patterns for 0-9 on the 7-segment display# 0 means the segment is on, 1 means the segment is offpatterns = [ (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]

In this section, we import the necessary modules (machine and utime), and then define the GPIO pins used for each segment of the 7-segment display. The segments tuple contains the pin numbers. We also define patterns, a list of tuples that represents the digit patterns for the numbers 0 to 9. In each tuple, a 0 represents an illuminated segment, and a 1 represents a segment that is turned off.

GPIO Initialization #

# Initialize the GPIO pinspins = [Pin(seg, Pin.OUT) for seg in segments]# Initialize the common cathode for the displaydisplay = Pin(16, Pin.OUT, value=1)

This section initializes the GPIO pins and the common cathode pin for the display. The pins list is created using a list comprehension, where each element is an instance of the Pin class, setting each GPIO pin from the segments tuple as an output pin. The display object is also created using Pin, representing the common cathode pin (pin 16) with an initial high value (1) to turn off the display.

Display Digit Function #

def display_digit(digit): # Get the pattern for the digit pattern = patterns[digit] # Set each segment to the correct state for i in range(8): pins[i].value(pattern[i]) # Turn on the display display.value(0) # Turn off the display display.value(1)

This section defines the display_digit function, which takes a digit parameter. The function retrieves the corresponding digit pattern from patterns based on the given digit. It then iterates over the eight segments and sets the respective GPIO pins according to the digit pattern. The common cathode pin is set to low (0) to turn on the display and then set back to high (1) to turn off the display.

Testing and Displaying the Digits #

# Test the displaywhile True: for i in range(10): start = utime.ticks_ms() while utime.ticks_diff(utime.ticks_ms(), start) < 1000: display_digit(i)

In this section, an infinite while loop is used to continuously test and display the digits on the 7-segment display. The for loop runs through the numbers 0 to 9, and for each digit i, it starts a while loop. Inside the inner loop, the current digit i is displayed using the display_digit function. The inner loop continues for 1 second (1000 milliseconds) before moving on to the next digit. This creates a delay of 1 second between displaying each digit.

Example 2 - Fritzing Diagram #

7 Segment Display + Multiplexing (2)

Code #

from machine import Pinimport utime# Define the GPIO pins for each segmentsegments = (8,9,4,3,2,7,6,5)# Define the digit patterns for 0-9 on the 7-segment display# 0 means the segment is on, 1 means the segment is offpatterns = [ (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]# Initialize the GPIO pinspins = [Pin(seg, Pin.OUT) for seg in segments]# Initialize the common cathodes for the displaysdisplays = [Pin(16, Pin.OUT, value=1), Pin(17, Pin.OUT, value=1)]def display_digit(display, digit): # Turn off both displays displays[0].value(1) displays[1].value(1) # Get the pattern for the digit pattern = patterns[digit] # Set each segment to the correct state for i in range(8): pins[i].value(pattern[i]) # Turn on the correct display displays[display].value(0) # Wait a short time utime.sleep_ms(12) # Turn off the display displays[display].value(1) # Test the displays# Test the displayswhile True: for i in range(10): for j in range(10): start = utime.ticks_ms() while utime.ticks_diff(utime.ticks_ms(), start) < 1000: display_digit(0, i) display_digit(1, j)

Code explanation #

from machine import Pinimport utime# Define the GPIO pins for each segmentsegments = (8,9,4,3,2,7,6,5)# Define the digit patterns for 0-9 on the 7-segment display# 0 means the segment is on, 1 means the segment is offpatterns = [ (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]

In this section, we import the necessary modules (machine and utime), and then define the GPIO pins used for each segment of the 7-segment display. The segments tuple contains the pin numbers. We also define patterns, a list of tuples that represents the digit patterns for the numbers 0 to 9. In each tuple, a 0 represents an illuminated segment, and a 1 represents a segment that is turned off.

# Initialize the GPIO pinspins = [Pin(seg, Pin.OUT) for seg in segments]# Initialize the common cathodes for the displaysdisplays = [Pin(16, Pin.OUT, value=1), Pin(17, Pin.OUT, value=1)]

Initialize the GPIO pins for each segment using the Pin class. List comprehension is used to create a list of Pin objects. Initialize the common cathode pins for the displays. The Pin function is used to set up Pin objects for GPIO pins 16 and 17, with an initial value of 1 to turn off the displays.

def display_digit(display, digit): displays[0].value(1) # Turn off display 0 displays[1].value(1) # Turn off display 1 pattern = patterns[digit] # Get the pattern for the given digit # Set each segment to the correct state for i in range(8): pins[i].value(pattern[i]) displays[display].value(0) # Turn on the specified display utime.sleep_ms(12) # Wait for a short time displays[display].value(1) # Turn off the specified display

Define a function display_digit that takes a display and digit as arguments. This function displays a specific digit on a specific display. It turns off both displays, sets the segment pins based on the digit’s pattern, turns on the appropriate display, waits for a short time, and turns off the display.

while True: for i in range(10): for j in range(10): start = utime.ticks_ms() while utime.ticks_diff(utime.ticks_ms(), start) < 1000: display_digit(0, i) # Display digit i on display 0 display_digit(1, j) # Display digit j on display 1

This code repeatedly displays two digits on a 7-segment display with multiplexing through GPIO pins GP15 (display 0) and GP17 (display 1).

← 4 x 4 Matrix KeyPadVoice Recognition Sensor Module →
7 Segment Display + Multiplexing (2024)
Top Articles
K Tattoo With Heart - Tattoo Mastery Academy
Candy Heart Tattoo: Unveiling Sweet Expressions in Ink
Lkq Pull-A-Part
Sixth Circuit Denies Qualified Immunity for State University Officials Who Allegedly Violated Professor's First Amendment Rights
Defense Immunity 2K23 Meaning
Craigslist Coeur D'alene Spokane
Valeriewhitebby Footjob
Grand Rapids Herald-Review Obituaries
Inloggen bij AH Sam - E-Overheid
Emily Katherine Correro
Aaf Seu
Xiom Vega X Review & Playtesting • Racket Insight
Craigslist Of Valdosta Georgia
50 Shades Of Grey Movie 123Movies
Kplctv Weather Forecast
Simply Gorgeous! — Tecnam Completely Refreshed their Best-Selling Light-Sport Aircraft - ByDanJohnson.com
Alamy Contributor Forum
Peoplesoft Oracle Americold Login
Frostbite Blaster
1970 Baltimore Orioles World Series Scroll Pennant
Espn Masters Leaderboard
Sejinming Telegram
What Does Exp Wed Mean On Hulu
Vision Government Solutions Stamford Ct
Myhr.bannerhealth.com
Aunt Nettes Menu
Vip Market Vetsource
2024-25 ITH Season Preview: USC Trojans
My Les Paul Forum
Goodwoods British Market Friendswood
Did Hannah Jewell Leave Wnem Tv5
Spn 102 Fmi 16 Dd15
Craigs List Skagit County
Envision Okta Sign In
Unfall mit Ikarus C42: Gefangen in der Umkehr-Falle
Teamnet O'reilly Login
Taika Waititi Birth Chart
Swissport Timecard
Kenji Lentil Soup
Giant Egg Classic Wow
Zuercher Portal Inmates Kershaw County
Ontdek Sneek | Dé leukste stad van Friesland
Plusword 358
Arre St Wv Srj
Joy Ride 2023 Showtimes Near Mjr Chesterfield
Braveheart Parents Guide
Duxa.io Reviews
Craigslist Antelope Valley General For Sale
Craigslist Old Forge
29+ Des Moines Craigslist Furniture
Codex Genestealer Cults 10th Edition: The Goonhammer Review
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 6035

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.