Lesson 13 – Driving LED’s using 74HC595 IC
Booting up the Raspberry Pi
- This section assumes that you have read through “Lesson 0 – Setup” and have a working Raspberry Pi you can connect to remotely using SSH or VNC.
- If that’s not the case please head back to “Lesson 0 – Setup” and work through the instructions provided.
- Let’s now get started and boot up the Raspberry Pi.
- Grab a good quality USB cable and a USB power adaptor (2.5A).
- Plug one end of the USB cable into the plug and the other microUSB end into the Raspberry Pi.
- This should now power up the Raspberry Pi.
- Once the Raspberry Pi has booted up, please ensure that it is able to connect it to the network so that you can access it over VNC.
- If you are using a local monitor connected to the Raspberry Pi, you are all sorted.
- Else get connected to the Raspberry Pi using VNC.
Let’s Get Started
- Our next tutorial is called, “Driving LED’s using 74HC595 IC”.
- We will start off this tutorial by first putting together the circuit using a breadboard.
- Components required for this circuit include –
- 1 * Raspberry Pi
- 1 * Breadboard* T-Extension Board
- 1 * 74HC595
- 8 * LED
- 8 * Resistor (220Ω)
- Jumper wires
- 1 * T-Extension Board
- 1 * 40-Pin GPIO Cable
- The below diagram provides a view of what the circuit should look like.
- Click on the following <Link> to open up the tutorial.
- Step through the instructions provided and put together the circuit.
- Once you have put together the circuit, open up the python editor and start writing your code.
PLEASE NOTE – Please make sure you have disconnected your breadboard from the Raspberry Pi before commencing build of the circuit. Once you have put the circuit together, get someone around you to review the circuit and confirm that the connections are proper before you proceed and power up the breadboard.
Let’s Write Some Python Code
Open up the Thonny editor on your Raspberry Pi and let’s start putting together some code……
#!/usr/bin/env python #================================================ # # This program is for SunFounder SuperKit for Rpi. # # Extend use of 8 LED with 74HC595. # # Change the WhichLeds and sleeptime value under # loop() function to change LED mode and speed. # #================================================= import RPi.GPIO as GPIO import time SDI = 17 RCLK = 18 SRCLK = 27 #=============== LED Mode Defne ================ # You can define yourself, in binay, and convert it to Hex # 8 bits a group, 0 means off, 1 means on # like : 0101 0101, means LED1, 3, 5, 7 are on.(from left to right) # and convert to 0x55. LED0 = [0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80] #original mode BLINK = [0xff,0x00,0xff,0x00,0xff,0x00] #blink LED1 = [0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff] #blink mode 1 LED2 = [0x01,0x05,0x15,0x55,0xb5,0xf5,0xfb,0xff] #blink mode 2 LED3 = [0x02,0x03,0x0b,0x0f,0x2f,0x3f,0xbf,0xff] #blink mode 3 #================================================= def print_message(): print ("========================================") print ("| LEDs with 74HC595 |") print ("| ------------------------------ |") print ("| SDI connect to GPIO 0 |") print ("| RCLK connect to GPIO 1 |") print ("| SRCLK connect to GPIO 2 |") print ("| |") print ("| Control LEDs with 74HC595 |") print ("| |") print ("| SunFounder|") print ("========================================\n") print 'Program is running...' print 'Please press Ctrl+C to end the program...' raw_input ("Press Enter to begin\n") def setup(): GPIO.setmode(GPIO.BCM) # Number GPIOs by its BCM location GPIO.setup(SDI, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(RCLK, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(SRCLK, GPIO.OUT, initial=GPIO.LOW) # Shift the data to 74HC595 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 main(): print_message() mode = LED0 # Change Mode, modes from LED0 to LED3 sleeptime = 0.15 # Change speed, lower value, faster speed blink_sleeptime = 0.3 leds = ['-', '-', '-', '-', '-', '-', '-', '-'] while True: # Change LED status from mode print " mode" for onoff in mode: hc595_shift(onoff) leds[mode.index(onoff)] = 1 # Show which led is on print leds time.sleep(sleeptime) leds[mode.index(onoff)] = '-' # Show the led is off print " blink" for onoff in BLINK: hc595_shift(onoff) if (onoff == 0x00): leds = ['-'] * 8 elif (onoff == 0xff): leds =  * 8 print leds time.sleep(blink_sleeptime) # Change LED status from mode reverse print " reversed mode" for onoff in reversed(mode): hc595_shift(onoff) leds[mode.index(onoff)] = 1 # Show which led is on print leds time.sleep(sleeptime) leds[mode.index(onoff)] = '-' # Show the led is off print " blink" for onoff in BLINK: hc595_shift(onoff) if (onoff == 0x00): leds = ['-'] * 8 elif (onoff == 0xff): leds =  * 8 print leds time.sleep(blink_sleeptime) def destroy(): GPIO.cleanup() if __name__ == '__main__': setup() try: main() except KeyboardInterrupt: destroy()
About the Raspberry Pi
The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.
The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.
You can read more about the Raspberry Pi here – RaspberryPi.org.
- This development track is based on the Rasbperry Pi and the SunFounder Super Starter Kit v3.0 for the Raspberry Pi.
- You will need access to both the Raspberry Pi 3 B and the electronics components part of the SunFounder Super Starter Kit v3.0 for the Raspberry Pi kit to be able to work on these tutorials.
- If you haven’t purchased the Raspberry Pi 3 B yet please head over to our store and purchase one now. You can pick up the SunFounder Super Starter Kit v3.0 for the Raspberry Pi from SunFounder’s website.
- Depending on where you live you might also be able to pick up the Raspberry Pi and SunFounder Super Starter Kit v3.0 for the Raspberry Pi at your local electronics hobby store.
- You can read more about the Raspberry Pi here – RaspberryPi.org.