Introduction: Controlling an LED Matrix
LED arrays are lots of fun, and easier than you may think to control. In this Instructable, you will learn how a LED matrix is built and works and how to drive it using a MAX7219 driver chip. We will be using an Intel Edison with Arduino breakout board to program static images and animations.
After learning the basics, check out the resources page, where you can build a binary clock, a game of Tetris and more!
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Gather Materials
Step 2: How an LED Matrix Works
An LED array is a collection of LEDs, they can be arranged in many patterns, the most typical being a single line, or a matrix pattern. Here we cover the matrix pattern. A matrix defines a rectangular or square consisting of intersecting rows and columns. The intersection of these rows and columns are important to how we talk to the matrix and address individual LEDs.
Let’s start by looking at an LED, there is a negative (cathode) terminal that goes towards ground, and a positive (anode) one that goes towards power. The power terminal gets connected to a microcontroller I/O pin, which will pump out voltage, turning it on and off based on the uploaded program. The anodes and cathodes are connected, forming the columns and rows. A LED matrix can either be column anode or column cathode (like in the diagram above).
If we wanted to be able to control each LED individually, every LED in a matrix would need it’s own I/O pin. A 8x8 matrix would need 64 pins. That’s a lot! Luckily there is something called multiplexing which reduces the number of pins needed to 16 in order to control the matrix. The pins can then be referred by the row number or column number, for example, Row 1-8 (R1 - R8) or Column 1-8 (C1 - C8).
Multiplexing works by turning on LEDs in one row or column at a time, for simplicity’s sake, lets say we are turning on rows. Each row is turned on and off right after one another at a rate quick enough that the persistence of vision causes us to see a static image.
Step 3: Wiring
If a multiplexed matrix is wired up to the microcontroller, each of the 16 pins on the matrix will need to be wired to 16 digital I/O pins. No matter what dimension the matrix is, those pins add up. This is where the MAX7219 driver chip comes in handy. It takes all 16 pins of an 8x8 matrix and reduces the number the pins connected to a microcontroller to 3, the data in, Load (CS) and clock pin.
Identifying Pins on Matrix
Before the matrix gets connected to the driver chip, you need to identify which pins correspond to the columns and rows that each LED intersects at. We can find that out by looking at the datasheet for the LED matrix being used. It helps to put a piece of tape along the right and bottom edge of the matrix and then write down what the pin corresponds to.
Connecting MAX7219 to Matrix
Once the pins are identified, they can be connected to the driver chip. Looking at the datasheet, we can see there are Seg and Dig pins. The Seg pins get connected to the anodes, the Dig pins connect to the cathodes. Like the matrix, the Seg pins aren't necessarily in sequential order when looking at the chip pins, each pin needs to be identified as which Seg and Dig pin they are. To make it easier, I've identified and mapped the pins of the driver chip the pin number of the matrix for you. See the image above and make the connections using jumper wires.
Connecting MAX7219 to Edison
Connect these 3 pins to the Arduino breakout board:
Driver pin 4 and 9 -> GND
Driver pin 19 -> +5 volts
Driver pin 12/Load (CS) --> 10
Driver pin 13/Clock --> 11
Driver pin 1/Data In --> pin 12
If connecting multiple matrices, daisy chain drivers by connecting the driver pin 24/Data Out to pin 1/Data In on the next driver chip.
Step 4: Code
Before uploading the program, the LedControl.h library needs to downloaded and put in the Arduino/libraries directory.
To turn on a LED, either the integer 0 or 1 needs to be sent through software. The pattern can be thought of a grid, each LED or pixel that needs to be turned on corresponds with the row and column set up of the LED matrix. Any LEDs that need to be turned on is assigned a 1, LEDs that are turned off are assigned a 0. Above you will find the patterned logic used to display an asterisk.
Remember, we are working with a column cathode matrix, so we will be sending the rows the data. If one row is thought of as an array, you can turn on R1 by sending the binary logic:
R1 : 10011001
You can then turn on rows 2 – 8 by sending the following:
R2 : 01011010
R3 : 00111100
R4 : 11111111
R5 : 11111111
R6 : 00111100
R7 : 01011010
R8 : 10011001
By turning on and off LEDs in a grid design, any static image can be made, within the matrix's resolution constraints. What if you wanted to create an animation? It's easier than you may think, each frame of the animation just needs to be thought of as a static image. Load up a function with arrays that holds each frame of the animation, when called your design will play. To dictate the frame rate, place a delay() after each frame with the value in milliseconds.
Download the attached example code and go to the next step to learn how to upload to the Edison. The program displays a static bang(!), asterisk (*) and heart and an animated exploding heart.
Step 5: Uploading to Edison
Intel has extensive documentation on their website for the Edison and Galileo. Below are listed some of the main steps to be aware of when preparing to upload a program to the Edison board with links to relevant pages. For more details and documentation, begin at Intel’s Get Started page. There you will learn how to assemble hardware, download software and find demos and links providing example code.
Get your Edison flashed with the latest firmware and learn how to connect to it through the USB serial port. To upload Eclipse you will also need to connect it to a WiFi network, with Arduino it’s not necessary. Download Intel’s Arduino IDE , when you open it you will see the Intel Edison and Galileo boards under the Tools menu. An understanding of Arduino is recommended before getting started with Intel’s version.
Upload your first blink sketch to the Edison
To upload a program via Eclipse, the Edison board needs to be connected to a network and your computer needs to be connected to the same network. After flashing the latest firmware and connecting to the USB serial port, visit Intel's Get Started page and choose how you want to get your board online.
If you get stuck, the best place for help with troubleshooting is Intel’s forum.
Step 6: Going Further
LedControl : Arduino.cc
MAX7219 : Arduino.cc
There is another way that matrices can be configured in, other than multiplexed that uses even fewer pins. This is called Charlieplexing. To learn more about what it is and how to hook one up, start with the Make: article on this very subject.