3

I want to show a program state with LEDs. There are 22 steps, each step is represented by an LED.

  • only one LED is on at a time
  • the sequence is set (forward or backwards).

I have only one pin left on my MCU, maybe two.

Basically, I'd like to have the pin work as a 'clock'. For every pulse, the "next" LED is switched on, all other LEDs are off.

I could use a clock to binary IC. (e.g. 74HC393 Binary Counter) This would create a nibble, which I could then feed into a multiplexer (e.g. 700-MAX336CPI+) which would switch one of its 16 outputs on at a time. (I can live with 16 steps).

I want to make it relatively cheap. I wonder if there is a more elegant solution. Maybe with a specialized IC, or if there is a solution with simple discrete components, oh, maybe I could even use a little servo to point at the right step... What comes to your mind?

Null
  • 7,448
  • 17
  • 36
  • 48
lode
  • 359
  • 2
  • 5
  • 12
  • 8
    Chain two decade counters (e.g. CD4017) you'll get a "decimal" display – Mat Jan 11 '22 at 22:28
  • 2 or 3 cascaded 4094B 8-Stage Shift and Store Bus Registers. D and Clock inputs. –  Jan 11 '22 at 22:44
  • Do you or do you not need for the display to move **both** forwards and backwards? If forward only, then I also recommend 4017's. – AnalogKid Jan 11 '22 at 23:01
  • yes, both forward and backward, but, backward would be pulsing it quickly 21 times – lode Jan 11 '22 at 23:11
  • So not actually backwards, just forwards + any form of reset? – FeRD Jan 12 '22 at 07:33
  • 1
    Does this answer your question? [How to Cascade 4017 Decade Counters?](https://electronics.stackexchange.com/questions/33652/how-to-cascade-4017-decade-counters) – Damien Jan 12 '22 at 08:58
  • What does "relatively cheap" mean? For some people that means <$10. For others it means <$100. For others yet it means <$1000. What MCU are you using now? Is $7 too expensive for an MCU? Is $2 too expensive for an MCU? Are you making just one of these, or are you intending to manufacture? How much do you care about cluttering up your BOM? – J... Jan 12 '22 at 16:46

7 Answers7

10

Upgrade you MCU to one with more pins. It will be cheaper than adding more parts.

Rodo
  • 875
  • 4
  • 15
8

Addressable RGB LED strips work with one single data pin.

Justme
  • 127,425
  • 3
  • 97
  • 261
  • To add some context: WS2812b is the type of the most often used addressable LED in an SMD 5050 package on a strip. APA106 would be a near-compatible LED type in a standard 5mm round-hat LED package ("F5"). Both of these work with a one-wire protocol, which needs a precision of plusminus 150ns in the timing of the data protocol; i.e. you'll be blocking your µC for the duration of the data transfer. There also is e.g. APA102(c) (in an SMD 5050 package), which uses a two-wire protocol, which is less prone to timing issues. – orithena Jan 12 '22 at 10:15
  • An I2C port expander would work as well. – Michael Jan 13 '22 at 08:09
6

I'd use 3 4017 counters, chained together. Just clock the chain from your microcontroller. The 4017 is very cheap, has an output for each LED (so no decoding), and can be set up very easily to be chained. You would need 3 for your application. The 23 pin output should just reset them all to restart the count.

Edit: You might be able to hack up something using diodes and pull-downs to test whether the 'reset' (ie, the 23 output) is high using the clock pin. A diode from 23 to the clock output through a 10k resistor, with a 100k resistor at the pin as a pull-down. When the clock is being driven low by the uC, momentarily change it to an input. If it stays low, it's not the end yet. You might also have to test 24 (ie, have them both connected to clock through a diode and 10k resistor) since the pullup will probably clock it again (but only once, until you drive it low again). Not sure, haven't tried it, but probably.

Much easier just to blow another uC pin to test 23, but you said you didn't want to do that.

bob_monsen
  • 351
  • 1
  • 5
  • CD4017 "decade counter" is the answer to my question and is putting me on track. Indeed cheap. Thanks. – lode Jan 11 '22 at 23:11
  • https://electronics.stackexchange.com/questions/33652/how-to-cascade-4017-decade-counters – Damien Jan 12 '22 at 08:58
  • Connecting the micro's output pin to the 4017 clock input *and* to an R,C,R network feeding MR on the 4017 probably would be simpler than "something using diodes and pull-downs". To clock the 4017 send a short pulse then delay briefly to let voltage on C drop. To reset the 4017 send pulse long enough to raise voltage on C above reset threshhold. (This supposes positive going reset which might not be so) – James Waldby - jwpat7 Jan 12 '22 at 21:17
5

Use a second MCU.

Connect to it using one I/O pin and implement a UART protocol between the two in software. That gives you a whole data byte in which to encode your LED pattern control, which is tons as you describe the requirements.

You can buy a 28-pin MCU that will do the trick for under a quid (£1). That gives you your 22 I/O pins for direct LED driving with only one ever on, plus 1 I/O for host MCU connection.

This is the smallest circuit with the fewest ICs for direct LED drive: one IC, probably one crystal, two decoupling capacitors. Saves on a load of shift registers etc. and uses cheap LEDs and resistors.

If you're happy to put the LEDs in an multiplexed drive array, you could use fewer resistors and fewer MCU pins, so a smaller MCU. The example circuit below shows 20 LEDs in an array, driven by 5 MCU I/Os and with 4 MCU I/Os sinking the return current.

You'd need a 5x5 LED array so you'd use 10 MCU I/O pins. With 1 more I/O pin for UART receiving, you could use a 14-pin or 16-pin MCU.

enter image description here

Image from Nduli's World

On top of the part cost, you'll pay program development/testing time and expense. If it's just you on your own, not a professional job, then that's less important. If it's a professional job, that is a consideration to be costed out.

TonyM
  • 21,742
  • 4
  • 39
  • 62
  • ... or even a 14-pin MCU! – Martin Rosenau Jan 12 '22 at 09:47
  • @MartinRosenau, very much, thanks. I didn't have the energy to put that in the original answer late last night but I've put it in now. – TonyM Jan 12 '22 at 11:38
  • With some charlieplexing, it could be done in 7 pins, (4x3)x2=24. – penguin359 Jan 12 '22 at 17:43
  • 2
    @penguin359, actually I was keen to avoid it subsiding into a 'how do I drive LEDs with fewest pins' answer, with the accompany mountain of comments. That's been done too many times before on the site. The OP will get the point from the direct and muxed examples given. – TonyM Jan 12 '22 at 20:11
2

Your proposed solution with a pulse to advance to the next LED sounds doable.

With two dedicated pins (data+clock) you could use I2C (bit-banged in software if required) and connect a couple I/O expanders or a multi-channel led driver.

With a single data pin, you could try an addressable RGB LED strip or your own arrangement of daisy-chained addressable led chips on a PCB. They're pretty cheap, but they will need 5V and a level shifter.

You can also look at the 1-wire family of chips.

A solution with a servo and dial would definitely have some character, if you can live with the noise ;)

DamienD
  • 3,093
  • 1
  • 14
  • 23
1

Like Justme I would suggest the use of addressable LEDs, like the ubiquitous WS2812 (single pin needed, weird timing-based protocol, but very widely supported), or the APA102 (two pins needed, standard SPI).

The LEDs are daisy-chained, so you send commands on the wire(s) and you can change all LEDs at once.

This may be overkill in you case as:

  • They are RGB LEDs
  • You can make any combination you want

But the simplicity and additional flexibility may be useful!

jcaron
  • 1,720
  • 8
  • 16
0

the sequence is set (forward or backwards).

Even if you use 74HC4017 you'll need a second pin for reset.

If you want to scan in both directions, you need a clock pin and a direction pin, so 2 pins. With 2 pins you can use chained 74HC595 shift registers.

If you don't have two pins free, you can move one of the outputs like another LED to one of the HC595 outputs, or mux some buttons to save a pin.

bobflux
  • 70,433
  • 3
  • 83
  • 203
  • 1
    You can use the 23rd output to reset the thing. This is an old trick with these counters. Both directions can't really be done with the 4017 solution. – bob_monsen Jan 12 '22 at 00:02
  • 3
    @bob_monsen you can do both directions, just send N-1 pulses quickly to light the previous LED – bobflux Jan 12 '22 at 07:08
  • @bobflux: Additionally, if one connects the first LED output to the clock wire via resistor, one could reset the state of the device by switching an output repeatedly between high and floating. – supercat Jan 12 '22 at 23:41
  • 1
    @bobflux I like it. zoom through the sequence! – bob_monsen Jan 13 '22 at 04:38
  • You still need a power on reset to make sure it starts at the first LED, but it doesn't have to me a microcontroller IO – bobflux Jan 13 '22 at 07:16