# Maximum LEDs on a matrix

There are several methods to drive LEDs using a microcontroller. The easiest method is just connecting all LEDs to an own pin on the microcontroller. Say you have $n$ pins available, you'll be able to drive $n$ pins.

However, there are different methods to drive LEDs as well:

• A diode matrix: Divide the pins into two sets: one for current source, one for current sink. Set the sources to 0 by default and the sinks to 1. Now, to light one LED, set the connected source to 1 and the sink to 0. Do this for all LEDs, after each other.
• Charlieplexing: With Charlieplexing, you set the unused pins to input, giving those a high-impedance state, 'disconnecting' them from the circuit.

What I'm looking for now is an overview of these methods to drive LEDs (and other much-used methods to drive LEDs, in a more efficient way than the 1:1-method described before, if they are noteworthy). What I need for every method is two calculations:

• Given $n$ pins, what would be the maximum amount of LEDs to drive?
• When you need $n$ LEDs, how many pins will you have to use as a minimum?
• Are you posting a question and then immediately answering it yourself?? Mar 12, 2013 at 11:49
• Yes, it's called "Share your knowledge, Q&A-style", and here is relevant meta: meta.electronics.stackexchange.com/q/2605/17592
– user17592
Mar 12, 2013 at 12:04
• OK, even seems to be encouraged. Strange in my book, but who am I... Mar 12, 2013 at 12:12

First, let's say we work with these two functions:

$L(n)$ is the maximum amount of LEDs that can be driven from $\mathsf{n}$ pins.
$p(n)$ is the minimum amount of pins needed to drive $\mathsf{n}$ LEDs.

## 1:1-method

This one is easy:

$$L(n)=n$$

$$p(n)=n$$

## A diode matrix

At first, we need to determine the most efficient diode matrix. For example, you could divide 4 pins into two sets of 2, or one set of 1 and one of 3. Obviously, the amount of LEDs is given by $\mathsf{length\cdot{}width}$. We can say $\mathsf{width=n-length}$, so the amount of LEDs is: $\mathsf{length\cdot{}(n-length)=-length^2+n\cdot{}length}$. Given an $\mathsf{n}$, this is a parabola, which has a maximum when $\mathsf{length=\frac{n}{2}}$. You can also do this on gut feeling. So, the maximum amount of LEDs is reached when the two sets have an equal amount of pins, or differ only 1, in case of an odd number of pins. We can now say:

$$L(n)=\lfloor{}\frac{n}{2}\rfloor{}\cdot\lceil{}\frac{n}{2}\rceil{}$$

Also, we can now easily understand the function $\mathsf{p(n)}$:

$$p(n)= \begin{cases} 1&\text{ for }n=1\\ \lceil{}\sqrt{n}\rceil&\text{ for }n\gt1 \end{cases}$$

I just included the cases for 1, as this is a special case. Normally, you can just use the second function.

## Charlieplexing

In this method, we have two LEDs between every set of two pins. We can calculate the amount of sets of two pins with:

$$(n-1)+(n-2)+\dots+1 = \frac{n\cdot(n-1)}{2}=\frac{n^2-n}{2}$$

Now we can say that:

$$L(n)=2\cdot\frac{n^2-n}{2}=n^2-n$$

We saw that the amount of pairs of pins equals $\mathsf{n\cdot(n-1)}$. With some reverse thinking, this leads to:

$$p(n)= \begin{cases} 1 &\text{ for } n=1\\ 2\cdot\lfloor\sqrt{n}\rfloor-1 &\text{ for } n\gt1 \end{cases}$$

I just included the cases for 1, as this is a special case. Normally, you can just use the second function.

## Other methods

I'm not aware of any other methods, as of Tuesday march 12, 2013.