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

http://www.raspberrypi.org/forums/viewtopic.php?f=...

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!

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.setmode(GPIO.BOARD)

GPIO.setwarnings(False)

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),

'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)}

try:

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)

else:

GPIO.output(11, 0)

GPIO.output(digits[digit], 0)

time.sleep(0.001)

GPIO.output(digits[digit], 1)

except KeyboardInterrupt:

GPIO.cleanup()

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 :-)

Comments

author
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.

http://pastebin.com/wCRWFK6d

author
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!

_20160313_003444.JPGclockpypart.png
author
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: http://robinhenniges.com/en/raspberry-pi-with-yl-3...
I also provide the code on github.
At the moment I use it to display the price of the next gas-station on it.

IMG_20151103_133648.jpg
author
bertwert (author)randerson422015-11-03

Nice.

author
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 =(

12081481_10203463402304385_337681040_n.jpg
author
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.

author
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 ><

author
bertwert (author)KavierK2015-10-14

OK

Are you sure you've got the right pins?

author
FaisalA19 (author)2015-10-06

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

author
bertwert (author)FaisalA192015-10-06

It would work.

author
bertwert (author)bertwert2015-10-06

Just make sure you use the same pins.

author
bertwert (author)bertwert2015-10-06

Just make sure you use the same pins.

author
OlivierCreativ (author)2015-09-12

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

File "script.py", line 14

GPIO.setup(segment, GPIO.OUT)

Why plz ?

author
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.

author
JasperK3 (author)JasperK32015-04-07

Edit, I meant the rightmost digit.

author
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.

author

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

author
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

author
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!

STOP FINDING ERRORS ;-)

author
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.

author
bertwert (author)jvinterberg2015-02-17

Fixed,

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

author
AndreB6 (author)2015-02-06

I get a Syntax Error on line 13.

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

^

author
bertwert (author)AndreB62015-02-06

Oops sorry ....

Lines:

segments

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

Should read:

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

Thanks for spotting that - fixed :-)

author
sarju7 (author)2014-11-10

Remember to take pictures next time but other than that good

author
bertwert (author)sarju72014-11-10

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

About This Instructable

25,707views

37favorites

License:

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