Lesson 15 – Driving Dot Matrix LEDs with 74HC595 IC

Booting up the Raspberry Pi

  1. 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.
  2. If that’s not the case please head back to “Lesson 0 – Setup” and work through the instructions provided.
  3. Let’s now get started and boot up the Raspberry Pi.
  4. Grab a good quality USB cable and a USB power adaptor (2.5A).
  5. Plug one end of the USB cable into the plug and the other microUSB end into the Raspberry Pi.
  6. This should now power up the Raspberry Pi.
  7. 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.
  8. If you are using a local monitor connected to the Raspberry Pi, you are all sorted.
  9. Else get connected to the Raspberry Pi using VNC.

Let’s Get Started

  1. Our next tutorial is called, “Driving Dot Matrix LEDs with 74HC595 IC”.
  2. We will start off this tutorial by first putting together the circuit using a breadboard.
  3. Components required for this circuit include –
    1. 1 * Raspberry Pi
    2. 1 * Breadboard* T-Extension Board
    3. 2 * 74HC595
    4. 1 * Dot-Matrix
    5. Jumper wires
    6. 1 * T-Extension Board
    7. 1 * 40-Pin GPIO Cable
  4. The below diagram provides a view of what the circuit should look like.
  5. Click on the following <Link> to open up the tutorial.
  6. Step through the instructions provided and put together the circuit.
  7. Once you have put together the circuit, open up the python editor and start writing your code.

Step 1 –

 

Step 2 –

 

Step 3 –

 

Step 4 –

 

PLEASE NOTEPlease 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

import RPi.GPIO as GPIO
import time

SDI   = 17
RCLK  = 18
SRCLK = 27

# we use BX matrix, ROW for anode, and COL for cathode
# ROW  ++++
code_H = [0x01,0xff,0x80,0xff,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff]
# COL  ----
code_L = [0x00,0x7f,0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f]

def print_msg():
    print ("========================================")
    print ("|      Dot matrix with two 74HC595     |")
    print ("|    ------------------------------    |")
    print ("|        SDI connect to GPIO 0         |")
    print ("|        RCLK connect to GPIO 1        |")
    print ("|        SRCLK connect to GPIO 2       |")
    print ("|                                      |")
    print ("|   Control Dot matrix 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 print_matrix(matrix):
    for i in xrange(0,len(matrix)):
        print matrix[i]

def get_matrix(row_buffer, col_buffer, max_row=8, max_col=8):
    matrix_msg = [[0 for i in range(max_row)] for i in range(max_col)]
    
    print "row_buffer = 0x%02x , col_buffer = 0x%02x"%(row_buffer, col_buffer)
    for row_num in xrange(0,8):         
        for col_num in xrange(0,8):
            #print (row_num, col_num), '-->', (((row_buffer >> row_num) & 0x01), ((col_buffer >> col_num) & 0x01))
            if (((row_buffer >> row_num) & 0x01) - ((col_buffer >> col_num) & 0x01)):
                matrix_msg[row_num][col_num] = 1
    print_matrix(matrix_msg)
    matrix_msg = [[0 for i in range(max_row)] for i in range(max_col)]

def setup():
    GPIO.setmode(GPIO.BCM)    # Number GPIOs by its BCM location
    GPIO.setup(SDI, GPIO.OUT)
    GPIO.setup(RCLK, GPIO.OUT)
    GPIO.setup(SRCLK, GPIO.OUT)
    GPIO.output(SDI, GPIO.LOW)
    GPIO.output(RCLK, GPIO.LOW)
    GPIO.output(SRCLK, 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_msg()
    while True:
        for i in range(0, len(code_H)):
            hc595_shift(code_L[i])
            hc595_shift(code_H[i])
            get_matrix(code_L[i], code_H[i])
            time.sleep(0.1)

        for i in range(len(code_H)-1, -1, -1):
            hc595_shift(code_L[i])
            hc595_shift(code_H[i])
            get_matrix(code_L[i], code_H[i])
            time.sleep(0.1)

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.

Prerequisites

  1. This development track is based on the Rasbperry Pi and the SunFounder Super Starter Kit v3.0 for the Raspberry Pi.
  2. 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.
  3. 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.
  4. 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.
  5. You can read more about the Raspberry Pi here – RaspberryPi.org.

Questions