In order to have an LED Matrix of any significant size, you'll need to control more LEDs than you have outputs. You can multiplex your LEDs to accomplish this. It works like this:
The LEDs are arranged in a rectangle. The anodes are soldered together in one direction and the cathodes are soldered together perpendicular to the anodes. When all cathodes are high and anodes are low a negative voltage is applied to every LED, and if the voltage is low enough (see 'choosing LEDs' in components section), it will not breakdown. To turn on a particular LED, turn its cathode line low and anode line high. For example, to turn on LED (4,2) in the diagram, you would set segB to high and Dig4 to low.
The setup uses POV (Persistence of Vision) to draw an image on the array. Each LED required to draw the image is turned on one at a time, and looped very fast, giving the illusion that all LEDs are on at the same time (see programming section).
In this project, I used the Arduino Mega 2560, and assigned 16 outputs to cathodes and 16 to anodes. I used the HIGH voltage output (5V) as the power source. This was done for simplicity in a first project. However, if you can, you should avoid using a digital signal as a power source. There are a few reasons for this. Digital outputs are signals, and aren't designed to provide much current. You can breach the per-line or overall current limit of your microcontroller (40mA per line supply/sink for the Atmega) if your circuit draws much power. Also, the digital outputs in the Arduino and virtually every board are directly wired to the microcontroller without a fuse. This makes the microcontroller vulnerable to errors in your wiring. A simple short circuit, for example, can potentially render the pins or the microcontroller useless. However, the main fuse that limits overall current draw should prevent this.
The Ideal solution is to use the digital outputs to turn on/off a transistor at each line, and use an external power supply to drive the circuit. This increases wiring complexity, and of course, you need an external power supply for this. If you are building this as practice before you build more complex projects like a cube, you should consider going the transistor route. If you are doing this as a small project, your LED array won't be too large, or you're constrained on time / cost, then you can skip using transistors.
If you are using an external power supply, you can also consider using shift registers and the function shift_out
to increase the number of your output pins. This increases wiring and programming complexity, although it is almost impossible to construct projects that needs the controlling of large numbers of LEDs, like a cube, without it.