Raspberry PI and 74hc595 python example

In this example we expand the amount of outputs using a 74HC595

The 74HC595; 74HCT595 are 8-stage serial shift registers with a storage register and 3-state outputs. The registers have separate clocks. Data is shifted on the positive-going transitions of the shift register clock input (SHCP). The data in each register is transferred to the storage register on a positive-going transition of the storage register clock input (STCP). If both clocks are connected together, the shift register will always be one clock pulse ahead of the storage register.

The shift register has a serial input (DS) and a serial standard output (Q7S) for cascading. It is also provided with asynchronous reset (active LOW) for all 8 shift register stages. The storage register has 8 parallel 3-state bus driver outputs. Data in the storage register appears at the output whenever the output enable input (OE) is LOW.

 

Schematic

PI and 595 schematic
PI and 595 schematic

Code

The code was written in python, save this as shifter.py.

import RPi.GPIO as gpio
from time import sleep
 
class Shifter():
 
	inputB=11
	clock=12
	clearPin=13
 
 
	def __init__(self):
		self.setupBoard()
		self.pause=0
	def tick(self):
		gpio.output(Shifter.clock,gpio.HIGH)
		sleep(self.pause)
		gpio.output(Shifter.clock,gpio.LOW)
		sleep(self.pause)		
 
	def setValue(self,value):
		for i in range(24):
			bitwise=0x800000>>i
			bit=bitwise&value
			if (bit==0):
				gpio.output(Shifter.inputB,gpio.LOW)
			else:
				gpio.output(Shifter.inputB,gpio.HIGH)
			Shifter.tick(self)
 
	def clear(self):
		gpio.output(Shifter.clearPin,gpio.LOW)
		Shifter.tick(self)
		gpio.output(Shifter.clearPin,gpio.HIGH)
 
	def setupBoard(self):
		gpio.setup(Shifter.inputB,gpio.OUT)
		gpio.output(Shifter.inputB,gpio.LOW)
		gpio.setup(Shifter.clock,gpio.OUT)
		gpio.output(Shifter.clock,gpio.LOW)
		gpio.setup(Shifter.clearPin,gpio.OUT)
		gpio.output(Shifter.clearPin,gpio.HIGH)
 
def main():
	pause=0.5
	gpio.setmode(gpio.BOARD)
	shifter=Shifter()
	running=True
	while running==True:
        	try:
			shifter.clear()
			shifter.setValue(1)
			sleep(1)
			shifter.clear()
			shifter.setValue(0x0AAAAAA)
			sleep(pause)
			shifter.clear()
			shifter.setValue(0x0555555)
			sleep(pause)
	        except KeyboardInterrupt:
        		running=False
 
 
if __name__=="__main__":
    main()

run this by typing in

sudo python shifter.py
SHARE
Previous articlePi and BMP180 sensor
Next articleRaspberry Pi and 74hc595 wiringPi example