Skip to content

Instantly share code, notes, and snippets.

@mandyRae
Created April 20, 2016 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandyRae/7763e7cbe6f9166b259cab8b69efc076 to your computer and use it in GitHub Desktop.
Save mandyRae/7763e7cbe6f9166b259cab8b69efc076 to your computer and use it in GitHub Desktop.
'''
Large Seven-Segment Display
Python Code for controlling display with a Raspberry Pi's GPIO
Written by Amanda on electrothoughts.wordpress.com, Aug 25, 2015
Modify this code to your heart's content!
This code contains function definitions. To make your display work,
it's recommended that you run the code in the Python shell IDE as root, and execute
functions from there, for example enter the following in a terminal:
sudo python3 -i seven_segment_display.py
>>>flash(circles_flash, 0.5, 20)
>>>countdown()
Here's a simple graphical representation of the pin layout. There are three
LEDs per segment wired in parallel to one GPIO pin. All numbers and animations
are controlled at the segment level.
Segments are ordered as follows:
---1---
| |
6 2
| |
---7---
| |
5 3
| |
---4---
'''
import RPi.GPIO as GPIO, time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#Change your segment/pin assignment as necessary
seg1 = 17
seg2 = 27
seg3 = 22
seg4 = 6
seg5 = 13
seg6 = 26
seg7 = 19
buzzer = 21
segments = [seg1, seg2, seg3, seg4, seg5, seg6, seg7]
'''
Animaton 'Flash' Data
The display can display other animations other than numbers. For example,
the figure_eight_flash makes the the segments light up each individually and
consecutively in a figure-eight shape. In the flash function far below, these
lists of segments define the order of the animations. Experiment with segment
orders and make your own flashes. The circles_flash only has two frames: it has
four segments lit per frame.
'''
figure_eight_flash = [[seg1], [seg2], [seg7], [seg5], [seg4], [seg3], [seg7], [seg6]]
outside_flash = [[seg1], [seg2], [seg3], [seg4], [seg5], [seg6]]
center_flash = [[seg1], [seg7], [seg4], [seg7]]
circles_flash = [[seg1, seg2, seg6, seg7],[seg3, seg4, seg5, seg7]]
#GPIO setup lines
for seg in segments:
GPIO.setup(seg, GPIO.OUT)
GPIO.setup(buzzer, GPIO.OUT)
def buzz(buzzer, pitch, duration):
'''
buzzer: int, pin number of piezo buzzer
pitch: integer frequency in hertz
duration: float or int, duration of the buzz
This is a standard function for operating a piezo buzzer at a
certain frequency for a set duration.
'''
period = 1.0/pitch
delay = period/2
cycles = int(duration * pitch)
for i in range(cycles):
GPIO.output(buzzer, True)
time.sleep(delay)
GPIO.output(buzzer, False)
time.sleep(delay)
def clearDisplay():
'''
Turns off all LEDs and segments regardless of current state.
'''
for seg in segments:
GPIO.output(seg, False)
def lightSeg(seg_list):
'''
seg_list: list of segment(s) to be turned on
Clears display, then turns the indicated segments on.
'''
clearDisplay()
for seg in seg_list:
GPIO.output(seg, True)
def displayLetter(letter):
'''
Due to the nature of the seven-segment display,
only certain letters are available. If you try to
use a letter that's not possible, an error will be thrown.
Available letters: A, C, E, F, H, J, L, O, P, S, U, Y, Z
'''
available_letters = ['a', 'c', 'e', 'f', 'h', 'j', 'l', 'o', 'p', 's', 'u', 'y', 'z']
clearDisplay()
letter = letter.lower()
if letter not in available_letters:
print('Invalid letter, choose another')
lightSeg(segments)
time.sleep(0.5)
clearDisplay()
lightSeg(segments)
time.sleep(0.5)
lightSeg(segments)
if letter == 'a':
lightSeg([seg1,seg2,seg3,seg5,seg6,seg7])
if letter == 'c':
lightSeg([seg1, seg4, seg5, seg6])
if letter == 'e':
lightSeg([seg1, seg4, seg5, seg6, seg7])
if letter == 'f':
lightSeg([seg1, seg5, seg6, seg7])
if letter == 'h':
lightSeg([seg2, seg3, seg5, seg6, seg7])
if letter == 'j':
lightSeg([seg2, seg3, seg4])
if letter == 'l':
lightSeg([seg4, seg5, seg6])
if letter == 'o':
lightSeg([seg1, seg2, seg3, seg4, seg5, seg6, seg7])
if letter == 'p':
lightSeg([seg1, seg2, seg5, seg6, seg7])
if letter == 's':
lightSeg([seg1, seg3, seg4, seg6, seg7])
if letter == 'u':
lightSef([seg2, seg3, seg4, seg5, seg6])
if letter == 'y':
lightSeg([seg2, seg3, seg4, seg6, seg7])
if letter == 'z':
ligthSeg([seg1, seg2, seg4, seg5, seg7])
def displayWord(word, pause=1):
word = word.lower()
for char in word:
displayLetter(char)
time.sleep(pause)
def displayNumber(num):
'''
num: a string, '0' to '9'
Takes an integer in string format and display that number on the display
'''
if num == '0':
lightSeg([seg1, seg2, seg3, seg4, seg5, seg6])
if num == '1':
lightSeg([seg2, seg3])
if num == '2':
lightSeg([seg1, seg2, seg7, seg5, seg4])
if num == '3':
lightSeg([seg1, seg2, seg3, seg4, seg7])
if num == '4':
lightSeg([seg2, seg3, seg6, seg7])
if num == '5':
lightSeg([seg1, seg6, seg7, seg3, seg4])
if num == '6':
lightSeg([seg1, seg3, seg4, seg5, seg6, seg7])
if num == '7':
lightSeg([seg1, seg2, seg3])
if num == '8':
lightSeg([seg1, seg2, seg3, seg4, seg5, seg6, seg7])
if num == '9':
lightSeg([seg1, seg2, seg3, seg4, seg6, seg7])
def countdown(delay=1):
'''
delay: int or float, seconds between each number change, default is 1 sec,
must be >0.5
Displays numbers 9-0, holding each for the delay amount. The buzzer buzzes
as part of the countdown.
'''
clearDisplay()
for i in range(10, 0 ,-1): #for num 9 to 0, display that number
displayNumber(str(i))
buzz(buzzer, 256, 0.5) #buzz the buzzer at each number change
time.sleep(delay-0.5)
displayNumber('0')
buzz(buzzer, 660, 1) #final higher pitched '0' buzz
clearDisplay() #clear display when finished counting down
def flash(flash_data, speed, reps):
'''
flash_data: list or list of lists of the flash data, list of frames to display
speed: int or float, seconds between each frame
reps: int, number of time to repeat the animation
This function displays an animation defined by flash_data. It can display
both frames with one segment and with multiple segments.
'''
clearDisplay()
for i in range(reps):
for frame in flash_data: #for each frame, turn on all segments
for seg in frame:
GPIO.output(seg,True)
time.sleep(speed) #hold frame for the speed time
clearDisplay() #clear display for next frame
#Test/Trial Code: Uncomment this block to run a demo of display functions
countdown()
flash(figure_eight_flash, 0.25, 5)
flash(circles_flash, 0.25, 20)
flash(outside_flash, 0.1, 20)
flash(center_flash, 0.5, 5)
displayWord('ace')
clearDisplay()
time.sleep(0.5)
displayWord('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment