Introduction: MAX7219 - 8 Digit LED Display Module Driver for ESP8266

MAX7219 8 Digit LED Display Module is one of the most popular and widely available display module, used already in many 8/16/32 bit MCU Projects, now is time to see it working also in the ESP8266 CBDBv2 EVO ecosystem :)

Step 1: Description

The MAX7219 are compact, serial input/output common-cathode
display drivers that interface microprocessors (µPs) to 7-segment numeric LED displays of up to 8 digits, bar-graph displays, or 64 individual LEDs. Included on-chip are a BCD code-B decoder, multiplex scan circuitry, segment and digit drivers, and an 8x8 static RAM that stores each digit. Only one external resistor is required to set the segment current for all LEDs.

A convenient 4-wire serial interface connects to all common µPs. Individual digits may be addressed and updated without rewriting the entire display. The MAX7219 also allow the user to select code-B decoding or no-decode for each digit.

The devices include a 150µA low-power shutdown mode, analog and digital brightness control, a scan-limit register that allows the user to display from 1 to 8 digits, and a test mode that forces all LEDs on.

Step 2: Key Features & Application Circuit

  • 10MHz Serial Interface
  • Individual LED Segment Control
  • Decode/No-Decode Digit Selection
  • 150µA Low-Power Shutdown (Data Retained)
  • Digital and Analog Brightness Control
  • Display Blanked on Power-Up
  • Drive Common-Cathode LED Display
  • 24-Pin DIP and SO Packages



This is a 5V operation device. If you need to run it by the book at 3.3V Logic Level you will
need to use a level shifter.

In practice, as you will see below, you can try and run it directly, a bit out of spec.

As MAX7219 HIGH logic level is at 3.5V...well...looks like it's working quite OK also in this way, in 48 hours of continuous running no freeze or strange behaviour:)

For more details please see MAX7219 Datasheet

For a true native 3V operation or segment blinking, i will recommend you to look at the MAX6951 datasheet.

Step 3: What We Will Need



Connections:


Wire  MAX7219  ESP8266
Green  +5Vcc  -
Blue  GND  GND
Yellow  DIN   13
White  CS  12
Orange  CLK  14

Step 4: MAX7219 Driver Implementation

For a detailed technical description of the MAX7219 operation please take a look on the related article and/or datasheet.



1. Initialisation

-- MAX7219 registers
MAXREG_DECODEMODE = 0x09
MAXREG_INTENSITY  = 0x0a
MAXREG_SCANLIMIT  = 0x0b
MAXREG_SHUTDOWN   = 0x0c
MAXREG_DISPTEST   = 0x0f 
DIN   = 7      -- 13 - data in pin
CS    = 6      -- 12 - load (CS) pin
CLK   = 5      -- 14 - clock pin
gpio.mode(DIN,gpio.OUTPUT)
gpio.mode(CS,gpio.OUTPUT)
gpio.mode(CLK,gpio.OUTPUT)


2. Write serialised data

function wrByte(data)
   i=8
   while (i>0)  
   do
       mask = bit.lshift(0x01,i-1)
       --print(mask)
       gpio.write( CLK, 0)    -- tick
       dser = bit.band(data,mask)
       if (dser > 0) 
         then gpio.write(DIN, 1)   -- send 1
              --print("1")
         else gpio.write(DIN, 0)   -- send 0
              --print("0")
       end --endif
       --print(dser)
       gpio.write( CLK, 1)    -- tick
       i=i-1
    end --while
end


3. Set Register

function setReg(reg, value)
  gpio.write(CS, 0)

  wrByte(reg)   -- specify register
  tmr.delay(10)
  wrByte(value) -- send data

  gpio.write(CS, 0)
  --tmr.delay(10)
  gpio.write(CS, 1)
end


4. Convert anf Print integer number in xxxx format

 function print_led_int(c) 
   th = string.format("%d",c / 1000)
   h = string.format("%d",(c-th*1000) / 100)
   t = string.format("%d", (c-th*1000-h*100) / 10)
   u = string.format("%d", c-th*1000-h*100-t*10)
   --print(string.format("%d %d %d %d", th,h,t,u))
   setReg(4, th)
   setReg(3, h)
   setReg(2, t)
   setReg(1, u)
end


5. Create a Display 'ZERO' init stage

function zero_all()
   v=1
   while (v<9) do
       setReg(v,0)
    v=v+1
   end
end 


6. MAX7219 Initialisation

setReg(MAXREG_SCANLIMIT, 0x07)
tmr.delay(100)
setReg(MAXREG_DECODEMODE, 0xFF)    -- full decode mode BCD
tmr.delay(100)
setReg(MAXREG_SHUTDOWN, 0x01)          -- not in shutdown mode
tmr.delay(100)
setReg(MAXREG_DISPTEST, 0x00)              -- no display test
tmr.delay(100)
setReg(MAXREG_INTENSITY, 0x00)            -- set Brightness
zero_all()                                -- set all to ZERO


7. Test Display - 9999 counter

count=0
tmr.alarm(0,1000,1, 
   function()  
       count=count+1; 
       --print(count); 
       print_led_int(count) 
       if (count>9999) then count=0;zero_all() 
       end 
   end)

Step 5: Testing and Running

For testing, just save the code on ESP as 'max7219.lua', restart ESP and run:

dofile("max7219.lua")       -- Start the MAX7219 Module Program<br>

If you want your MAX7219 Display Driver to start automatically when your
ESP8266 module starts or reboots, then you neet to create and add some lines in your 'init.lua' file:

dofile("max7219.lua")       -- Start the MAX7219 Module Program

Save the code on ESP as 'init.lua', restart ESP. It should reboot and restart automatically the program .