Go Down

Topic: LED matrix (Read 5 times) previous topic - next topic


I would like to control individually 36 LEDs in 6x6 matrix by an OpenGL programme well known as PPM reader. Do you have any suggestion on how to connect C code withe LEDs most efficiently in Arduino environment? I guess serial with multplexer would be a solution, but that's where my knowlegde ends so far...
Thank you in advance.
Best regards,



There are multiple ways.... (as usual)

There are these chips from Maxim called MAX7219 that can drive up to 8x8 leds matrices. They are very simple to wire up, you just need a resistor and a capacitor. in the arduino distribution there should be some code called MAX7219 (written by Nick Zambetti) that shows how to send data to these guys.

you can find a schematic here in the datasheet http://pdfserv.maxim-ic.com/en/ds/MAX7219-MAX7221.pdf

this can drive even fairly bright LEDs.

I'll attach the code here.
Code: [Select]

// define arduino pins

byte pin_max7219_dataIn =  2;
byte pin_max7219_clock  =  3;
byte pin_max7219_load   =  4;

// specify wiring pin i/o directions
void setPinModes()
 pinMode(pin_max7219_dataIn, OUTPUT);
 pinMode(pin_max7219_clock,  OUTPUT);
 pinMode(pin_max7219_load,   OUTPUT);

// define max7219 registers
byte max7219_reg_noop        = 0x00;
byte max7219_reg_digit0      = 0x01;
byte max7219_reg_digit1      = 0x02;
byte max7219_reg_digit2      = 0x03;
byte max7219_reg_digit3      = 0x04;
byte max7219_reg_digit4      = 0x05;
byte max7219_reg_digit5      = 0x06;
byte max7219_reg_digit6      = 0x07;
byte max7219_reg_digit7      = 0x08;
byte max7219_reg_decodeMode  = 0x09;
byte max7219_reg_intensity   = 0x0a;
byte max7219_reg_scanLimit   = 0x0b;
byte max7219_reg_shutdown    = 0x0c;
byte max7219_reg_displayTest = 0x0f;

// define max7219 as rows and cols (for nexus 8x8 displays)
byte max7219_row0 = 0x01;
byte max7219_row1 = 0x02;
byte max7219_row2 = 0x03;
byte max7219_row3 = 0x04;
byte max7219_row4 = 0x05;
byte max7219_row5 = 0x06;
byte max7219_row6 = 0x07;
byte max7219_row7 = 0x08;
byte max7219_col0 = 0x01;
byte max7219_col1 = 0x02;
byte max7219_col2 = 0x04;
byte max7219_col3 = 0x08;
byte max7219_col4 = 0x10;
byte max7219_col5 = 0x20;
byte max7219_col6 = 0x40;
byte max7219_col7 = 0x80;

// function to control max7219 data line
void max7219_setData(boolean value)
 digitalWrite(pin_max7219_dataIn, value);

// function to control max7219 clock line
void max7219_setClock(boolean value)
 digitalWrite(pin_max7219_clock, value);

// function to control max7219 load line
void max7219_setLoad(boolean value)
 digitalWrite(pin_max7219_load, value);

