MAX7219 Display driver internal block diagram.

Raspberry Pi and the 8-Digit LED MAX7219 Display Driver

by Lewis Loflin


The program presented below will allow Raspberry in Python and setup a MAX7219 display driver to act as a four digit counter. The program was ported over from Arduino C to illustrate how coding can be reused.

The MAX7219 display driver can drive 8 digit multiplexed LED display or a 8X8 LED matrix. This is setup in software. It includes selectable internal binary-coded-decimal (BCD) decoding which is used here.

It can also be cut on/off by a single command, the number of digits displayed, and intensity are all selectable in software. See "def initMAX7219()" below.

Refer to the block diagram above. 16-bit data input is broken into 2 8-bit data bytes the first being an address and the second being data. Using the function writeMAX7219(digit, k) digit is the digit value and k is position pointer.

For example assuming BCD mode and digit = 1 and k = 1 a one will be displayed on the far right of the display. writeMAX7219(digit, k) uses ssrOut() twice.

BCD code is limited to 0-9. Here we take a for loop where i counts from 0-999. Each iteration the value of i is sent to j which the MOD function j with 10 returns remainder value of 0-9.

The position and value are output to the display. The j is then divided by 10. This done 4 times to cover all four digits. Then i will increment and the process will begin again.

In the next section we will use the Python time and datetime function to create a LED real time clock.

See Raspberry Pi Python RTC with MAX7219 Display Driver

#!/usr/bin/env python
# File
# By Lewis Loflin -

# Here we connect a MAX7219 8-digit module to display a 
# count from 0-9999 after each digit converted to BCD format.
# The loop can be ended before count is finished by pressing Sw1.

# Two bytes are shifted in first being address, second being data.
# Works the same as two 74165 SSRs in series or 16-bits.
# LD "pulseCS()" clocks 16-bit address/data into working registers.

# access to GPIO must be through root
import RPi.GPIO as GPIO
import time

LATCH = 11 # CS
CLK = 12
dataBit = 7 # DIN

GPIO.setup(CLK, GPIO.OUT) # P1 
GPIO.setup(dataBit, GPIO.OUT) # P7

# Setup IO
GPIO.output(11, 0)
GPIO.output(CLK, 0)

def pulseCLK():
    GPIO.output(CLK, 1)
    # time.sleep(.001) 
    GPIO.output(CLK, 0)

def pulseCS():
    GPIO.output(LATCH, 1)
    # time.sleep(.001)
    GPIO.output(LATCH, 0)

# shift byte into MAX7219
# MSB out first!
def ssrOut(value):
    for  x in range(0,8):
        temp = value & 0x80
        if temp == 0x80:
           GPIO.output(dataBit, 1) # data bit HIGH
           GPIO.output(dataBit, 0) # data bit LOW
        value = value << 0x01 # shift left       

# initialize MAX7219 4 digits BCD
def initMAX7219():
    # set decode mode
    ssrOut(0x09) # address
    #	ssrOut(0x00); // no decode
    ssrOut(0xFF) # 4-bit BCD decode eight digits

    # set intensity
    ssrOut(0x0A) # address
    ssrOut(0x04) # 9/32s

    # set scan limit 0-7
    ssrOut(0x0B); # address
    ssrOut(0x07) # 8 digits
    # ssrOut(0x03) # 4 digits

    # set for normal operation
    ssrOut(0x0C) # address
    # ssrOut(0x00); // Off
    ssrOut(0x01)  # On
	# clear to all 0s.
    for x in range(0,9):

def writeMAX7219(data, location):

def displayOff():
   # set for normal operation
    ssrOut(0x0C) # address
    ssrOut(0x00); # Off
    # ssrOut(0x01)  # On

def displayOn():
   # set for normal operation
    ssrOut(0x0C) # address
    # ssrOut(0x00); # Off
    ssrOut(0x01)  # On


# Converts four digits in i into 4 BDC bytes.
# The writes digits to MAX7219.

for i in range(0, 9999):
    j = i
    # get 1st digit j
    for k in range(1, 4):
		digit = j % 10
		writeMAX7219(digit, k)
		j = j / 10 


print "Good by!"  


Optical Isolation of H-Bridge Motor Controls YouTube
Optical Isolation of H-Bridge Motor Controls

Opto-Couplers Theory and Circuits YouTube
Opto-Isolated Transistor Drivers for Micro-Controllers

All NPN Transistor H-Bridge Motor Control YouTube
All NPN Transistor H-Bridge Motor Control

Pulse-Width Modulation Tutorial YouTube
Pulse-Width Modulation Tutorial

PIC12F683 Microcontroller and Circuits YouTube
PIC12F683 Microcontroller and Circuits