FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!

Controlling an LED Matrix Using Multiplexing

      The resistor’s value must be chosen to ensure that the maximum current through a pin does not exceed 40 mA. Because the current for up to eight LEDs can flow through each column pin, the maximum current for each LED must be one-eighth of 40 mA, or 5 mA. Each LED in a typical small red matrix has a forward voltage of around 1.8 volts. Calculating the resistor that results in 5 mA with a forward voltage of 1.8 volts
gives a value of 680 ohms. Check your data sheet to find the forward voltage of the matrix you want to use. Each column of the matrix is connected through the series resistor to a digital pin. When the column pin goes low and a row pin goes high, the corresponding LED will light. For all LEDs where the column pin is high or its row pin is low, no current will flow through the LED and it will not light. The for loop scans through each row and column and turns on sequential LEDs until all LEDs are lit. The loop starts with the first column and row and increments the row counter until all LEDs in that row are lit; it then moves to the next column, and so on, lighting another LED with each pass through the loop until all the LEDs are lit. You can control the
number of lit LEDs in proportion to the value from a sensor  by making the following changes to the sketch. Comment out or remove these three lines from the beginning of the loop:

pixel = pixel + 1;
if(pixel > 63)
pixel = 0;

Replace them with the following lines that read the value of a sensor on pin 0 and map
this to a number of pixels ranging from zero to 63:

int sensorValue = analogRead(0); // read the analog in value
pixel = map(sensorValue, 0, 1023, 0, 63); // map sensor value to pixel (LED)

CODE:
This sketch uses an LED matrix of 64 LEDs with anodes connected in rows and cathodes in columns (as in the Futurlec LEDM88R). Dual-color LED displays may be easier to obtain, and you can drive just one of the colors if that is all you need.
/* matrixMpx sketch Sequence LEDs starting from first column and row until all LEDS are lit Multiplexing is used to control 64 LEDs with 16 pins */

const int columnPins[] = { 2, 3, 4, 5, 6, 7, 8, 9};
const int rowPins[] = { 10,11,12,15,16,17,18,19};
int pixel = 0; // 0 to 63 LEDs in the matrix
int columnLevel = 0; // pixel value converted into LED column
int rowLevel = 0; // pixel value converted into LED row
void setup() {
for (int i = 0; i < 8; i++)
{
pinMode(columnPins[i], OUTPUT); // make all the LED pins outputs
pinMode(rowPins[i], OUTPUT);
}
}
void loop() {
pixel = pixel + 1;
if(pixel > 63)
pixel = 0;
columnLevel = pixel / 8; // map to the number of columns
rowLevel = pixel % 8; // get the fractional value
for (int column = 0; column < 8; column++)
{
digitalWrite(columnPins[column], LOW); // connect this column to Ground
for(int row = 0; row < 8; row++)
{
if (columnLevel > column)
{
digitalWrite(rowPins[row], HIGH); // connect all LEDs in row to +5 volts
}

else if (columnLevel == column && rowLevel >= row)
{
digitalWrite(rowPins[row], HIGH);
}
else
{
digitalWrite(columnPins[column], LOW); // turn off all LEDs in this row
}
delayMicroseconds(300); // delay gives frame time of 20ms for 64 LEDs
digitalWrite(rowPins[row], LOW); // turn off LED
}
digitalWrite(columnPins[column], HIGH); // disconnect this column
from Ground
}

}


No comments:

Post a Comment