How to control a 7 Segment Display from Raspberry PI with Python

Check my RPI articles in Best Raspberry PI projects article or home page. Or subscribe my newsletter (top right in this page) to be notified when new projects are available!
7 digit display

7 segment display is a simple electronic display composed of 7 (surprise) LEDs. It is common to find an addictional led for a little dot near classic 7 segments.

It is used within a wide number application, usually to diplay numbers.

These devices have a simple internal wiring diagrams, which maps one by one LEDs to its pins:

7 digit display pin

In this article we’ll control a 7 Segment Display from a Raspberry PI Zero W. This article applies also to newer Raspberry PI boards.

What We Need

Raspberry PI Zero WH board

As usual, I suggest adding from now to your favourite ecommerce shopping chart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from shopping chart. So, hardware will be only:

Many of listed hardware (except from Raspberry PI Zero W and micro SD Card) can be bought alone or can be also found in the useful Elegoo starter kit.

Step-by-Step Procedure

Wiring Diagram

Prepare cabling according to following wiring diagram:

RPI 7 digit display wiring

This wiring produces the following mapping between Display and Raspberry PI:

Display SegmentDisplay PinRaspberry PI phisical pinRaspberry PI BCM GPIO

So, each Raspberry PI pin / GPIO powers on/off its related segment. Powering on appropriated segments together will display the number we want to show.

Please find below the overall picture:

RPI 7 segment Display picture

OS Preparation

Start with OS installation using Install Raspbian Buster Lite guide.

Make your OS up-to-date:

sudo apt update
sudo apt upgrade

Python Scripting

Install GPIO

sudo apt install rpi.gpio

Get from my download area script:


Script Usage

This script can be used by simply calling with a simple argument passing the number to display and the dot (if required). So, for example:

python3 1.

Will display the number “1” and powers ON the dot led.

python3 5

Will display the number “5” and powers OFF the dot led.

python3 10

Will power OFF the whole display.

Script explaination

First section imports required libraries

import sys
import RPi.GPIO as GPIO

This script works mapping following pins activation according to wiring diagram:

Num_to Display -> GPIO pins state
0 -> [1,1,1,1,1,1,0]
1 -> [0,1,1,0,0,0,0]
2 -> [1,1,0,1,1,0,1]
3 -> [1,1,1,1,0,0,1]
4 -> [0,1,1,0,0,1,1]
5 -> [1,0,1,1,0,1,1]
6 -> [1,0,1,1,1,1,1]
7 -> [1,1,1,0,0,0,0]
8 -> [1,1,1,1,1,1,1]
9 -> [1,1,1,1,0,1,1]
10 (OFF) -> [0,0,0,0,0,0,0]

Dot GPIO (6) is managed alone. Last array is used to deactivate the Display.

GPIO id order is stored in a simple array. GPIO order is according to mapping. GPIO are also all set as output.

display_list = [17,27,22,10,9,11,5]

for pin in display_list:
 GPIO.setup(pin,GPIO.OUT) # setting pins
 GPIO.setup(6,GPIO.OUT) # setting dot pin

Warnings are disabled because this script will leave display active after execution.

A new array is defined, listing all possible combinations to Display. So, arrSeq[0] stores GPIO status to show “0” on display. arrSeq[10] will be managed alone to push a GPIO.cleanup.

arrSeg = [[1,1,1,1,1,1,0],\ # -> arrSeq[0] displays 0
 [0,1,1,0,0,0,0],\ # -> arrSeq[1] displays 1
 [1,1,0,1,1,0,1],\ # -> arrSeq[2] displays 2
 [1,1,1,1,0,0,1],\ # -> arrSeq[3] displays 3
 [0,1,1,0,0,1,1],\ # -> arrSeq[4] displays 4
 [1,0,1,1,0,1,1],\ # -> arrSeq[5] displays 5
 [1,0,1,1,1,1,1],\ # -> arrSeq[6] displays 6
 [1,1,1,0,0,0,0],\ # -> arrSeq[7] displays 7
 [1,1,1,1,1,1,1],\ # -> arrSeq[8] displays 8
 [1,1,1,1,0,1,1]] # -> arrSeq[9] displays 9

Then a basic error check on passed argument is performed. On error, program exits:

if len(sys.argv) > 2:
 print("ERROR: too many arguments")
elif len(sys.argv) == 1:
 print("ERROR: missing argument")
elif int(sys.argv[1].replace(".", "")) > 10 or int(sys.argv[1].replace(".", ""))<0:
 print("ERROR: insert a number between 0 and 10")

Dot led activation is managed alone, so only number is passed to main diplay procedure:

if sys.argv[1].count(".") == 1:GPIO.output(6,1)

numDisplay = int(sys.argv[1].replace(".", ""))

FInal operation is just activating GPIOs according to number passed on argument. This script then exits:

if numDisplay == 10: GPIO.cleanup()
else: GPIO.output(display_list, arrSeg[numDisplay])



How useful was this post?

Click on a star to rate it anonymously!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?