Menu Close

Seven Segment Display on Raspberry Pi

This tutorial of Robo India explains how to use a seven segment display on Raspberry Pi.

1. Introduction:

A seven segment display is a simple displaying device that uses 8 LEDs to display decimal numerals. It is generally used in digital clocks, calculators, electronic meters, and other devices that displays numerical information.

A seven segment display has 8 LEDs in it. Each LED is controlled through specific pin. It is made of seven different illuminating segments which are arranged in such a way that it can form the numbers from 0-9 by displaying different combinations of segments. It is also able to form some alphabets like A, B, C, H, F, E, G, DP.

These can be two type common anode and common cathode. In common cathode type the cathode is common for all 8 LEDs and in common anode type the anode is common for all 8 LEDs. A seven segment display has 10 pin interface, 2 pins are for Common and 8 pins are for each LED. Both common pins are internally shorted.

1.2 Hardware required

S.No.ItemQuantity
1Raspberry Pi 1
2Pi Wedge B + 1
3Breadboard 1
4Seven Segment Display 1
5Jumper Male to male 10

2. Building Circuit

Schematic Diagram:

Circuit Layout:

3. Programming:

Once the circuit part is done, Raspberry Pi is needed to be programmed. Here is the python code to run this circuit.

You may download this code (Python code) from here.

import RPi.GPIO as GPIO
import time
import os, sys
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
#setup output pins
GPIO.setup(35, GPIO.OUT)      //GPIO19
GPIO.setup(12, GPIO.OUT)      //GPIO18
GPIO.setup(36, GPIO.OUT)      //GPIO16
GPIO.setup(33, GPIO.OUT)      //GPIO13
GPIO.setup(32, GPIO.OUT)      //GPIO12
GPIO.setup(38, GPIO.OUT)      //GPIO20
GPIO.setup(40, GPIO.OUT)      //GPIO21

#define 7 segment digits
digitclr=[1,1,1,1,1,1,1]
digit0=[0,0,0,0,0,0,1]
digit1=[1,0,0,1,1,1,1]
digit2=[0,0,1,0,0,1,0]
digit3=[0,0,0,0,1,1,0]
digit4=[1,0,0,1,1,0,0]
digit5=[0,1,0,0,1,0,0]
digit6=[0,1,0,0,0,0,0,]
digit7=[0,0,0,1,1,1,1]
digit8=[0,0,0,0,0,0,0]
digit9=[0,0,0,1,1,0,0,]
gpin=[35,12,36,33,32,38,40]
#routine to clear and then write to display
def digdisp(digit):
for x in range (0,7):
GPIO.output(gpin[x], digitclr[x])
for x in range (0,7):
GPIO.output(gpin[x], digit[x])
#routine to display digit from 0 to 9
digdisp(digit0)
time.sleep(1)
digdisp(digit1)
time.sleep(1)
digdisp(digit2)
time.sleep(1)
digdisp(digit3)
time.sleep(1)
digdisp(digit4)
time.sleep(1)
digdisp(digit5)
time.sleep(1)
digdisp(digit6)
time.sleep(1)
digdisp(digit7)
time.sleep(1)
digdisp(digit8)
time.sleep(1)
digdisp(digit9)
time.sleep(1)
#tidy up
GPIO.cleanup()
import sys
sys.exit()


4. Output

Python Coding with Raspberry Pi connects your project to the real world.The easiest introduction to Python is through IDLE, a Python development environment.

Open IDLE (Python Development Environment) from the Desktop or applications menu:

Python 3 is the newest version. Write the above code and save your code on desktop or any other location. Open Command Prompt. Write cd location_of_program > Press Enter.

Then write Python 3 file_name.py > Press Enter. (.py is file extension).

After run this code, Number 0 to 9 is displayed on seven segment display for one second each.

5. Troubleshooting:

If invert type number is displaying then the seven segment may be a common cathode type, in this case LED glows when pin is set to High.

If still not working pull the led out and test it may be defective

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

Leave a Reply