# How can I control 8 LEDs using only 5 pins of a microcontroller?

How can I control 8 LEDs using only 5 pins of a microcontroller without using any shift registers or I/O expanders?

By "controlling" I mean turning an LED or a set of LEDs on or off, not just turning one only at a time like Charlieplexing.

Edited : This query had a boundary of not using any devices or ICs other than the microcontroller, any possible number and combination of wires and resistors. Some answers tend to be useful for people who have the same question but using devices or ICs is allowed.

• If no shift registers, IO expanders, multiplexing or chaelieplexing is allowed, then what is allowed to achieve this? Two 4-bit D latches? Sep 20, 2020 at 13:11
• Charlieplexing can do that if you have enough drive current . Just time multiplex the LEDs that need to look like on at the same time. Sep 20, 2020 at 13:16
• Use the HC595 shift register would do exactly what you need. And it's very simple. Why don't you like it? Sep 20, 2020 at 13:19
• Could you explain why a shift register is not a good solution for you? Sep 20, 2020 at 13:31
• So it’s homework. Please show what you have tried so far and we’ll help you where you got stuck. Sep 20, 2020 at 19:10

8 leds on/off have 2^8 = 256 possible states

5 control pins on/off have 2^5 = 32 possible states

Since 32 < 256, and you can't use shift registers or i/o expanders, it would be impossible...

But if the 5 control pins are the pins of a MCU, you need to use only one of them for controling the 4 * 2 multiplexing process. One of the pins can be 0V or +Vcc, each of these two states enables a different set of 4 LEDs attached to the other 4 pins.

"he gave us a hint : use a wire connection combination if a problem appears try solving it in your code"

As this is a theoretical question in a curse, I think that this would be a possible correct answer. This is a kind of 2*4 multiplexing, using only 5 pins.

If you need to control a higher number of LED's, charlieplexing would allow to control untill 20 LED's.

simulate this circuit – Schematic created using CircuitLab

• That's a very interresting answer. But it can control only 4 leds at a time. (which is already better than just one) Sep 20, 2020 at 14:35
• Yes, but it is mathematically impossible to control all 8 leds at a time with 5 pins, if you can't use latches, shift registers, or other external components. OP said that he was thinking about 2*4 multiplexing; well, this proposed circuit is a kind 4*2 multiplexing using 5 pins. Sep 20, 2020 at 14:39
• @mguima Thank you so much for the effort , I'll try it with superimposing ( turning on and off LEDs for short time) to get a full control of the LEDs and I'll update you as soon as I finish. Sep 20, 2020 at 16:14
• @Mohamed Atef Pulsing high line 5 while pulsing low lines 1,2,3,4 controlling the led you want to turn on, then, on the next clock cycle, you pulse line 5 low and lines 1,2,3,2 high controlling the other 4 leds. You play on the visual effect. Should do the trick. Sep 20, 2020 at 17:01
• @MohamedAtef What you call "superimposing" or "optical delusion" is usually known as "charlieplexing" Sep 21, 2020 at 5:45

Use addressable LEDs like WS2812B or similar.

You’d need only 1 pin (plus power and ground) to control hundreds of them. They are available individually or as part of an LED strip (which can usually be cut into smaller strips or individual LEDs).

• I relate this answer is correct but is there any way to do this with normal LEDs ? I'm taking an online live course and our instructor told us it is possible with normal LEDs and made it a report for the next session and I didn't find anything on the internet about this configuration. Sep 20, 2020 at 13:13
• Are you allowed to use any other components apart from the LEDs themselves? Sep 20, 2020 at 13:20
• unlimited number of wires. he gave us a hint : use a wire connection combination if a problem appears try solving it in your code Sep 20, 2020 at 13:23
• The problem is that there’s no way that 5 i/o pins can represent all possible on/off states of 8 LEDs. The only possible solution I can think of is using something sequential (i.e. superimposing two or more states by quickly switching between them) or something that has memory (i.e. latches/flip flops or shift registers). Sep 20, 2020 at 13:34
• @MohamedAlef But you have ruled out all the methods to do this with normal LEDs. Sep 20, 2020 at 13:53

There are these things that are known as "movies", where an image is switched on and off rapidly, giving the feeble human mind the illusion that the image is continuous.

You can accomplish something similar by having a multiplexer (3 selector signals and a strobe signal) turn the 8 LEDs on (or not) one at a time.

And if you really want to get fancy you can add a capacitor to each LED to keep it lit for a period of time after it's signal is turned off.

• Movies don't switch an image off and on rapidly? Lights, however, do. Sep 21, 2020 at 11:01
• @Corsaka Old-style movie projectors (and camera) do "switch the image off and on" using a mechanical shutter to block the lignt. Shine light through a stationary frame of cine film. Block the light. Move the film. Shine the light through the next stationary frame. Repeat 24 times per second Sep 21, 2020 at 15:23
• Indeed, see the various 8x8x8 LED cube projects one can make. 512 LED's controlled by 2 x 8 = 16 lines.
– fiat
Sep 22, 2020 at 0:30

8x D flip-flops and a 8:1 multiplexer will do the job. Choose a D flip-flop whose value will change say on rising edge.

Connect "value" pin from µC to all the D flip-flops. Connect "clock" pin from µC to the 8:1 mix input. Connect each output of the mux to the clock signal of a D flip-flop. Connect 3 "address" pin of mux to µC.

Now, to change the state of a LED, you set your "value" pin, program the "address" pin to target the right D flip-flop, and finally, make a low-hi cycle on "clock" pin.

You now have full individual control of each LED.

• Thanks for the effort . I should've explained further in the question that this problem had a boundary of not using any devices or ICs (only any combination of wires) .This was mentioned several times in the comments. Sep 21, 2020 at 11:12
• No problem, it might always help someone else later! Sep 21, 2020 at 11:15
• That's why I marked the answer as a useful one :D Thanks again. Sep 21, 2020 at 11:18
• @user1532080 For further helping someone else later, it can also be said that there are "8-bit latch" IC's (e.g. 74xx259, 4099) that contains 8 flip-flops and a 8:1 decoder (multiplexer) inside, exactly as needed for this use. Sep 21, 2020 at 12:51