 # Using Lots of LEDs (Charlieplexing)

## Problem

You want to control lots of LEDs using as few GPIO pins as possible.

## Solution

The way to do this is to use a technique called Charlieplexing. The name comes from the inventor, Charlie Allen of the company Maxim, and the technique takes advantage of the feature of GPIO pins that allows them to be changed from outputs to inputs while a program is running. When a pin is changed to be an input, not enough current will flow through it to light an LED or influence other pins connected to the LED that are set as outputs.

Figure 4-1 shows the arrangement for controlling six LEDs with three pins.

Figure 4-2 shows the breadboard layout for the LEDs and resistors.

To make this recipe, you will need:

Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called charlieplexing.py.

This example code prompts you to enter a number between 0 and 5 and then lights one of the six LEDs:

````import` `RPi.GPIO` `as` `GPIO`

`pins` `=` `[``18``,` `23``,` `24``]`

`pin_led_states` `=` `[`
`[``1``,` `0``,` `-``1``],` `# A`
`[``0``,` `1``,` `-``1``],` `# B`
`[``-``1``,` `1``,` `0``],` `# C`
`[``-``1``,` `0``,` `1``],` `# D`
`[``1``,` `-``1``,` `0``],` `# E`
`[``0``,` `-``1``,` `1``]`  `# F`
`]`

`GPIO``.``setmode``(``GPIO``.``BCM``)`

`def` `set_pin``(``pin_index``,` `pin_state``):`
`if` `pin_state` `==` `-``1``:`
`GPIO``.``setup``(``pins``[``pin_index``],` `GPIO``.``IN``)`
`else``:`
`GPIO``.``setup``(``pins``[``pin_index``],` `GPIO``.``OUT``)`
`GPIO``.``output``(``pins``[``pin_index``],` `pin_state``)`

`def` `light_led``(``led_number``):`
`for` `pin_index``,` `pin_state` `in` `enumerate``(``pin_led_states``[``led_number``]):`
`set_pin``(``pin_index``,` `pin_state``)`

`set_pin``(``0``,` `-``1``)`
`set_pin``(``1``,` `-``1``)`
`set_pin``(``2``,` `-``1``)`

`while` `True``:`
`x` `=` `int``(``raw_input``(``"Pin (0 to 5):"``))`
`light_led``(``x``)````

## Discussion

To understand how Charlieplexing works, imagine that you want to light LED A in Figure 4-1. An LED will only light when its positive lead is high, and its negative lead is low. If the voltage is the other way around, it will not light. To light LED A, you need its lead connected to GPIO 18 (via a resistor) to be high and the other lead to LED A, connected to GPIO 23 by a resistor, to be low. However, you must also make sure that GPIO 24 is set to be an input; otherwise, LED C or D will also light depending on whether GPIO 24 is high or low.

The array pin_led_states holds the settings for each GPIO for each of the six LEDs. If the value is 0, the pin is low; 1 means high and -1 means set to be an input.

The number of LEDs that can be controlled per GPIO pin is given by the formula:

LEDs = n2 - n

Using 4 pins, we can have 16, 4, or 12 LEDs, whereas 10 pins would give you a massive 90 LEDs.

In this example, you’re lighting only one LED at a time. To light more than one at a time, you need to run a refresh loop that keeps the desired state of the LEDs in an array and refreshes the display, turning on the LEDs that need to be on before moving on to the next. It must do this sufficiently fast so that it appears that more than one of the LEDs is on at the same time.

The more LEDs you use when it comes to making it appear that more than one LED is on at a time, the less time the LED will actually be lit, and the dimmer the LEDs will become.