Last Updated on 2nd September 2023 by peppe8o
In this tutorial, I’m going t show you how to connect and configure a 7 segment display with a Raspberry PI Pico. If you are interested in how to get it working with Raspberry PI computer boards (like RPI Zero, RPI 4 model B, RPI 3 model A/B, and so on), please refer to my Control a 7 Segment Display from Raspberry PI with Python.
How 7-Segment Display Works
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.
What We Need
As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if continue with the project or remove them from the shopping cart. So, hardware will be only:
- A common computer (maybe with Windows, Linux or Mac). It can also be a Raspberry PI Computer board
- Raspberry PI Pico microcontroller (with a common micro USB cable)
- a 7 Segment Display
- 8x resistors (220 Ohm used)
- dupont wirings
Many of the listed hardware (except Raspberry PI Pico) can be bought alone or can be also found in the useful Elegoo starter kit.
Check hardware prices with the following links:
Prepare cabling according to the following wiring diagram. Regarding Raspberry PI Pico pinout, you can refer my guide showing it: Raspberry PI Pico pinout
This wiring produces the following mapping between Display and Raspberry PI:
|Display Segment||Display Pin||Raspberry PI Pico GP pin|
So, each Raspberry PI Pico pin powers a specific Led segment. Powering together a defined sequence of segments will display the number we want to show.
Please find below some pictures from my home lab:
Prepare cabling according to the previous paragraph.
Get and Understand my rpiPicoSegDisplay.py Code
Connect RPI Pico to Thonny (you can refer to my tutorial about the First steps with Raspberry PI Pico).
Now download the following file to use your passive buzzer:
You can both load it in your Raspberry PI Pico storage or run it from your computer.
I’ll explain all code lines in the following paragraphs.
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:
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 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 displays 0 [0,1,1,0,0,0,0],\ # -> arrSeq displays 1 [1,1,0,1,1,0,1],\ # -> arrSeq displays 2 [1,1,1,1,0,0,1],\ # -> arrSeq displays 3 [0,1,1,0,0,1,1],\ # -> arrSeq displays 4 [1,0,1,1,0,1,1],\ # -> arrSeq displays 5 [1,0,1,1,1,1,1],\ # -> arrSeq displays 6 [1,1,1,0,0,0,0],\ # -> arrSeq displays 7 [1,1,1,1,1,1,1],\ # -> arrSeq displays 8 [1,1,1,1,0,1,1]] # -> arrSeq 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:
If we don’t want the dot, simply call in this way:
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:
Interested to do more with your Raspberry PI Pico? Try to look at my Raspberry PI Pico tutorials for useful and funny projects!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?