74HC595 connected to Arduino
Fig. 1

Interfacing Raspberry Pi 74HC595 Serial Shift Register

by Lewis Loflin

Here we look into connecting a 74HC595 8-bit serial shift register with 8-bit latch to a Raspberry Pi. Fig. 1 shows the internal configuration.

# MSB out first!
def ssrWrite(value):
    for  x in range(0,8):
        temp = value & 0x80
        if temp == 0x80:
           GPIO.output(dataBit, 1) # data bit HIGH
        else:
           GPIO.output(dataBit, 0) # data bit LOW
        pulseCLK()        
        value = value <<  0x01 # shift left
    serLatch() # output byte
    return 

Above is our serial shift subroutine ssrWrite(). It's nothing more than a for loop, a bitwise AND, and left-shift that sets or clears the dataBit pin, pulses HIGH then LOW the shift clock (pulseCLK), then after doing that 8 times pulses HIGH then LOW (serLatch) the latch pin. Here we shift out the most significant bit first.

74HC595 connected to 8 LEDs.
Fig. 2

Fig. 2 shows the LED connections. The LED pattern is simply a reproduction of the binary pattern shifted into the 8-bit register.

while 1:
    temp = 1
    for j in range(0, 8 ):
        ssrWrite(temp)
        temp = temp << 1
        time.sleep(.2)

    for j in range(0, 8 ):
        temp = temp >> 1
        ssrWrite(temp)
        time.sleep(.2)  

The main program turns on a single LED and moves it back and forth using left and right shift commands. This consists of an infinite while loop and two for loops.

The program code is rpi_cylon.txt.