Example Code for Arduino

Last revision 2025/12/26

The article offers example Arduino code for managing 8x8 LED matrices, detailing initialization, individual LED control, creating dynamic patterns, and utilizing the led_8x8.h library.

Software Preparation

Development platform: Arduino;
Required library: led_8x8.h (put the led_8x8.h in the same file as the sample codes)

Sample Code

led_8x8.h

/**************** start of led_8x8.h ********************/
/*
 *  created: 2013-06-14
 *  by: adrian
 *
 */
#include <Arduino.h>

const uint8_t row[8] = {9, 8,  4, 17, 3, 10, 11,  6};
const uint8_t col[8] = {2, 7, 19, 5, 13, 18, 12, 16};

//init 8x8 led
void led_init (void) {
    for (int pin = 0; pin<8; pin++)  {
        pinMode (row[pin], OUTPUT);
        pinMode (col[pin], OUTPUT);
        digitalWrite (row[pin], LOW);
        digitalWrite (col[pin], HIGH);
    }
}

//display one led
void led_xy_on (int x, int y) {
    digitalWrite (row[x], HIGH);
    digitalWrite (col[y], LOW);
    digitalWrite (col[y], HIGH);
    digitalWrite (row[x], LOW);
}

//display one led with a time
void led_xy_time (int x, int y, int time) {
    for (int t=0; t<=time; t++) {
        digitalWrite (row[x], HIGH);
        digitalWrite (col[y], LOW);
        digitalWrite (col[y], HIGH);
        digitalWrite (row[x], LOW);
    }
}

//display one led with a state
void led_xy_state (int x, int y, int state) {
    digitalWrite (row[x], HIGH);
    digitalWrite (col[y], !state);
    digitalWrite (col[y], HIGH);
    digitalWrite (row[x], LOW);
}

//display one picture
void led_pic (int pix[8][8]) {
    for (int x=0; x<8; x++)
        for (int y=0; y<8; y++)
            led_xy_state (x, y, pix[x][y]);
}

//loop displays a set of pictures in pix[][8][8];
void led_pix (int pix[][8][8], int pmax, int time) {
    for (int p=0; p<pmax; p++)  //picture from 0 to pmax
        for (int t=0; t<time; t++)  //delay
            for (int x=0; x<8; x++)
                for (int y=0; y<8; y++)
                    led_xy_state (x, y, pix[p][x][y]);
}


//open all led
void led_all_on () {
    for (int i=0; i < 8; i++) {
        digitalWrite (row[i], HIGH);
        digitalWrite (col[i], LOW);
    }
}

/***************** end of led_8x8.h ***********************/

sample3.c

/*************** start of sample3.c ******************/
/*
 *  created: 2013-06-14
 *  by:  adrian
 *      loop displays a volution
 */

#include <Arduino.h>
#include "led_8x8.h"

void setup () {
    led_init ();
}

void loop () {
    int left = 0;
    int right = 7;
    int time = 500;

    for (int x=0,y=0 ;left<right; left++, right--) {
        for (x=left; x<=right; x++)
            led_xy_time (x, y, time);
        x--;
        for (y=left; y<=right; y++)
            led_xy_time (x, y, time);
        y--;
        for (x=right; x>=left; x--)
            led_xy_time (x, y, time);
        x++;
        for (y=right; y>left; y--)
            led_xy_time (x, y, time);
        y++;;
    }
}
/*************** end of sample3.c ******************/

Was this article helpful?

TOP