# Controlling a 7 Segment 4 Digit Display With a Raspberry Pi

27,540

37

24

Posted

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

If you have any questions please comment :-)

## Recommendations

464 Enrolled

• ### Make it Move Contest

We have a be nice policy.

## Questions

Thanks for fixing.

Still a little error in line 63.

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

^

IndentationError: unexpected indent

4 replies

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

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

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

Fixed,

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

do we use gpio numbering or physical pi numbering?

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

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

3 replies

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.

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

OK

Are you sure you've got the right pins?

3 replies

It would work.

Just make sure you use the same pins.

Just make sure you use the same pins.

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

File "script.py", line 14

GPIO.setup(segment, GPIO.OUT)

Why plz ?

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.

3 replies

Edit, I meant the rightmost digit.

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!