This tutorial of Robo India explains how to use Shift Register HC595 on Raspberry Pi.
1. Introduction:
This sequential device loads the data present on its inputs and then moves or “shifts” it to its output once every clock cycle, hence the name Shift Register.
A shift register is used for serial input and converting them to parallel output. In simple language it is used to increase output pins. Because it just used three pins as input and gives 8 ouput pins.
We are using 74HC595 Shift Register (by Robo India) in this tutorial. It requires three pin interfaces from NodeMCU:
1. Data pin: Data is sent in serial mode.
2. Clock Pin: A clock runs on this pin
3. Latch Pin: This pin is used to toggle so that shift register shows 8 bit data on output.
1.1 Hardware required
S.No. | Item | Quantity |
1 | Raspberry Pi | 1 |
2 | Pi Wedge B + | 1 |
3 | Bread Board | 1 |
4 | Shift Register Module by Robo India | 1 |
5 | Jumper Male to male | 5 |
2. Building Circuit
Schematic of circuit
Layout of circuit
3. Programming:
Once the circuit part is done, Raspberry Pi is needed to be programmed. Here is the python code to run this circuit.
You may download this code (Python Code) from here.
import RPi.GPIO as GPIO import time LATCH = 37 # GPIO 26 CLK = 22 # GPIO 25 dataBit = 13 # GPIO 27 GPIO.setup(LATCH, GPIO.OUT) GPIO.setup(CLK, GPIO.OUT) GPIO.setup(dataBit, GPIO.OUT) GPIO.output(22, 0) # Setup IO GPIO.output(CLK, 0) def pulseCLK(): GPIO.output(CLK, 1) # time.sleep(.01) GPIO.output(CLK, 0) return def serLatch(): GPIO.output(LATCH, 1) # time.sleep(.01) GPIO.output(LATCH, 0) return def ssrWrite(value): # MSB out first! for x in range(0,8): temp = value & 0x80 if temp == 0x80: GPIO.output(dataBit, 1) # data bit HIGH else: GPIO.output(dataBit, 0) pulseCLK() value = value << 0x01 # shift left serLatch() # output byte return def convBinary(value): # convert an 8-bit number to a binary string binaryValue = '0b' for x in range(0,8): temp = value & 0x80 if temp == 0x80: binaryValue = binaryValue + '1' else: binaryValue = binaryValue + '0' value = value << 1 return binaryValue 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)
4. Output
Python Coding with Raspberry Pi connects your project to the real world.The easiest introduction to Python is through IDLE, a Python development environment.
Open IDLE (Python Development Environment) from the Desktop or applications menu:
Python 3 is the newest version. Write the above code and save your code on desktop or any other location. Open Command Prompt. Write cd location_of_program > Press Enter.
Then write Python 3 file_name.py > Press Enter. (.py is file extension).
After executing the python code successfully, two LEDs start from LSBFIRST should blink, and then next two LEDs blink up to all 8 LEDs. This will continue in loop. Try changing values for different pattern of LEDs.
If you have any query please write us at support@roboindia.com
Thanks and Regards
Content Development Team
Robo India
https://roboindia.com