You are on page 1of 7

Welcome to Pico sensor kit repository

7 segment Display
Segment7.py

Learning target

In this course, we will learn how to use pins of the Raspberry Pi Pico board.

How to control the 7 segment Display module.

Preparation

Raspberry Pi Pico board *1

Pico sensor expansion board *1

PC *1

USB data cable *1

7 Segment Display module *1

male-to-male DuPont line *9

Note:

7 segment display can be controlled with a few Micropython lines from Raspberry PI
Pico. It is one of simplest projects and a funny way to start coding and cabling

7 segment display is used within a wide number of applications, usually to


single a display number. These devices have simple internal wiring
diagrams, which maps one by one LEDs to its pins:
Please note that this tutorial uses a common cathode 7 segment display.
This means that the common pins (3 and 8) go to the ground and each led
segment is turned on when a positive value comes from RPI Pico. With
common anode, the common Pind go to 3,3V and each led segment is
turned on when a negative value comes from RPI Pico. In the second case,
compared to my tutorial you will have to change 3 and 8 pins wiring them
to Raspberry PI Pico physical pin 36 (3v3 OUT) and invert my code logic.

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

Display Segment Display Pin Raspberry PI Pico GP pin


A 7 17
B 6 16
C 4 14
D 2 13
E 1 12
F 9 18
G 10 19
DP 5 15
GRD 3 GND
GRD 8 GND

4. About code
Thonny programming

About how to using ThonnyIDE, please check the tutorials in【2.Development


environment】
from machine import Pin
# define GP ports to use
# Pins Matching: A, B, C, D, E, F, G
display_list = [17,16,14,13,12,18,19]
dotPin=15
display_obj = []

# Set all pins as output


for seg in display_list:
display_obj.append(Pin(seg, Pin.OUT))

dot_obj=Pin(dotPin, Pin.OUT)

# DIGIT map as array of array


arrSeg = [[1,1,1,1,1,1,0],\
[0,1,1,0,0,0,0],\
[1,1,0,1,1,0,1],\
[1,1,1,1,0,0,1],\
[0,1,1,0,0,1,1],\
[1,0,1,1,0,1,1],\
[1,0,1,1,1,1,1],\
[1,1,1,0,0,0,0],\
[1,1,1,1,1,1,1],\
[1,1,1,1,0,1,1]]

def SegDisplay(toDisplay):
numDisplay = int(toDisplay.replace(".", ""))
for a in range(7):
display_obj[a].value(arrSeg[numDisplay][a])
# Manage DOT activation
if toDisplay.count(".") == 1:
dot_obj.value(1)
else:
dot_obj.value(0)

SegDisplay("5.")
First of all, we import required libraries:

from machine import Pin

Then we define 2 variables with Pin numbers. This is a convenient way as, if you need
to change wirings, you can set new GP numbers here instead of spreading around in
our code. display_list collects all segments pins, ordered so that they will match A, B, C,
D, E, F and G LED segments:

display_list = [17,16,14,13,12,18,19]

dotPin=15

We’ll use arrays to make our code as shorter as possible. One of these arrays will
collect the PIN objects. This array is first declared and then filled with a for loop which
also set pins to out mode:

display_obj = []

for seg in display_list:

display_obj.append(Pin(seg, Pin.OUT))

Same PIN setting also for the one connected to the dot LED:

dot_obj=Pin(dotPin, Pin.OUT)

Another array variable keeps a mapping of numbers with their LED


configuration. For example, the number 0 (zero) has all the LED on, except
the central segment (G). This will be represented by the arrSeg[0] that
maps the “1,1,1,1,1,1,0” as A=1,B=1,C=1,D=1,E=1,F=1,G=0. Same logic for all
other numbers:
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

Here I use a function to control the 7 segment display so that calling this
function with a string representing what number we want to show (with or
without the dot). This function starts converting what is received into a
number (int() function) and removing the trailing dot (“.”), if present. In this
way the numDisplay variable is an integer number and can be used in
following for loop to configure pins output:

def SegDisplay(toDisplay): numDisplay = int(toDisplay.replace(".", "")) for a in


range(7): display_obj[a].value(arrSeg[numDisplay][a])

Still inside the SegDisplay() function, we also manage the dot led activation
by checking if the “.” is present in the input value:

if toDisplay.count(".") == 1: dot_obj.value(1) else: dot_obj.value(0)

Finally, we use our function by calling it with the desired output. For
example, number five with the dot led on will be call with following:

SegDisplay("5.")

If we don’t want the dot, simply call in this way:

SegDisplay("5")

The number can vary from 0 to 9 and the parameter needs to be a string. If
you have integer numbers, you can use the str() function to convert them.
An example:

SegDisplay(str(5))
5. Phenomenon

Click the green run button of Thonny IDE to start running the program. Click the

red stop button to stop the program.

You might also like