// function that puts a byte of data to the max7219
void max7219_putByte(byte data)
 byte i = 8;
 byte mask;
 while(i > 0) {
   mask = 0x01 << (i - 1);  // get bitmask
   max7219_setClock(LOW);   // tick
   if (data & mask){        // choose bit
     max7219_setData(HIGH); // send 1
     max7219_setData(LOW);  // send 0
   max7219_setClock(HIGH);  // tock
   --i;                     // move to lesser bit

// function that puts a byte of data into a max7219 register
void max7219_put(byte reg, byte data)
 max7219_setLoad(LOW); // begin
 max7219_putByte(reg);  // specify register
 max7219_putByte(data); // put data
 max7219_setLoad(LOW);  // latch in data
 max7219_setLoad(HIGH); // end
 max7219_setClock(LOW);   // tick

// function that sets brightness of the max7219
void max7219_setIntensity(byte intensity)
 // range: 0x00 to 0x0f
 max7219_put(max7219_reg_intensity, intensity & 0x0f);

// function that sets the same value for all registers of the max7219
void max7219_all(byte value){
 max7219_put(0x01, value);
 max7219_put(0x02, value);
 max7219_put(0x03, value);
 max7219_put(0x04, value);
 max7219_put(0x05, value);
 max7219_put(0x06, value);
 max7219_put(0x07, value);
 max7219_put(0x08, value);

// function that initializes the max7219 to use a matrix of leds
void max7219_init()
 max7219_put(max7219_reg_scanLimit, 0x04);   // use 5 columns
 max7219_put(max7219_reg_decodeMode, 0x00);  // using an led matrix (not digits)
 max7219_put(max7219_reg_shutdown, 0x01);    // not in shutdown mode
 max7219_put(max7219_reg_displayTest, 0x00); // no display test
 max7219_all(0x00);                          // empty registers
 max7219_setIntensity(0x08);                 // set initial brightness to dim

// program initialization routine
void setup()


// program loop
void loop()
 // draw diagonal line
 max7219_put(max7219_row0, 0x0f);
 max7219_put(max7219_row1, 0x0e);
 max7219_put(max7219_row2, 0x08);
 max7219_put(max7219_row3, 0x03);
 max7219_put(max7219_row4, 0x1e);
 max7219_put(max7219_row5, 0x0e);
 max7219_put(max7219_row6, 0x0e);
 max7219_put(max7219_row7, 0x0e);

If you have more questions just ask



Hi Massimo,
Thank you very much!
I need some time now to figure things out...
All best,


I am quite new in the physical computing world and I need your help.
I want to upgrade my 6x6 LED matrix to 6x6x6 cube that works as 2D display with "extruded" z direction (basically it's like a chess board, where each field (e.g. A2 or B5) is in parallel with 5 more diods that turn on and off as the first one controlled by an OpenGL-Arduino code.
The matrix is controlled by an OpenGL program, which proceeds images taken from the camera and notices changes for 6x6 fields. Since the extreme case could require turning on all the LEDs, I have to count on much stronger current...
So, I'm thinking of having established serial communication in between OpenGL code and microchip on the Arduino board. OpenGL code should send array of data  to 12 pins (6 for rows A to F, 6 for coloumns 1-6). An OpenGl code proceeds image taken from the camera  and for example if something happens on the field A5, the pins set to A and to 5 should be high...only in case if the two pins controling the exact row and exact column are set to high should turn the LED high (e.g. digital.Write(pinA, HIGH); digital.Write(pin5, HIGH); )
I'm thinking to use power transistor Darlingtons for this purpose. However, I set the codes in C and Arduino for test communication ( and printing for debugging), but there is probably something that I'm missing... I don't know how to send the data from computer to Arduino chip to clearly know what to do...this far I came on my own...
I would appreciate if you take a look at this codes and give some advice how to solve this...
Thank you very much,
//C code sends data from the computer
/* Standard input/output definitions */
#include <stdio.h>  
/* String function definitions */
#include <string.h>  
/* UNIX standard function definitions */
#include <unistd.h>  
/* File control definitions */
#include <fcntl.h>  
/* Error number definitions */
#include <errno.h>  
/* POSIX terminal control definitions */
#include <termios.h>

#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyUSB0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

int fd;
int n;

//FILE *input;
int output;
char readBuf[255];  //buffer for where data is put

int main(void)

     //init serial stuff

     struct termios oldtio, newtio;
     /* open the device to be non-blocking (read will return immediatly) */
     fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
     if (fd <0) {
     tcgetattr(fd,&oldtio); /* save current port settings */
     /* set new port settings for canonical input processing */
     newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
     newtio.c_iflag = IGNPAR | ICRNL;
     newtio.c_oflag = 0;
     newtio.c_lflag = ICANON;
     tcflush(fd, TCIFLUSH);

     //fd_set input;
       fd_set output;
     struct timeval timeout;
     int res;
     int paramValue;

     FD_SET(fd, &output);
     timeout.tv_sec = 0;
     timeout.tv_usec = 0;
     while(write(fd+1, &output,&timeout)) {
       res = write(fd, readBuf, 1);
     FD_SET(fd, &output);
     timeout.tv_sec = 0;
     timeout.tv_usec = 0;


// On Arduino

int ledPin = 13; //select the pin for LED

int val;

int serialGet = 0; //variable to store the value coming from the serial port

int delaytime = 1; //delaytime for the main loop

void setup() {

 pinMode(ledPin, OUTPUT); //declared LED as output




void loop() {

// if (Serial.available() > 0)

 serialGet = serialRead(); //read the serial port

 if (serialGet != -1) { //if the input is -1, then there is no data

   val = serialGet;     //otherwise store it


 if (val == 'H') {

   digitalWrite(ledPin, HIGH);

   Serial.print("I received: ");


   } else if(val == 'L') {

     digitalWrite(ledPin, LOW);

     Serial.print("I received: ");





David Cuartielles


I might be wrong, because I don't code C from the computer to perform this type of task but ... your C program is not handling the event of receiving data from the port, and therefore it is impossible for you to capture the data that Arduino sends.

The Arduino program looks ok, it is a "relay server" that sends back whatever it gets at the input. It is the program for the computer that is not finished.


Go Up