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:
    enter image description here
    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:
    enter image description here
    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?
  • \$\begingroup\$ Are you posting a question and then immediately answering it yourself?? \$\endgroup\$ Mar 12, 2013 at 11:49
  • 2
    \$\begingroup\$ Yes, it's called "Share your knowledge, Q&A-style", and here is relevant meta: meta.electronics.stackexchange.com/q/2605/17592 \$\endgroup\$
    – user17592
    Mar 12, 2013 at 12:04
  • \$\begingroup\$ OK, even seems to be encouraged. Strange in my book, but who am I... \$\endgroup\$ Mar 12, 2013 at 12:12

1 Answer 1


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.


This one is easy:



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:


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.


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:


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy