Tutorial.RowColumnScanning History

Hide minor edits - Show changes to markup

June 07, 2010, at 02:08 PM by Equipo Traduccion -
Changed lines 1-6 from:

Examples > Display

Row-columm Scanning to control an 8x8 LED Matrix

LED displays are often packaged as matrixes of LEDs arranged in rows of common anodes and columns of common cathodes, or the reverse. Here's a typical example, and its schematic:

to:

Ejemplos> Pantalla Matriz LED

Escaneo fila-columna para controlar una matriz de LED 8x8

Las pantallas de LED están a menudo empaquetadas como matrices de LED dispuestos en filas de ánodos comunes y columnas de cátodos comunes, o a la inversa. Aquí hay un ejemplo típico, y su esquema:

Changed lines 12-21 from:

These can be very useful displays. To control a matrix, you connect both its rows and columns to your microcontroller. The columns are connected to the LEDs anodes (see Figure 1), so a column needs to be high for any of the LEDs in that column to turn on. The rows are connected to the LEDs cathodes, so the row needs to be low for an individual LED to turn on. If the row and the column are both high or both low, no voltage flows through the LED and it doesn’t turn on.

To control an individual LED, you set its column high and its row low. To control multiple LEDs in a row, you set the rows high, then take the column high, then set the lows row or high as appropriate; a low row will turn the corresponding LED on, and a high row will turn it off.

Although there are pre-made LED matrices, you can also make your own matrix from 64 LEDs, using the schematic as shown above.

It doesn’t matter which pins of the microcontroller you connect the rows and columns to, because you can assign things in software. Connected the pins in a way that makes wiring easiest. A typical layout is shown below.

Here's a matrix of the pin connections, based on the diagram above:

to:

Estas pantallas pueden ser muy útiles. Para controlar una matriz, conectas sus filas y columnas a tu microcontrolador. Las columnas se conectan a los ánodos de los LED (ver Figura 1), por lo que una columna tiene que estar a nivel alto para encender cualquiera de los LEDs de esa columna. Las filas están conectadas a los cátodos de los LEDs, por lo que la fila tiene que estar a nivel bajo para que un LED en concreto se encienda. Si la fila y la columna están a nivel alto o bajo a la vez, la corriente no fluye a través del LED y no se enciende.

Para controlar un LED en concreto, se establece su columna a nivel alto y su fila a nivel bajo. Para controlar múltiples LEDs en una columna, primero se ponen las filas a nivel bajo, a continuación se pone la columna a nivel alto, luego se establecen las filas a nivel alto o bajo según corresponda, una fila a nivel bajo encenderá el LED y por el contrario una fila a nivel bajo lo apagará.

To control multiple LEDs in a row(COLUMNA???), you set the rows high, then take the column high, then set the lows row or high as appropriate; a low row will turn the corresponding LED on, and a high row will turn it off.

Aunque hay matrices LED ya hechas, tu también también hacer tu propia matriz con 64 leds, utilizando el esquema de a continuacion.

No importa en qué pins del microcontrolador conectes las filas y columnas, porque estas cosas se pueden asignar en software. Conecta los pines de una manera que te haga más fácil el cableado. Un diseño típico se muestra a continuación.

Esta es una tabla de las conexiones de pin, basada en el diagrama de a continuación.

Changed line 27 from:

Matrix pin no.

to:

Nº pin de Matriz

Changed line 29 from:

Row

to:

Fila

Changed line 31 from:

Column

to:

Columna

Changed line 33 from:

Arduino pin number

to:

Nº pin Arduino

Changed line 73 from:

16 (analog pin 2)

to:

16 (pin analógico 2)

Changed line 81 from:

17 (analog pin 3)

to:

17 (pin analógico 3)

Changed line 89 from:

18 (analog pin 4)

to:

18 (pin analógico 4)

Changed line 97 from:

19 (analog pin 5)

to:

19 (pin analógico 5)

Changed lines 163-170 from:

Circuit

the 16 pins of the matrix are hooked up to 16 pins of the Arduino. Four of the analog pins are used as digital inputs 16 through 19. The order of the pins is assigned in two arrays in the code.

Two potentiometers control the movement of a lit LED in the matrix.

click the image to enlarge

to:

Circuito

Los 16 pines de la matriz están conectados a 16 pines del Arduino. Cuatro de los pines analógicos se utilizan como entradas digitales 16 a 19. El orden de los pines se le asigna en dos matrices en el código.

Dos potenciómetros controlan el movimiento de una luz LED de la matriz.

haz clic en la imagen para ampliarla

Changed lines 173-179 from:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic:

click the image to enlarge.

to:

Imagen desarrollada utilizando Fritzing. Para mas circuitos de ejemplo, mira la página de proyectos Fritzing

Esquema

haz clic en la imagen para ampliarla

Changed lines 182-183 from:

Code

to:

Código

Changed lines 191-194 from:

There is a more complex example in the Arduino playground. Other examples can be found on Tom Igoe's blog:

  • Tom's original version of this tutorial
  • Two matrixes on an Arduino Mega
  • Two-Matrix Pong
to:

Hay un ejemplo más complejo en el playground Arduino. Puedes encontrar otros ejemplos en el blog de Tom Igoe:

  • La version original del tutorial de Tom
  • Dos matrices en un Arduino Mega
  • Pong con dos matrices
February 24, 2010, at 04:33 AM by Tom Igoe -
Changed lines 183-214 from:
 /*
   Row-Column Scanning an 8x8 LED matrix with X-Y input
  
  This example controls an 8x8 LED matrix using two analog inputs
  
  created 27 May 2009
  modified 29 Jun 2009
  by Tom Igoe
  
  This example works for the Lumex  LDM-24488NI Matrix. See 
  http://sigma.octopart.com/140413/datasheet/Lumex-LDM-24488NI.pdf
  for the pin connections
  
  For other LED cathode column matrixes, you should only need to change 
  the pin numbers in the row[] and column[] arrays
  
  rows are the anodes
  cols are the cathodes
  ---------
  
  Pin numbers:
  Matrix:
  * Digital pins 2 through 13,
  * analog pins 2 through 5 used as digital 16 through 19
  Potentiometers:
  * center pins are attached to analog pins 0 and 1, respectively
  * side pins attached to +5V and ground, respectively.
  
  http://www.arduino.cc/en/Tutorial/RowColumnScanning
  
  see also http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more
  */
to:
Deleted lines 184-263:
 // 2-dimensional array of row pin numbers:
 const int row[8] = {
   2,7,19,5,13,18,12,16 };

 // 2-dimensional array of column pin numbers:
 const int col[8] = {
   6,11,10,3,17,4,8,9  };

 // 2-dimensional array of pixels:
 int pixels[8][8];           

 // cursor position:
 int x = 5;
 int y = 5;

 void setup() {
   Serial.begin(9600);
   // initialize the I/O pins as outputs:

   // iterate over the pins:
   for (int thisPin = 0; thisPin < 8; thisPin++) {
     // initialize the output pins:
     pinMode(col[thisPin], OUTPUT); 
     pinMode(row[thisPin], OUTPUT);  
     // take the col pins (i.e. the cathodes) high to ensure that
     // the LEDS are off: 
     digitalWrite(col[thisPin], HIGH);    
   }

   // initialize the pixel matrix:
   for (int x = 0; x < 8; x++) {
     for (int y = 0; y < 8; y++) {
       pixels[x][y] = HIGH;
     }
   }
 }

 void loop() {
   // read input:
   readSensors();

   // draw the screen:
   refreshScreen();
 }

 void readSensors() {
   // turn off the last position:
   pixels[x][y] = HIGH;
   // read the sensors for X and Y values:
   x = 7 - map(analogRead(0), 0, 1023, 0, 7);
   y = map(analogRead(1), 0, 1023, 0, 7);
   // set the new pixel position low so that the LED will turn on
   // in the next screen refresh:
   pixels[x][y] = LOW;

 }

 void refreshScreen() {
   // iterate over the rows (anodes):
   for (int thisRow = 0; thisRow < 8; thisRow++) {
     // take the row pin (anode) high:
     digitalWrite(row[thisRow], HIGH);
     // iterate over the cols (cathodes):
     for (int thisCol = 0; thisCol < 8; thisCol++) {
       // get the state of the current pixel;
       int thisPixel = pixels[thisRow][thisCol];
       // when the row is HIGH and the col is LOW,
       // the LED where they meet turns on:
       digitalWrite(col[thisCol], thisPixel);
       // turn the pixel off:
       if (thisPixel == LOW) {
         digitalWrite(col[thisCol], HIGH);
       }
     }
     // take the row pin low to turn off the whole row:
     digitalWrite(row[thisRow], LOW);
   }
 }
