The 7 or 8-segment display are a number of LEDs
Source: https://www.jameco.com/jameco/workshop/techtip/working-with-seven-segment-displays.html
You can control them as 7 separate LEDs But this is quite complexe "just" to display a digit. So there is another way to make the display is using a SHIFT REGISTER
SNx4HC595 8-Bit Shift Registers With 3-State Output Registers
The 74HC595 consists of an 8−bit shift register and a storage register with three−state parallel outputs. It converts serial input into parallel output so that you can save IO ports of an MCU. The 74HC595 is widely used to indicate multipath LEDs and drive multi-bit segment displays. "Three-state" mentioned above refers to the fact that you can set the output pins as either high, low or high impedance. With data latching, the instant output will not be affected during the shifting; with data output, you can cascade 74HC595s more easily. Compatible with low voltage TTL circuit, 74HC595 can transform serial input of 8-bit data into parallel output of 8-bit data. So it is often used to extend GPIO for embedded system and drive low power devices.
Features
Pins of 74HC595 and their functions:
The 74HC595 shift register is commonly used with microcontrollers or microprocessors to expand the GIPO functionalities. It requires only 3 pins connected to the MCU, which are Clock, Data and Latch. It has a wide operating voltage from 2V to 6V. An application circuit of the IC is shown below:
Connect
Input data in DS pin to the shift register when SH_CP (the clock input of the shift register) is at the rising edge, and to the memory register when ST_CP (the clock input of the memory) is at the rising edge. Then you can control the states of SH_CP and ST_CP via Raspberry Pi GPIO to transform serial input data into parallel output data so as to save Raspberry Pi GPIOs.
Now you should see a number flashing between 0 and 6 quickly on the segment display. Press the button on the breadboard, and the display will statically display a random number between 0 and 6 for 2 seconds and then circularly flash randomly between 0 and 6 again.
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
SDI = 11
RCLK = 12
SRCLK = 13
segCode = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x80]
def print_msg():
print 'Program is running...'
print 'Please press Ctrl+C to end the program...'
def setup():
GPIO.setmode(GPIO.BOARD) #Number GPIOs by its physical location
GPIO.setup(SDI, GPIO.OUT)
GPIO.setup(RCLK, GPIO.OUT)
GPIO.setup(SRCLK, GPIO.OUT)
GPIO.output(SDI, GPIO.LOW)
GPIO.output(RCLK, GPIO.LOW)
GPIO.output(SRCLK, GPIO.LOW)
def hc595_shift(dat):
for bit in range(0, 8):
GPIO.output(SDI, 0x80 & (dat << bit))
GPIO.output(SRCLK, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.output(RCLK, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(RCLK, GPIO.LOW)
def loop():
while True:
for i in range(0, len(segCode)):
hc595_shift(segCode[i])
time.sleep(0.5)
def destroy(): #When program ending, the function is executed.
GPIO.cleanup()
if __name__ == '__main__': #Program starting from here
print_msg()
setup()
try:
loop()
except KeyboardInterrupt:
destroy()