Controlling the Sure Electronics 3216 LED matrix with an Arduino UNO
The goal of this tutorial is to understand how it is possible to control a large number of LEDs using a simple Arduino UNO board; we’ll need to understand the language that the specific LED matrix by Sure Electronics “speaks” and learn how to communicate with it.
1 3216 Bicolor LED 3mm Dot Matrix Display - Sure Electronics
1 Arduino UNO board
1 Breadboard
A bunch of jumper wires of different length and colors
1. Inside The Matrix
The LED matrix has 16 rows and 32 columns of bicolor LEDs: red and green. Looking at the front it seems like it’s composed of 8 mini matrix 8x8 joined together, but this is not a fundamental aspect for our objective. What it’s important to notice instead is in the back: there are 4 microchips, each of which controls a part of the matrix, subdivided as in Figure 1.


Figure 1 Regions controlled by the microchips in the rear. Each chip controls a mini matrix 8 x 16.
Before we connect the hardware it would be appropriate to learn the logic behind the matrix. We can do that easily by focusing just on a mini matrix 8x16, and trying to understand how the microchip works and how we can make Arduino communicate with it.
Now, if you feel ready to learn how things really work go further, else if you just want to light up the whole thing go straight to chapter 3 (and come back later!)
2. Logic inside the chip
All the information here are taken from the user guide provided by sure electronics
http://www.sure-electronics.net/download/DE-DP14112_Ver1.1_EN.pdf
The main aim of the microchip is to translate the complex commands we send to it in a form like: “light the LED on the 3rd row and 5th column choosing the color red”.
Ok, but how to say exactly which LED has to light up? What’s the protocol behind the communication?
From the guide we understand that every information we want to send to the microchip is made of 3 clusters:
- the first consists of 3 bits in which we tell the microchip if we want to write or read from the RAM (the RAM configuration of the microchip corresponds to the state of each LED!) or execute more complex commands;
- then 7 bits for the address of the LED;
- finally 4 bits where we tell the microchip which configuration (a pattern) of LED light up.
Ok, one thing at a time, the last two sequences of bits need a further explanation.
The microchip’s behavior is not very intuitive at a first glance, due to the fact that it is trying to use the less number of bits to communicate the biggest number of informations possible.
The method used is the following:

Figure 2 Groups of led pointed by the number in the address bits (for 1 chip, just a 8x16 matrix)
The address specify a group of 4 led in column. From the 0 to 31 the light will be green, from 32 to 63 the reference is the same but the color is red. So this also explains how the chip manage different colors!
Ok, but I want to light a specific led, not 4!
Let’s see how:

Depending on the integer number (that we pass in its binary form) the microchip lights the LEDs corresponding to the 1 in the sequence of bits.
Examples:
Address = 0
pattern = 8

Address = 45
pattern = 5

Address = 45
pattern = 5
+
Address = 13
pattern = 2

Ok? Problems with decimal to binary convertion? Try this http://www.digitconvert.com/
Let’s get practice!
3. Arduino meets the matrix
As you probably already noticed we need to send to the matrix a series of 0 and 1, representing the address, the pattern and some other stuff we don’t need to get too much into.
If we look at the communication pins in the back of the matrix we can read some interesting names: cs, clk, data, wr. All the others are power signals (+5 V and GROUND) or useless (NC).
cs CHIP SELECT - select the microchip at which Arduino send the data to; by changing the chip we can control all the LEDs of the matrix with the method explained before.
clk CLOCK - the clock signal helps the microchip to sync with data sent from the Arduino, this clock is needed to sync the chip selection information
data DATA - the series of 0 and 1 that forms the information explained before
wr WRITE - sync the data information, it’s the clock for the data transmission
So, first of all connect this pins to some digital pins on the Arduino. In honor of Tim Gilmore, who wrote a terrific code to control this matrix, let’s use the same pins that he used for his code:
static const byte data = 6; // Data pin (pin 7) static const byte clk = 9; // Data pin (pin 2) static const byte wrclk = 7; // Write clock pin (pin 5) static const byte cs = 8; // Chip Select (pin 1)