November 26, 2009, at 06:17 AM by David A. Mellis -
Changed line 219 from:
   2,7,19,5,18,12,16 };
to:
   2,7,19,5,13,18,12,16 };
August 27, 2009, at 08:57 PM by Tom Igoe -
Added lines 170-172:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 05, 2009, at 07:50 PM by Tom Igoe -
Changed lines 177-179 from:

[@ /*

  Row-Column Scanning an 8x8 LED matrix with X-Y input
to:
Deleted line 212:
 This example controls an 8x8 LED matrix using two analog inputs
Changed lines 214-216 from:
 created 27 May 2009
 modified 29 Jun 2009
 by Tom Igoe
to:
 // 2-dimensional array of row pin numbers:
 const int row[8] = {
   2,7,19,5,18,12,16 };
Changed lines 218-220 from:
 This example works for the Lumex  LDM-24488NI Matrix. See 
 http://sigma.octopart.com/140413/datasheet/Lumex-LDM-24488NI.pdf
 for the pin connections
to:
 // 2-dimensional array of column pin numbers:
 const int col[8] = {
   6,11,10,3,17,4,8,9  };
Changed lines 222-223 from:
 For other LED cathode column matrixes, you should only need to change 
 the pin numbers in the row[] and column[] arrays
to:
 // 2-dimensional array of pixels:
 int pixels[8][8];           
Changed lines 225-227 from:
 rows are the anodes
 cols are the cathodes
 ---------
to:
 // cursor position:
 int x = 5;
 int y = 5;
Changed lines 229-235 from:
 Pin numbers:
 Matrix:
 * Digital pins 2 through 13,
 * analog pins 2 through 5 used as digital 16 through 19
 Potentiometers:
 * center pins are attached to analog pins 0 and 1, respectively
 * side pins attached to +5V and ground, respectively.
to:
 void setup() {
   Serial.begin(9600);
   // initialize the I/O pins as outputs:
Changed lines 233-241 from:
 http://www.arduino.cc/en/Tutorial/RowColumnScanning
to:
   // iterate over the pins:
   for (int thisPin = 0; thisPin < 8; thisPin++) {
     // initialize the output pins:
     pinMode(col[thisPin], OUTPUT); 
     pinMode(row[thisPin], OUTPUT);  
     // take the col pins (i.e. the cathodes) high to ensure that
     // the LEDS are off: 
     digitalWrite(col[thisPin], HIGH);    
   }
Changed lines 243-325 from:
 see also http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more
 */

// 2-dimensional array of row pin numbers: const int row[8] = {

  2,7,19,5,18,12,16 };

// 2-dimensional array of column pin numbers: const int col[8] = {

  6,11,10,3,17,4,8,9  };

// 2-dimensional array of pixels: int pixels[8][8];

// cursor position: int x = 5; int y = 5;

void setup() {

  Serial.begin(9600);
  // initialize the I/O pins as outputs:

  // iterate over the pins:
  for (int thisPin = 0; thisPin < 8; thisPin++) {
    // initialize the output pins:
    pinMode(col[thisPin], OUTPUT); 
    pinMode(row[thisPin], OUTPUT);  
    // take the col pins (i.e. the cathodes) high to ensure that
    // the LEDS are off: 
    digitalWrite(col[thisPin], HIGH);    
  }

  // initialize the pixel matrix:
  for (int x = 0; x < 8; x++) {
    for (int y = 0; y < 8; y++) {
      pixels[x][y] = HIGH;
    }
  }

}

void loop() {

  // read input:
  readSensors();

  // draw the screen:
  refreshScreen();

}

void readSensors() {

  // turn off the last position:
  pixels[x][y] = HIGH;
  // read the sensors for X and Y values:
  x = 7 - map(analogRead(0), 0, 1023, 0, 7);
  y = map(analogRead(1), 0, 1023, 0, 7);
  // set the new pixel position low so that the LED will turn on
  // in the next screen refresh:
  pixels[x][y] = LOW;

}

void refreshScreen() {

  // iterate over the rows (anodes):
  for (int thisRow = 0; thisRow < 8; thisRow++) {
    // take the row pin (anode) high:
    digitalWrite(row[thisRow], HIGH);
    // iterate over the cols (cathodes):
    for (int thisCol = 0; thisCol < 8; thisCol++) {
      // get the state of the current pixel;
      int thisPixel = pixels[thisRow][thisCol];
      // when the row is HIGH and the col is LOW,
      // the LED where they meet turns on:
      digitalWrite(col[thisCol], thisPixel);
      // turn the pixel off:
      if (thisPixel == LOW) {
        digitalWrite(col[thisCol], HIGH);
      }
    }
    // take the row pin low to turn off the whole row:
    digitalWrite(row[thisRow], LOW);
  }

} @]

to:
June 30, 2009, at 05:13 PM by Tom Igoe -
Changed line 184 from:
 modified 26 Jun 2009
to:
 modified 29 Jun 2009
Changed lines 189-196 from:
 for the pin connections.  

  For other LED cathode column matrixes, you should only need to change 
  the pin numbers in the row[] and column[] arrays

 rows are the anodes
 cols are the cathodes
 ---------
to:
 for the pin connections
Added lines 191-197:
 For other LED cathode column matrixes, you should only need to change 
 the pin numbers in the row[] and column[] arrays

 rows are the anodes
 cols are the cathodes
 ---------
Changed lines 202-203 from:
 Accelerometer: 
 * X and Y axes attached to analog in 0 and 1
to:
 Potentiometers:
 * center pins are attached to analog pins 0 and 1, respectively
 * side pins attached to +5V and ground, respectively.
Changed lines 206-208 from:
  http://www.arduino.cc/en/Tutorial/RowColumnScanning

 see http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more
to:
 http://www.arduino.cc/en/Tutorial/RowColumnScanning

 see also http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more
Changed lines 211-215 from:

// max and min values from the accelerometer, // found by experiment: const int sensorMin = 380; const int sensorMax = 620;

to:
Changed lines 214-215 from:
   2,7,19,5,18,12,16 };
to:
  2,7,19,5,18,12,16 };
Changed lines 261-262 from:
  x = 7 - map(analogRead(0), sensorMin, sensorMax, 0, 7);
  y = map(analogRead(1), sensorMin, sensorMax, 0, 7);
to:
  x = 7 - map(analogRead(0), 0, 1023, 0, 7);
  y = map(analogRead(1), 0, 1023, 0, 7);
  // set the new pixel position low so that the LED will turn on
  // in the next screen refresh:
June 30, 2009, at 05:11 PM by Tom Igoe -
Changed lines 172-173 from:

click the image to enlarge

to:

click the image to enlarge.

June 29, 2009, at 06:00 PM by Tom Igoe -
Added lines 162-165:

the 16 pins of the matrix are hooked up to 16 pins of the Arduino. Four of the analog pins are used as digital inputs 16 through 19. The order of the pins is assigned in two arrays in the code.

Two potentiometers control the movement of a lit LED in the matrix.

June 29, 2009, at 05:53 PM by Tom Igoe -
Added lines 16-17:

Although there are pre-made LED matrices, you can also make your own matrix from 64 LEDs, using the schematic as shown above.

Deleted lines 21-22:
June 29, 2009, at 05:51 PM by Tom Igoe -
Changed lines 5-6 from:

LED displays are often packaged as matrixes of LEDs arranged in rows of common anodes and columns of common cathodes, or the reverse. Here's a typical example, and its schematic:

to:

LED displays are often packaged as matrixes of LEDs arranged in rows of common anodes and columns of common cathodes, or the reverse. Here's a typical example, and its schematic:

June 29, 2009, at 05:51 PM by Tom Igoe -
Changed lines 175-176 from:
  Row-Column Scanning an 8x8 LED matrix with accelerometer
 for Arduino 
to:
  Row-Column Scanning an 8x8 LED matrix with X-Y input
Changed lines 177-178 from:
 This example controls an 8x8 LED matrix using two inputs
 from an accelerometer.
to:
 This example controls an 8x8 LED matrix using two analog inputs
Changed lines 183-186 from:
 This example works for the Fairchild GMA2288C matrix.  See

http://sigma.octopart.com/125394/datasheet/Fairchild-GMA2288C.pdf for the pin connections

to:
 This example works for the Lumex  LDM-24488NI Matrix. See 
 http://sigma.octopart.com/140413/datasheet/Lumex-LDM-24488NI.pdf
 for the pin connections.  

  For other LED cathode column matrixes, you should only need to change 
  the pin numbers in the row[] and column[] arrays
Added line 203:
 see http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more
Changed lines 213-214 from:
   9,8,7,6,16,17,18,19 };
to:
   2,7,19,5,18,12,16 };
Changed lines 217-218 from:
  13,12,11,10,5,4,3,2  };
to:
  6,11,10,3,17,4,8,9  };
Deleted line 253:
Deleted line 285:
Deleted line 286:
June 29, 2009, at 05:47 PM by Tom Igoe -
June 29, 2009, at 05:44 PM by Tom Igoe -
Added line 21:
Deleted line 38:
Deleted line 46:
Deleted line 54:
Deleted line 62:
Deleted lines 70-71:
Deleted lines 78-79:
Deleted lines 86-87:
Deleted lines 94-95:
Deleted lines 102-103:
Deleted lines 110-111:
Deleted lines 118-119:
Deleted lines 126-127:
Deleted lines 134-135:
Deleted line 159:
June 29, 2009, at 05:43 PM by Tom Igoe -
June 29, 2009, at 05:42 PM by Tom Igoe -
Added line 20:
Changed line 22 from:
to:
Changed line 30 from:
to:
Changed line 39 from:
to:
Changed line 48 from:
to:
Changed line 57 from:
to:
Changed line 66 from:
to:
Changed line 76 from:
to:
Changed line 86 from:
to:
Changed line 96 from:
to:
Changed line 106 from:
to:
Changed line 116 from:
to:
Changed line 126 from:
to:
Changed line 136 from:
to:
Changed line 146 from:
to:
Changed line 156 from:
to:
Changed line 164 from:
to:
Changed line 172 from:
to:
June 29, 2009, at 05:41 PM by Tom Igoe -
Changed line 21 from:
to:
Changed line 23 from:
to:
Changed line 25 from:
to:
Changed line 27 from:
to:
Changed line 29 from:
to:
Changed line 31 from:
to:
Changed line 33 from:
to:
Changed line 35 from:
to:
Changed line 38 from:
to:
Changed line 40 from:
to:
Changed line 42 from:
to:
Changed line 44 from:
to:
Changed line 47 from:
to:
Changed line 49 from:
to:
Changed line 51 from:
to:
Changed line 53 from:
to:
Changed line 56 from:
to:
Changed line 58 from:
to:
Changed line 60 from:
to:
Changed line 62 from:
to:
Changed line 65 from:
to:
Changed line 67 from:
to:
Changed line 69 from:
to:
Changed line 71 from:
to:
Changed line 75 from:
to:
Changed line 77 from:
to:
Changed line 79 from:
to:
Changed line 81 from:
to:
Changed line 85 from:
to:
Changed line 87 from:
to:
Changed line 89 from:
to:
Changed line 91 from:
to:
Changed line 95 from:
to:
Changed line 97 from:
to:
Changed line 99 from:
to:
Changed line 101 from:
to:
Changed line 105 from:
to:
Changed line 107 from:
to:
Changed line 109 from:
to:
Changed line 111 from:
to:
Changed line 115 from:
to:
Changed line 117 from:
to:
Changed line 119 from:
to:
Changed line 121 from:
to:
Changed line 125 from:
to:
Changed line 127 from:
to:
Changed line 129 from:
to:
Changed line 131 from:
to:
Changed line 135 from:
to:
Changed line 137 from:
to:
Changed line 139 from:
to:
Changed line 141 from:
to:
Changed line 145 from:
to:
Changed line 147 from:
to:
Changed line 149 from:
to:
Changed line 151 from:
to:
Changed line 155 from:
to:
Changed line 157 from:
to:
Changed line 159 from:
to:
Changed line 161 from:
to:
Changed line 163 from:
to:
Changed line 165 from:
to:
Changed line 167 from:
to:
Changed line 169 from:
to:
Changed line 171 from:
to:
Changed line 173 from:
to:
Changed line 175 from:
to:
Changed line 177 from:
to:
June 29, 2009, at 05:40 PM by Tom Igoe -
Changed line 21 from:
to:
June 29, 2009, at 05:39 PM by Tom Igoe -
Changed line 20 from:
to:
June 29, 2009, at 05:37 PM by Tom Igoe -
Changed line 20 from:
to:
June 29, 2009, at 05:37 PM by Tom Igoe -
Changed line 20 from:
to:
June 29, 2009, at 05:36 PM by Tom Igoe -
Changed lines 18-19 from:

Here's a matrix of the pin connections

to:

Here's a matrix of the pin connections, based on the diagram above:

Changed line 24 from:

Row'''

to:

Row

Changed line 26 from:

Column'''

to:

Column

Changed line 28 from:

Arduino pin number'''

to:

Arduino pin number

Changed line 106 from:

X

to:

9

Changed line 108 from:

X

to:

1

Changed line 110 from:

X

to:

-

Changed lines 112-114 from:

X

to:

2

Changed line 116 from:

X

to:

10

Changed line 118 from:

X

to:

-

Changed line 120 from:

X

to:

4

Changed lines 122-124 from:

X

to:

3

Changed line 126 from:

X

to:

11

Changed line 128 from:

X

to:

-

Changed line 130 from:

X

to:

6

Changed lines 132-134 from:

X

to:

4

Changed line 136 from:

X

to:

12

Changed line 138 from:

X

to:

4

Changed line 140 from:

X

to:

-

Changed lines 142-144 from:

X

to:

5

Changed line 146 from:

X

to:

13

Changed line 148 from:

X

to:

-

Changed line 150 from:

X

to:

1

Changed lines 152-154 from:

X

to:

6

Changed line 156 from:

X

to:

14

Changed line 158 from:

X

to:

2

Changed line 160 from:

X

to:

-

Changed lines 162-164 from:

X

to:

7

Changed line 164 from:

X

to:

15

Changed line 166 from:

X

to:

-

Changed line 168 from:

X

to:

7

Changed lines 170-172 from:

X

to:

8

Changed line 172 from:

X

to:

16

Changed line 174 from:

X

to:

-

Changed line 176 from:

X

to:

8

Changed lines 178-180 from:

X

to:

9

June 29, 2009, at 05:33 PM by Tom Igoe -
Changed line 22 from:

'''Matrix pin no.

to:

Matrix pin no.

Changed line 24 from:

Row

to:

Row'''

Changed line 26 from:

Column

to:

Column'''

Changed lines 36-37 from:

X

to:

13

Changed line 39 from:

-

to:

2

Added lines 41-42:
Added lines 45-49:
Added lines 52-58:
Added lines 60-184:
June 29, 2009, at 05:28 PM by Tom Igoe -
Changed line 22 from:

Matrix pin no.

to:

'''Matrix pin no.

Changed line 28 from:

Arduino pin number

to:

Arduino pin number'''

June 29, 2009, at 05:27 PM by Tom Igoe -
Changed line 21 from:

(cellnr:)

to:
June 29, 2009, at 05:26 PM by Tom Igoe -
Changed lines 21-22 from:

(cellnr:)Matrix pin no.

to:

(cellnr:) Matrix pin no.

Deleted line 28:
Added line 38:

-

Added line 40:

-

Added line 42:

-

Changed line 44 from:
to:

-

June 29, 2009, at 05:26 PM by Tom Igoe -
Added lines 18-43:
June 26, 2009, at 10:34 PM by Tom Igoe -
Changed lines 7-13 from:

to:

June 26, 2009, at 10:28 PM by Tom Igoe -
June 26, 2009, at 10:26 PM by Tom Igoe -
Added lines 1-152:

Examples > Display

Row-columm Scanning to control an 8x8 LED Matrix

LED displays are often packaged as matrixes of LEDs arranged in rows of common anodes and columns of common cathodes, or the reverse. Here's a typical example, and its schematic:

These can be very useful displays. To control a matrix, you connect both its rows and columns to your microcontroller. The columns are connected to the LEDs anodes (see Figure 1), so a column needs to be high for any of the LEDs in that column to turn on. The rows are connected to the LEDs cathodes, so the row needs to be low for an individual LED to turn on. If the row and the column are both high or both low, no voltage flows through the LED and it doesn’t turn on.

To control an individual LED, you set its column high and its row low. To control multiple LEDs in a row, you set the rows high, then take the column high, then set the lows row or high as appropriate; a low row will turn the corresponding LED on, and a high row will turn it off.

It doesn’t matter which pins of the microcontroller you connect the rows and columns to, because you can assign things in software. Connected the pins in a way that makes wiring easiest. A typical layout is shown below.

Circuit

click the image to enlarge

Schematic:

click the image to enlarge

Code

/*
  Row-Column Scanning an 8x8 LED matrix with accelerometer
 for Arduino 

 This example controls an 8x8 LED matrix using two inputs
 from an accelerometer.

 created 27 May 2009
 modified 26 Jun 2009
 by Tom Igoe

 This example works for the Fairchild GMA2288C matrix.  See
http://sigma.octopart.com/125394/datasheet/Fairchild-GMA2288C.pdf
for the pin connections

 rows are the anodes
 cols are the cathodes
 ---------

 Pin numbers:
 Matrix:
 * Digital pins 2 through 13,
 * analog pins 2 through 5 used as digital 16 through 19
 Accelerometer: 
 * X and Y axes attached to analog in 0 and 1

  http://www.arduino.cc/en/Tutorial/RowColumnScanning

 */

// max and min values from the accelerometer, 
// found by experiment:
const int sensorMin = 380;
const int sensorMax = 620;

// 2-dimensional array of row pin numbers:
const int row[8] = {
   9,8,7,6,16,17,18,19 };

// 2-dimensional array of column pin numbers:
const int col[8] = {
  13,12,11,10,5,4,3,2  };

// 2-dimensional array of pixels:
int pixels[8][8];           

// cursor position:
int x = 5;
int y = 5;

void setup() {
  Serial.begin(9600);
  // initialize the I/O pins as outputs:

  // iterate over the pins:
  for (int thisPin = 0; thisPin < 8; thisPin++) {
    // initialize the output pins:
    pinMode(col[thisPin], OUTPUT); 
    pinMode(row[thisPin], OUTPUT);  
    // take the col pins (i.e. the cathodes) high to ensure that
    // the LEDS are off: 
    digitalWrite(col[thisPin], HIGH);    
  }

  // initialize the pixel matrix:
  for (int x = 0; x < 8; x++) {
    for (int y = 0; y < 8; y++) {
      pixels[x][y] = HIGH;
    }
  }
}

void loop() {
  // read input:
  readSensors();

  // draw the screen:
  refreshScreen();

}

void readSensors() {
  // turn off the last position:
  pixels[x][y] = HIGH;
  // read the sensors for X and Y values:
  x = 7 - map(analogRead(0), sensorMin, sensorMax, 0, 7);
  y = map(analogRead(1), sensorMin, sensorMax, 0, 7);
  pixels[x][y] = LOW;

}

void refreshScreen() {
  // iterate over the rows (anodes):
  for (int thisRow = 0; thisRow < 8; thisRow++) {
    // take the row pin (anode) high:
    digitalWrite(row[thisRow], HIGH);
    // iterate over the cols (cathodes):
    for (int thisCol = 0; thisCol < 8; thisCol++) {
      // get the state of the current pixel;
      int thisPixel = pixels[thisRow][thisCol];
      // when the row is HIGH and the col is LOW,
      // the LED where they meet turns on:
      digitalWrite(col[thisCol], thisPixel);
      // turn the pixel off:
      if (thisPixel == LOW) {
        digitalWrite(col[thisCol], HIGH);
      }
    }
    // take the row pin low to turn off the whole row:
    digitalWrite(row[thisRow], LOW);
  }

}


There is a more complex example in the Arduino playground. Other examples can be found on Tom Igoe's blog:

  • Tom's original version of this tutorial
  • Two matrixes on an Arduino Mega
  • Two-Matrix Pong

Share