Multiplexing is really cool. Put simply, it lets you control lots of LEDs with only a few wires. I’ve been doing quite a bit with multiplexing recently, but I haven’t found a nice simple introduction yet, so I wrote one.
Multiplexing is based on the fact that an LED is a diode, and diodes only let current flow in one direction. This can be annoying at times (if you install them the wrong way they don’t work, and they need DC power to run off), but can be quite advantageous at other times. Most LEDs won’t be damaged if you connect them backwards at a vaguely-sane voltage, they just won’t turn on.
For example, if we wanted to switch between two LEDs being on, normally you’d connect a wire to the positive legs of each of them, and connect the other legs to ground: a grand total of 3 wires.
However, because LEDs only conduct in one direction, it’s possible to do this with just 2 wires. The LEDs are connected across the wires with one in one direction, and one in the other., as shown in the diagram.
If the blue wire is connected to -, and the red wire to +, the left-hand LED will light. If the blue wire is connected to + and the red to -, the right hand LED will light.
It’s even possible to get both LEDs to light, by very quickly switching from positive to negative on each wire. This relies on a phenomenon called persistence of vision – essentially, if you switch a light on and off fast enough, the eye is too slow at seeing the change, so the LED just seems as if its on. More on that later.
Similarly, the same can be done with 3 wires. In the diagram below, any one of the LEDs can be turned on independently, if the wires its connected to are connected to the right things. For example, to turn on the LED second from right, the red wire is set to positive and the purple to negative.
The blue wire is said to be ‘floating’ – it is in neither positive or negative states. This essentially means there is no voltage connected, so the only way there would be a current is through electromagnetism or charge, but we don’t need to worry about that here. Because it is unconnected, and has no voltage or current, the LEDs connected to it don’t light. The rightmost LED doesn’t light because it’s connected in reverse, so only the second-from-right LED lights up. Again, multiplexing could be used here to light any combination of LEDs.
Similarly, 4 LEDs can be used:
The diagram above shows what can be done with 4 wires. Here, we can start to see a pattern. With 2 wires, we could control 2 LEDs. With 3 wires, we could control 6 LEDs. For 4 wires, 12 LEDs.
We can calculate the number of LEDs you can light with a given combination using a formula:
n x (n-1) . So if we had 100 wires, we could light
100 x 99 = 9,900LEDs. If we wanted to light 90 LEDs, rearranging and solving the equation gives
n = 10, so 10 wires would be needed.
Neat huh? This is most useful when using a microcontroller or Raspberry Pi – you can control far more LEDs with less pins than if you were doing them all individually, and you don’t have to use expensive addressable LEDs (like Neopixels), multiplexing only uses plain ol’ LEDs.
One of the biggest uses of multiplexing is for LED displays:
For this project, the LEDs were connected in a grid: the rows were the anodes of the LEDs and the columns were the cathodes. Consider a grid of LEDs, connected in numbered rows and alphabetic columns, like the one below.
To turn on A2, we’d apply a voltage to row 2, and connect column A to ground. Simple right?
But if we wanted to turn on, say, A0 and H7, if we simply applied a voltage to rows 0 and 7, and grounded columns A and H, two more LEDs would light: A7 and H0.
So, the way around this is to very quickly scan through the rows faster than the eye can see, and light the desired LEDs in that row. When you scan through the rows quick enough, it looks like the desired LEDs are all on at the same time as the eye cannot detect the change.
Shift registers are often used to further reduce the number of microcontroller pins (they allow you to convert a single stream of serial data, to a number of streams of parallel data, so you only have to use one input signal to control a number of outputs) but that’s a story for another day! There are also chips designed specifically to do multiplexing and drive large numbers of LEDs.
Multiplexing is often used for most LED displays (the kind on the front of buses or at train stations). If you point your phone camera at the front of a bus, you sometimes won’t be able to read the screen, because the camera updates faster than the screen so you can see the lines being drawn.
This same principal can be used with other components too: for example, you could read from an 8×8 grid of buttons the same way.
There is lots more to multiplexing – but maybe I’ll cover that in a later post…..