LED Matrix

I’ve recently purchased a pair of 5×7 LED matrices. To learn how to use them I’ve decided to use my arduino to prototype them. I’ve downloaded the datasheet for the LEDs and I already have the arduino toolchain, so I’m ready to begin. The LEDs are in a matrix which means that you can not turn them on individually. Instead you have to strobe the LEDs at a frequency high enough that you can’t perceive any flicker and that each row or column is active at a different time.

To select each we need use a digital output for both the rows and columns. This means we need we need a total of 12 outputs. It might be possible to reduce this number further, but this will be fine as a first pass as we are not short on IO on the arduino.

The arduino has 13 digital outputs, but two are used for serial communication and I generally avoid using them as we might need them to help debug any problems. Instead I’m going to use the analog pins, which are referred to by using pin 14 to 20.

The arduino’s digital outputs deliver 5v, which is higher than the 2v required for each LED. As we are only going to drive one LED, per output, at a time the circuit decomposes to a standard series LED-resistor. I’ve calculated the value required using a web form using the information from the datasheet.

The column pins connect first to a resistor and then to the normal digital pins of the arduino. The row pins connect directly to the analog pins of the arduino.

The arduino sketch is simple to write, but care must be taken to ensure that the LEDs are on for similar durations, otherwise the will be different brightnesses. It is possible to write directly to the ports but for simplicity I’m going to just use digital write.

Here is the Arduino code, the pins (apins, cpins) need to be specified depending on how you have wired it up.

int ledPin = 13;  
int cpins[] = {15,17,18,16,19,0};
int apins[] = {6,8,7,2,5,3,4,0};
 
int pic0[] = {
  B00010000,
  B01010000,
  B00010000,
  B01010000,
  B00010000
};
 
int pic1[] = {
  B00010000,
  B01011000,
  B00011000,
  B01011000,
  B00010000
};
 
 
int pic2[] = {
  B00011000,
  B01010100,
  B00010100,
  B01010100,
  B00011000
};
 
int pic3[] = {
  B00011100,
  B01010010,
  B00010010,
  B01010010,
  B00011100
};
 
int *anim[] = { pic0, pic1, pic2, pic3, pic2, pic1, NULL };
 
void setup() 
{
  for (int i =0; apins[i]; ++i)
    pinMode(apins[i], OUTPUT); 
    
  for (int i =0; cpins[i]; ++i)
    pinMode(cpins[i], OUTPUT); 
  
}
 
int frame = 0;
unsigned long next_frame = 0;
void loop() 
{
  unsigned long time = millis();
  if (next_frame < time)
  {
    frame += 1;
    next_frame = time + 50;
    if (anim[frame] == NULL)
      frame = 0;
  }
  
  for (int j =0; cpins[j]; ++j)
  {
    for (int i =0; apins[i]; ++i)
    {
      digitalWrite(apins[i], LOW);
    }
    digitalWrite(cpins[j], LOW);
    for (int i =0; apins[i]; ++i)
    {
      digitalWrite(apins[i], anim[frame][j] & (1<<i));
    }
    digitalWrite(cpins[j], HIGH);
  }
}
LED Matrix and Arduino

LED Matrix and Arduino

Advertisements
This entry was posted in Arduino, Electronics, Prototyping and tagged , , , . Bookmark the permalink.

One Response to LED Matrix

  1. Pingback: ProjectSteve: Cylon attachment « Dave’s Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s