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

Glowing Cube Hack

One of the promotional things that Linden Lab gives away is a small colour changing, glowing cube. Last year, we attended a job fair were we gave several away. One of the people who received one decided to hack his cube and made a Gmail notifier.

I decided to copy the hack, bought myself an Arduino, and set about hacking the cube. I went for a slight improvement where I individually wired each LED. This means that using the PWM from the Arduino I was able to set the colour of the cube to whatever I chose. 

Arduino and Cube
Arduino and Cube

The main problem with this hack was the physical size of the Arduino. It’s too large to fit inside the cube. I could have attempted to use one of the smaller Arduino variants, but it would have been a bit pricey, costing about £30 for the Mini and the USB adapter.

I’d recently discovered AVR USB, which implements, USB in software, for any AVR microcontroller. USB needs two wires and I needed another 3 to control my LEDs, so even the smallest microcontroller would be enough and it doesn’t even need a crystal. AVR USB needs at least 2K of flash memory. There is an example project, EasyLogger, that uses ATTiny45 so that’s what I’ll use too.

ATTiny45
ATTiny45

The EasyLogger project is a good starting point, but I’m going to make some changes. First, I want to control the LEDs, so I replace the input voltage and start switch with a resistor LED pair. 

USB uses 2.8-3.6V for high on the data pair but provides 5V for power. Since I’m going to be powering the ATTiny from the USB I need a way to make the outputs deliver the lower voltage. The EasyLogger project uses Zener diodes to provide level conversion, but unfortunately, I don’t have any Zener diodes. 

The AVR USB provides an alternative suggestions of using a 3.3V voltage regulator for reducing the power (which again I don’t have any of) or a cheaper alternative, using a pair of normal diodes. Each diode has a drop of 0.7V so the pair drops the power voltage to 3.6V. 

Here is the first prototype circuit on my bread board. 

Breadboard prototype
Breadboard prototype

I’ll describe the code for the firmware and driver in later blog posts.

Getting started with Arduino; things to buy.

A friend of mine, Simon, has recently purchased an Arduino. He asked me, “how do I get started?”. Here is a list of the things that I’d recommend you purchase and why.

LEDs

Probably the simplest output indicator you can get: the LED. You can place the LEDs directly into the Arduino, as in the Blink tutorial. They provide a simple mechanism of getting information from the Arduino to you. With the Arduino’s 13 digital outputs can drive 13 LEDs. This number can be increased using components, such as, Shift Registers or techniques like charlieplexing.

Resistors

LEDs usually have a maximum voltage of ~2V. The Arduino’s outputs are 5v. For an Arduino to be able to power an LED the voltage must be reduced to by 3V. The simplest component to do this is a resistor. To figure out which values of resistor you will need, you can, use this helpful calculator. Make sure you get ~1/4W resistors as higher wattage means bigger components!

Breadboard

When you start to make circuits more complex than Blink, you will need something to prototype circuits on. A breadboard is a a solderless way of prototyping, which can be quickly put together and reused an unlimited number of times. Depending on the type, breadboards usually have horizontal strips, a gap in the middle for DIL components, such as ICs, and a pair of power lines marked on the sides. I only have two types of board, both are small, as I don’t make very complex circuits.

Wire & cutters/strippers


You can get quite far using the component leads to prototype, but its likely you are going to want to make connections from place to place, either from Arduino to breadboard or between components on the breadboard. To use wire with breadboard it needs to be solid core. To strip and cut wire, I just use a small pair of sharp side cutters.

Buttons

Although the above button has bent legs, they can easily be straightened to fit into a breadboard. Some electronics tutorials will tell you you require ‘pull-down’ resistors to use with the switch but this is not the case for the Arduino, as it has built in resistors, that can be turned on from software. It may still be worth getting a few 10K resistors to use with your switches.

What can I do with this stuff?

There is a good introduction to Arduino on Ladyada, where you will use need the stuff above to get started. In addition, I’ve made several different projects using just the above components, including, a POV Toy, an email notifier and a colour changing light.

Where next?

When you’ve finished with this stuff, you’ll probably want to look a different kinds of sensor. You can get a huge array of different types, such as temperature, light, acceleration or even blood alcohol content! For most simple sensors, you can use the Arduino’s analog inputs, for more complicated things you might have to use a number of digital pins or possibly I2C or SPI. There is a lot to learn about sensors, but that probably another blog post.