Introduction: Controlling a 7 Segment 4 Digit Display With a Raspberry Pi

Picture of Controlling a 7 Segment 4 Digit Display With a Raspberry Pi

Here is a way to control 7 segment 4 digit display with a Raspberry Pi.

Please vote as I really want to win one contest in my life :-)

Sorry no pictures :-(

Step 1: The Wiring!

Picture of The Wiring!

We need to wire from the Pi GPIO to the pins on the display:

Resistors are all 390 Ω

Pin 1 on the display goes to pin 23 on the GPIO with a resistor

2 to 21 resistor

3 to 11 resistor

4 to 19 resistor

5 to 15 resistor

6 to 13 no resistor

7 to 12 resistor

8 to 16 no resistor

9 to 18 no resistor

10 to 22 resistor

11 to 24 resistor

12 to 26 no resistor

That is the wiring done :-)

Step 2: The Code!

Now we need to code it to work, I am using python, for this we need the time module and theRPI.GPIOmodule.

This code displays the time and updates it.

Here is the code:

import RPi.GPIO as GPIO
import time



GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

segments = (24,12,19,21,23,22,15,11)

for segment in segments:

GPIO.setup(segment, GPIO.OUT)

GPIO.output(segment, 0)

digits = (26,18,16,13)

for digit in digits:

GPIO.setup(digit, GPIO.OUT)

GPIO.output(digit, 1)

num = {' ':(0,0,0,0,0,0,0),












while True:

n = time.ctime()[11:13]+time.ctime()[14:16]

s = str(n).rjust(4)

for digit in range(4):

for loop in range(0,7):

GPIO.output(segments[loop], num[s[digit]][loop])

if (int(time.ctime()[18:19])%2 == 0) and (digit == 1):

GPIO.output(11, 1)


GPIO.output(11, 0)

GPIO.output(digits[digit], 0)


GPIO.output(digits[digit], 1)

except KeyboardInterrupt:


That is the code :-)

Step 3: Were DONE!

Now we are done and you have a working clock :-)

Please help me win!

If you have any questions please comment :-)


AndreB6 (author)2015-02-07

Thanks for fixing.

Still a little error in line 63.

if (int (time.ctime()[18:19])%2 == 0) and (digit == 1):


IndentationError: unexpected indent

ray_grod (author)AndreB62018-01-11

go through the script and make sure the indentations after if statement, loops and all that are correct... python is picky on whitespacing

bertwert (author)AndreB62015-02-07

The way to implement code into instructables is terrible!

It took me ten minutes to fix the other one yesterday!!!

Looks like there is a space before that line that needs to go poof!


jvinterberg (author)bertwert2015-02-15

Sorry to break it, but on the same line it seems like the ending ":" is missing in your code above.

bertwert (author)jvinterberg2015-02-17


It is easier to fix now I have stopped using the code type text.

ray_grod (author)2018-01-11

do we use gpio numbering or physical pi numbering?

hydranix (author)2016-04-27

I don't code in python, find it very annoying. but i went ahead and fixed the code since copy-paste fails on this site.

bobmccall made it! (author)2016-03-12

Finally got it working!!!

Used a Pi Zero with a Broadcom HDSP-B09G display from CPC.

Had the same problem as others with only the far right digit showing, but after much messing and testing finally realised it was an indent prob in python. In the code above I think there should be an additional tab - see attached pic.

Thanks, great little project!

randerson42 made it! (author)2015-11-03

I started with this tutorial and it was a awesome intro. But I had a few problems, so a friend told me about YL-3 8x7segment display he used on his Arduino Project. It is just 3 $ and it worked out pretty well.

I wrote a summary here:
I also provide the code on github.
At the moment I use it to display the price of the next gas-station on it.

bertwert (author)randerson422015-11-03


KavierK (author)2015-10-13

I did according to the tutorial above, the coding part is good, the wiring is correct as well but the result turn out to be not what im expected, please help =(

bertwert (author)KavierK2015-10-13

In looking at this picture, you have multiple resistors instead of a singular resistor through wires requiring resistors.

The output on the display indicates wiring errors.

KavierK (author)bertwert2015-10-13

I'm using a raspberry pi 2, reason being of using multiple resistors is because I only have a lots of 100 Ω resistor and 220Ω, the next power will be 470Ω so I uses the combination of 100Ω + 220Ω to form a 320 Ω. I did the wiring according to the table above ><

bertwert (author)KavierK2015-10-14


Are you sure you've got the right pins?

FaisalA19 (author)2015-10-06

and what about pi 2??????????????????????????????????????????

bertwert (author)FaisalA192015-10-06

It would work.

bertwert (author)bertwert2015-10-06

Just make sure you use the same pins.

bertwert (author)bertwert2015-10-06

Just make sure you use the same pins.

OlivierCreativ (author)2015-09-12

Hello I'm sorry, i have a error on line : 14

File "", line 14

GPIO.setup(segment, GPIO.OUT)

Why plz ?

JasperK3 (author)2015-04-07

I made this, but unfortunately only the leftmost digit lit (although it was correct). I have checked all my connections, as well as the display, but still can't get it to work. Raspberry pi pins 16, 18, and 26 seem to be the issue, as applying voltage to display pins 8, 9, and 12 causes the missing digits to light.

JasperK3 (author)JasperK32015-04-07

Edit, I meant the rightmost digit.

carlinidragon (author)JasperK32015-07-11

Hi! Thanks for the instructable, I'm finding it very useful. Unfortunately, I get the same problem as JasperK3, and only the rightmost digit (pin 6-13) ligths up... Did you manage ot solve it, or does it happen to anyone else? I've checkd and rechecked my connections.

Fixed it! It was a matter of indentation in my code, some lines were outside the "for digit" loop... Sorry!

AndreB6 (author)2015-02-06

I get a Syntax Error on line 13.

= (24,12,19,21,23,22,15,11)


bertwert (author)AndreB62015-02-06

Oops sorry ....



= (24, 12, 19, 21, 23, 22, 15, 11)

Should read:

segments = (24, 12, 19, 21,23, 22, 15, 11)

Thanks for spotting that - fixed :-)

sarju7 (author)2014-11-10

Remember to take pictures next time but other than that good

bertwert (author)sarju72014-11-10

I took it apart but at the time I wasn't thinking of instructables :-)

About This Instructable




More by bertwert:Choosing your first DSLRHomemade Camera Lensbertwert
Add instructable to: