5

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.

JRE
  • 67,678
  • 8
  • 104
  • 179
Mohamed Atef
  • 105
  • 1
  • 9
  • 5
    If no shift registers, IO expanders, multiplexing or chaelieplexing is allowed, then what is allowed to achieve this? Two 4-bit D latches? – Justme Sep 20 '20 at 13:11
  • 2
    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. – carloc Sep 20 '20 at 13:16
  • using any wire combination between the LEDs and the pins and solving the problem in the code (if there was a problem) – Mohamed Atef Sep 20 '20 at 13:17
  • 1
    Use the HC595 shift register would do exactly what you need. And it's very simple. Why don't you like it? – Fredled Sep 20 '20 at 13:19
  • Charlieplexing insures that I can turn on one led at a time , This doesn't mean I have fully control to turn on / of any Led(or set of LEDs) at the same time – Mohamed Atef Sep 20 '20 at 13:19
  • The multiplexing technique I know needs 6 pins to do that ( 2 *4 ) do you mean this ? I'm only limited to 5 pins and What do you mean by "time" in time multiplexing – Mohamed Atef Sep 20 '20 at 13:21
  • @Fredled This is a kind of an open - search report I have I'm allowed to use the internet to get the knowledge to solve this. and I'm fully aware of i/o expanders , shift registers , multiplexing , charlieplexing .. none of them met the boundaries of this problem – Mohamed Atef Sep 20 '20 at 13:26
  • 2
    Could you explain why a shift register is not a good solution for you? – Fredled Sep 20 '20 at 13:31
  • In my opinion They are a very very good solution , but this is a problem with boundaries . I didn't set this challenge by myself , an instructor gave us this as an open - source / book report and he will answer it next session. – Mohamed Atef Sep 20 '20 at 13:32
  • (Copied from the comments from my answer) 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). – StarCat Sep 20 '20 at 13:36
  • @StarCat hmmmm I thought of using 2*4 multiplexing but this may consume an extra pin ( as I have only 5 pins in hand). Do you think I can solve this problem by superimposing things and compensate the sixth pin ? – Mohamed Atef Sep 20 '20 at 13:38
  • Is this homework? – winny Sep 20 '20 at 19:03
  • @winny Not a homework this is an open-book / source report made by my instructor and he has a solution for it. – Mohamed Atef Sep 20 '20 at 19:07
  • 6
    So it’s homework. Please show what you have tried so far and we’ll help you where you got stuck. – winny Sep 20 '20 at 19:10
  • @mguima Check his answer I'm currently trying it – Mohamed Atef Sep 20 '20 at 19:25

4 Answers4

14

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.

schematic

simulate this circuit – Schematic created using CircuitLab

mguima
  • 1,425
  • 11
  • 20
  • 1
    That's a very interresting answer. But it can control only 4 leds at a time. (which is already better than just one) – Fredled Sep 20 '20 at 14:35
  • 3
    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.** – mguima Sep 20 '20 at 14:39
  • 1
    @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. – Mohamed Atef Sep 20 '20 at 16:14
  • 1
    @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. – Fredled Sep 20 '20 at 17:01
  • @Fredled you mean "you pulse line 5 low and lines 1,2,3,4 high " this is supposed to give me the ability of making them all look turned on right ? – Mohamed Atef Sep 20 '20 at 17:15
  • 1
    I think you got your exponents backwards. 8 LEDs present 2^8 = 256 combos and 5 control bits provide 2^5 = 32 combos. Even if you allow for the control bits to have 3 states that's 3^5 = 243 combos, not enough to cover 256 cases. So it does seem some form of multiplexing is required, which makes for a rather disappointing answer. – td127 Sep 20 '20 at 18:15
  • @td127 really, I've made a mistake at the mathematic part, 8 LED's on/off: 2^8, not 8^2, same mistake in control pins' math. Sorry for this, it was a sunday morning lazyness; I edited the answer to correct this part. But, the math for the proposed circuit is not 3^5. The proposed circuit envolves multiplexing, so, the circuit doesn't need to deal with all the 256 states at the same time; the proposed circuit alternately flashes the states of the two sets of 4 LEDs each set; so, 2^4 (for each set). Flickering between two states for each set, we have (2 ^ 4) ^ 2 = 256 – mguima Sep 20 '20 at 18:55
  • @td127 I edited the anwer to correct the mathematical flaw, and to make it clear that this solution doesn't need to use the third (high-Z) state of any of the pins. The point is, if one of the control pins can assume Vcc OR 0V, this pin itself has binary states, so it alone can play the "2" part in the 4 * 2 multiplexing scheme. – mguima Sep 20 '20 at 19:13
  • @mguima I've tried it with some of optical delusion ( through switching pins form high to low quickly) it worked ! – Mohamed Atef Sep 20 '20 at 20:12
  • Thank you so much @mguima , StarCat – Mohamed Atef Sep 20 '20 at 20:14
  • unsigned char single_led[8] = {0x1E , ~0x1E , 0x1D , ~ 0x1D , 0x1B , ~0x1B , 0x17 , ~ 0x17} ; based on the picture you gave me this is the array I can Iterate on to access a unique led – Mohamed Atef Sep 20 '20 at 20:17
  • and a delay of 5 ms can make them all ( or a set of them ) look like they are turned on together – Mohamed Atef Sep 20 '20 at 20:18
  • @MohamedAtef Atef I looks like you understood what I meant by "pulsing low and high". :) I'm glad it worked because I was not sure. – Fredled Sep 20 '20 at 23:42
  • 1
    Okay, but only 4 resistors are required, one each for outputs 1..4. – Spehro Pefhany Sep 21 '20 at 04:50
  • 2
    @MohamedAtef What you call **"superimposing"** or **"optical delusion"** is usually known as **"charlieplexing"** – slebetman Sep 21 '20 at 05:45
  • @selbetman nope , charlieplexing allows us to turn on maximum 4 out of 8 LEDs at a time ( or just turning on one LED at a time). adding visual delusion makes the human eye think that all the 8 LEDs are turned on at the same time. – Mohamed Atef Sep 21 '20 at 09:23
  • @mguima I'm sure this answer is sort of a charlieplexing technique , but the obly charlieplexing I know with 5 pins makes us able to connect( (5 ^2 ) - (5) ) LEDs – Mohamed Atef Sep 21 '20 at 09:24
  • So, What is the name and source of your good technique ? Or you just developed it ? – Mohamed Atef Sep 21 '20 at 09:25
  • 1
    @MohamedAtef Seriously, it's [Charlieplexing](https://en.wikipedia.org/wiki/Charlieplexing). It's a particular variant of multiplexing. See also https://en.wikipedia.org/wiki/Multiplexed_display . – Sneftel Sep 21 '20 at 11:49
  • @MohamedAtef Please, I didn't invented or developed anything new! I just proposed a casual solution for the question, following the constraints (no external components, common LEDs, just wire the LEDs and address complexities in code). I wonder that the solution that your instructor will provide is the same. If I were to give it a name, I would call it just "multiplexing". The only "thing" that I really did was to show a way of multiplex (because you were thinking that 6 pins were needed for 2 *4 multiplexing). – mguima Sep 21 '20 at 12:22
  • I wouldn't call this "Charlieplexing" just because the high-Z "third-state" of MCU pins was not necessary. I tought that what is called charlieplexing is a technique that really needs this third state; if the charlieplexed pins and LEDs were driven only with binary ON / OFF states, the display wouldn't work as intended, as some LED's would light at unwanted time. In conclusion, in spite of those naming issues, fact is, whatever we call it, the good thing is that the circuit worked as you wanted. – mguima Sep 21 '20 at 12:35
  • @SpehroPefhany Yes, only 4 resistors are needed. I edited the answer. – mguima Sep 21 '20 at 12:42
  • I think you could get away with just one current limiting resistor, on D5. Provided you do not light more than 1 LED at a time – Lorraine Sep 21 '20 at 12:56
  • So you have the credit for this approach ? Or it is based on previous experience ? I need to mention my resources in the report. – Mohamed Atef Sep 21 '20 at 12:56
  • 1
    @MohamedAtef No, I don't deserve any kind special credit, really! I just wrote an answer, but there is no really creative work in the proposed solution. This solution is trivial for anyone with an intermediary level of experience and knowledge. – mguima Sep 21 '20 at 12:59
  • This solution has been much of a help ! Thanks. – Mohamed Atef Sep 21 '20 at 13:01
  • @MohamedAtef: There's a question of whether, from the perspective of citations, your professor is treating this assignment akin to a research paper or akin to homework. In the latter case, your professor may have an expectation that you cite sources that provided "homework help", even if that help was ordinarily unsuitable for citation. – Brian Sep 21 '20 at 17:11
10

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).

StarCat
  • 1,124
  • 1
  • 7
  • 13
  • 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. – Mohamed Atef Sep 20 '20 at 13:13
  • Are you allowed to use any other components apart from the LEDs themselves? – StarCat Sep 20 '20 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 – Mohamed Atef Sep 20 '20 at 13:23
  • 2
    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). – StarCat Sep 20 '20 at 13:34
  • hmmmm I thought of using 2*4 multiplexing but this may consume an extra pin ( as I have only 5 pins in hand). Do you think I can solve this problem by superimposing things and compensate the sixth pin ? – Mohamed Atef Sep 20 '20 at 13:36
  • 2
    @MohamedAlef But you have ruled out all the methods to do this with normal LEDs. – StainlessSteelRat Sep 20 '20 at 13:53
6

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.

Hot Licks
  • 752
  • 6
  • 10
  • 1
    Movies don't switch an image off and on rapidly? Lights, however, do. – Corsaka Sep 21 '20 at 11:01
  • 4
    @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 – alephzero Sep 21 '20 at 15:23
  • 1
    Indeed, see the various 8x8x8 LED cube projects one can make. 512 LED's controlled by 2 x 8 = 16 lines. – fiat Sep 22 '20 at 00:30
1

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.

user1532080
  • 177
  • 7
  • 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. – Mohamed Atef Sep 21 '20 at 11:12
  • 1
    No problem, it might always help someone else later! – user1532080 Sep 21 '20 at 11:15
  • That's why I marked the answer as a useful one :D Thanks again. – Mohamed Atef Sep 21 '20 at 11:18
  • 1
    @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. – mguima Sep 21 '20 at 12:51