Using Shift Register 74HC595 with Raspberry Pi

In the previous article we were talking about this device and the advantages that we could have using it. But it may be a little difficult to understand without seeing an example or without understanding its operation and usage modes.
Today we will be appling it using all the pins that this device provides us with a simple example using a code library open to everyone, made by Mecany and hosted on Github.

Requirements:

  • 1 Raspberry Pi
  • 1 Breadboard
  • 8 Leds
  • 8 Resistor 220 Ω
  • 17 Jumper Wires
  • 1 74HC595

Installing required library:

We can obtain a simple library to manage this shift registry from github:
In the same directory that you have the python script you need to do:

git clone git@github.com:marsminds/shiftr_74HC595.git

The procedure:

  • Before of connecting the Raspberry Pi to the power we need to build the circuit like the diagram is showing.

 

  • Connect the power in the Raspberry Pi
  • Save the code in a file called shift_register.py
import RPi.GPIO as GPIO
from shiftr_74HC595.shiftr_74HC595 import ShifRegister #importing the downloaded class
from time import sleep
GPIO.setmode(GPIO.BOARD)
data_pin = 7 #pin 14 on the 75HC595
latch_pin = 11 #pin 12 on the 75HC595
clock_pin = 12 #pin 11 on the 75HC595
shift_register = ShifRegister(data_pin, latch_pin, clock_pin) #using the class to manage the shift register
try:
    while 1:
        #setting the pins to turn on or off the leds
        shift_register.setOutput(0, GPIO.HIGH)
        shift_register.setOutput(1, GPIO.LOW)
        shift_register.setOutput(2, GPIO.LOW)
        shift_register.setOutput(3, GPIO.LOW)
        shift_register.setOutput(4, GPIO.HIGH)
        shift_register.setOutput(5, GPIO.LOW)
        shift_register.setOutput(6, GPIO.LOW)
        shift_register.setOutput(7, GPIO.HIGH)
        sleep(1)
        #setting the pins to turn on or off the leds
        shift_register.setOutput(0, GPIO.LOW)
        shift_register.setOutput(1, GPIO.HIGH)
        shift_register.setOutput(2, GPIO.HIGH)
        shift_register.setOutput(3, GPIO.HIGH)
        shift_register.setOutput(4, GPIO.LOW)
        shift_register.setOutput(5, GPIO.HIGH)
        shift_register.setOutput(6, GPIO.HIGH)
        shift_register.setOutput(7, GPIO.LOW)
        sleep(1)
except KeyboardInterrupt:
    print “Ctrl-C – quit”
GPIO.cleanup() #cleaning all the GPIO pins when the script is finished

 

  • Execute the file using the following command in the terminal:
sudo python shift_register.py