Step 8Project 3[pt 2]: '2 Wire' 4x4 led matrix display
bit 1 = column 1 (rightmost)
bit 2 = column 2
bit 3 = column 3
bit 4 = column 4
bit 5 = row 1 (topmost)
bit 6 = row 2
bit 7 = row 3
bit 8 = row 4
To make an image draw out a 4x4 square on graph paper and fill in which ones you want displayed, next make a YX table. Below you will see a mapping for a simile, well as best one can do on 4x4 "pixels"
For each filled in section I write down which column (Y) it is in, then which row it is in (X)
Now open up the _4x4.pde file in the arduino IDE you will see our old 2 friends
#define data 2
#define clock 3
then a array of integers
int img[] = {1,1,4,1,1,3,4,3,2,4,3,4};
If you look its just a list of my written down YX coordinates, it would be a big pain in the butt to convert those values by hand, and we have a computer ... let it do it!
Moving on there is void setup where we make our clock and data pins OUTPUTS
void setup()
{
pinMode(clock, OUTPUT); // make the clock pin an output
pinMode(data , OUTPUT); // make the data pin an output3
}
And a confusing looking void loop, to start things off we need to declare some local variables
void loop()
{
int Y;
int X;
byte out;
Then a for loop, this loop needs to be as long as the amount of entries in the img array, for this image I only used 6 pixels, so that makes 12 YX coordinates. I make it skip every other number by using i +=2, because we read 2 coordinates per loop
for(int i = 0; i < 12; i += 2) // number of points in the img array, this case 12
{
Now we read the Y entery at [i] in the array, and subtract one from its value, because bytes don't start at one, they start at zero, but we counted from 1
// get the first pair of YX cords
Y = (img[i] - 1); // subtract one since the bit count starts at 0
Next we read the X entery at [i + 1] in the array, and subtract one from its value, because of the same reason
X = (img[i+1] - 1);
After we have the YX values of the pixel, we do some bitwise or math and shifting to the left.
First we need to read the X value, and whatever its value is shift it that many places + 4 left, so if X is 4 and add 4 it is bit 8 (MSB), looking at the chart again ...
bit 1 = column 1 (rightmost)
bit 2 = column 2
bit 3 = column 3
bit 4 = column 4
bit 5 = row 1 (topmost)
bit 6 = row 2
bit 7 = row 3
bit 8 = row 4
Bit 8 is the last row
Next the Y value is also shifted to the left, this time just by its self, nothing added on.
Finally the two are or'ed together into 1 byte instead of 2 half bytes (nibbles), using bitwise or (the symbol | ) takes two bytes and basicly adds them together, lets assume
X = 10000000
Y = 00000001
--------------------
OR =10000001
row 4 column 1
out = 1 << (X + 4) | 1 << Y;
And finally shiftOut to display the current picture, and keep doing that until we have no more data in the array ... delay a moment and loop forever, since we were shifting data to the left and we need the MSB to be on the last output pin of the shift register send it out first.
shiftOut(data, clock, MSBFIRST, out); // shift the byte out to our register
delay(1); // delay it abit so it has a chance to leave a spot of light in your eyes
Feel free to make your own images, and effects, There are 3 sample files, the smiley face and a checkerboard (which looks more like stripes), and finally a random sparkle maker
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|



















![Project 3[pt 2]: \](/image/F0YEB01G1ZGOSXN/Project-3pt-2-2-Wire-4x4-led-matrix-display.jpg)






