After you’ve connected also the +5 V and the ground with the corresponding pins in the Arduino it’s “Hello world” time!
/*** Open the sketch "Step1" in the attachment at the end of the page. ***/
If the LED in the upper left lights up you can scream of joy, else check your wiring:

What have we just done?
Take a look at the lines of code inside the loop() section:
- we select the first microchip (try select the others! and also try select the chip 0 and the -1!!!);
- then we send the 3 clusters of information explained above;
- finally we just wait 10 seconds.
Taking a look at the function writebits we notice that it puts the WR pins LOW, then it makes the pins DATA oscillate HIGH and LOW corresponding to the data we want to send to the matrix, and at the end it puts again the WR pins HIGH.
So, we create digitally the "ones" and the "zeros" of the informations by switching the Arduino pins HIGH and LOW. If you want to know the protocol in details and understand deeper what you are doing, it’s better to read the guide of the matrix linked above. It’s more complicated than this tutorial but also more complete.
4. Having fun with the matrix
Now we know how to light up any of the LEDs in the matrix, but we are still far from done; actually we still have to specify the address and the pattern. So why not building a function who get as input the coordinates X / Y and return the lighting of the led?
Simply as follow:
void Plot (int X, int Y, int color)
{
// color:
// 0 -> green
// 1 -> red
// check the inputs value
// in case of error return nothing
if(color > 1 || color < 0)
return;
if(X<0 || X>31 || Y<0 || Y>15)
return;
// variables declaration
int x = 0;
int y = 0;
int nChip = 0;
// transform the X/Y coordinates to the number of the microchip
// that controls the region (1,2,3 or 4) the LED you want to
// light up
nChip = 1 + X/16 + (Y>7?2:0);
chipselect(nChip); // call the function chipselect to send the
// information to the matrix
// after the selection of the chip we need just the coordinate
// for 1 of them, so we need to change the coordinate system
x = X%16; // columns
y = Y%8; // rows
// from X/Y to address
address = ((x*2+y/4)) + color*32;
// from Y to pattern
pattern[address][nChip] = pattern[address][nChip] | ((1<<(3-y%4)));
writebits(HT1632_ID_WR, 2); // send ID: WRITE to RAM
writebits(address, 6); // Send address
writebits(pattern[address][nChip], 3); // send 4 bits of data
}
Depending on the input (X,Y and color) the function selects the chip, translates the coordinates in the two variables (address and pattern) and creates an array in which store all the led state (pattern itself).
As you can see color is an integer that can take only two values:
0 for green
1 for red
For the red color the function simply add the number 32 to the address value.
Now we can use this function to build up a simple game, maybe the simplest! The evergreen PONG!
/*** Open the sketch "Step2" in the attachment at the end of the page. ***/
The logic of the game is explained in the loop() section.
We call different functions:
- first of all we call logic(), in which we update the position of the ball, the points and check for the game over;
- once the variables are updated with new values, we call a series of draw functions to physically light the correspondent LEDs (using the function Plot, of course!);
- hence a little bit of delay useful to correct the speed game, and finally we clear the matrix.
What happens if we forget to clear the matrix?
Try yourself!
How to control the game?
Why not connecting two simple potentiometers in the analog inputs of the Arduino board?
Ok, from now on it’s up to your creativity!

5. What about writing stuff?
Of course it’s possible to write letters on the matrix.
To do so you can pass to the plot function the coordinates describing the pattern of a letter shape.
Or... you can organize letters, numbers and symbols in a working library, as a team of amazing guys released here: http://www.arduino.cc/playground/Main/HT1632C

| Attachment | Size |
|---|---|
| arduino-3216ledmatrix.zip | 10.82 KB |

Comments