Author Topic: Want to control 12 LEDs but there is not enough pins on Arduino?  (Read 2874 times)

0 Members and 1 Guest are viewing this topic.

Offline engineheatTopic starter

  • Frequent Contributor
  • **
  • Posts: 274
  • Country: us
Hi,

I have an application where I need 12 LEDs to light up at different situations. But each LEDs requires a signal pin and the Arduino Nano (can't use bigger Arduino due to other reasons) don't have enough pins since I also need to control some servos and stepper motor.

I would hate to use a 2nd Arduino because the LED response is related to the actions of other parts of the program. Theoretically I only need 4 bits to uniquely identify each LED, so I shouldn't need 12 pins. But how do I implement something like this?

Thanks
 

Offline mathsquid

  • Regular Contributor
  • *
  • Posts: 248
  • Country: us
  • I like math.
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #1 on: July 21, 2019, 07:31:46 pm »
You could try charlieplexing, or use a couple of 74hc595 shift registers.
 

Online Kjelt

  • Super Contributor
  • ***
  • Posts: 6586
  • Country: nl
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #2 on: July 21, 2019, 08:51:46 pm »
As they say above or what we used back in the good ol days a HEF4515B 4 bits to 16 outputs decoder.
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2418
  • Country: us
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #3 on: July 21, 2019, 09:00:20 pm »
led direct = 12 pins for 12 led's
led multiplexing = 7 pins for 12 led's
led charlieplexing = 4 pins for 12 led's
led shift register = 3 pins for any reasonable number of led's, since shift-registers can be chained.

Searches on any of those terms will give you the details very quickly.
 
The following users thanked this post: Psi, tooki, MyHeadHz, kulky64

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5170
  • Country: ro
  • .
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #4 on: July 21, 2019, 09:20:00 pm »
There's LED drivers ... some work just like shift registers.

Here's a 16 channel one for 1$ : https://www.digikey.com/product-detail/en/stmicroelectronics/STP16CPC26TTR/497-11985-1-ND/2772290

There's also versions with the pins more spread out, for example this one has 1.27mm spacing for the pins : https://www.digikey.com/product-detail/en/stmicroelectronics/STP08DP05MTR/497-6118-1-ND/1654043
You could bend each odd pin down so it goes directly in a 0.1" spaced circuit board and every even pin could be soldered to the next row of holes

Also multiplexing, for example have 3 groups of 4 leds ... power each group from a 5v pin so you have 3 pins reserved to send power to the leds (pins on output). Set the 4 pins as input to have current flow into the micro ... add resistors to limit current.  Power the leds you want from first group, wait a bit of time, turn off leds and power to first group, turn power to 2nd group and set the right leds, repeat..
This way you control 12 leds with 3+4 pins.

For a lot of current you could use something like ULN2003
 
The following users thanked this post: MarkF

Offline soldar

  • Super Contributor
  • ***
  • Posts: 3595
  • Country: es
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #5 on: July 21, 2019, 09:25:41 pm »
As they say above or what we used back in the good ol days a HEF4515B 4 bits to 16 outputs decoder.
Simplest and best.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2418
  • Country: us
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #6 on: July 21, 2019, 09:34:40 pm »
As they say above or what we used back in the good ol days a HEF4515B 4 bits to 16 outputs decoder.
Simplest and best.
A decoder doesn't work so well if you want more than one LED lit at a time, although you can likely cycle it so several appear lit. All of them at once is probably out of the question.
 

Online malagas_on_fire

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: pt
  • Kernel Panic
    • Malagas Lair
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #7 on: July 21, 2019, 09:47:24 pm »
Search keywords for i2c gpio expanders:

arduino i2c gpio expander pcf mcp

If one can make knowledge flow than it will go from negative to positve , for real
 

Offline soldar

  • Super Contributor
  • ***
  • Posts: 3595
  • Country: es
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #8 on: July 21, 2019, 10:27:25 pm »
A decoder doesn't work so well if you want more than one LED lit at a time, although you can likely cycle it so several appear lit. All of them at once is probably out of the question.
Very true. You can multiplex or you can have latches to sample and hold.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Offline BU508A

  • Super Contributor
  • ***
  • Posts: 4542
  • Country: de
  • Per aspera ad astra
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #9 on: July 21, 2019, 10:32:17 pm »
I suggest to use a shift register as a serial to parallel decoder.

Example:



Source:
https://prometec.org/actuators-and-sensors/arduino-and-shift-registers/
“Chaos is found in greatest abundance wherever order is being sought. It always defeats order, because it is better organized.”            - Terry Pratchett -
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2418
  • Country: us
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #10 on: July 21, 2019, 10:56:37 pm »
I suggest to use a shift register as a serial to parallel decoder.

Example:



Source:
https://prometec.org/actuators-and-sensors/arduino-and-shift-registers/
If only the person making that diagram paid more attention to being correct than using all the pretty colors. I'm sure he knew better, but that's not what he drew. Look at the yellow data line.
« Last Edit: July 21, 2019, 10:59:29 pm by Nusa »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9964
  • Country: us
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #11 on: July 21, 2019, 11:03:48 pm »
As long as an LED gets lit within 1/60th of a second of an event, you won't know whether is was coincident or not.  The only requirement is that when the dance stops, the proper lights are lit (within 1/60th second).  Actually, 1/30th may be ok as well.  It's an eye versus flicker thing.

You can EASILY use an IO Expander like the 16bit MCP23S17 running at as high as 10 MHz!  No problem shifting out the bits at some reasonable clock rate and meeting the 1/60th second thing.  Extra points if you use the hardware SPI gadget rather than bit-banging and doing it with an interrupt on each transfer means it will work by magic.  Just stick the LED values in a known location (an unsigned 16 bit int) and every time there is an interrupt, the latest value gets sent.  The SPI gadget can only handle 8 bit transfers but what you do is lower CS', send the high 8 bits, send the low 8 bits and then raise CS'.

Did I mention FAST!
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5170
  • Country: ro
  • .
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #12 on: July 21, 2019, 11:06:18 pm »
I suggest to use a shift register as a serial to parallel decoder.

Or just use an actual led driver IC like I suggested a few posts above, and you save pcb space by not having to use one resistor for each led, and you have finer control over the current.
 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #13 on: July 22, 2019, 12:56:45 am »
With only 12 LEDs I'd be tempted to just multiplex or charlieplex them directly. If you wanted to drive a larger number of LEDs the MAX7219 can handle 64 of them from just a few IO pins.
 

Online Kjelt

  • Super Contributor
  • ***
  • Posts: 6586
  • Country: nl
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #14 on: July 22, 2019, 05:19:58 am »
A decoder doesn't work so well if you want more than one LED lit at a time, although you can likely cycle it so several appear lit. All of them at once is probably out of the question.
The hef4000 were sturdy, IIRC  25mA per output max total 250mA or so, with modern he leds @4 to 6mA it is more then enough, so wouldn't be a problem at all.
 

Offline Dabbot

  • Regular Contributor
  • *
  • Posts: 198
  • Country: au
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #15 on: July 22, 2019, 08:05:36 am »
With only 12 LEDs I'd be tempted to just multiplex or charlieplex them directly. If you wanted to drive a larger number of LEDs the MAX7219 can handle 64 of them from just a few IO pins.

What is with the pricing for these chips?
Digikey / Mouser / element14: $15
Jaycar: $5 :o

TLC5916 is approaching something more reasonable.
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5170
  • Country: ro
  • .
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #16 on: July 22, 2019, 08:20:04 am »
You can still buy LED171596ARSLR (96 channel led driver) for 8$ : https://www.digikey.com/product-detail/en/texas-instruments/LED171596ARSLR/296-51223-1-ND/9745587

48 channel drivers are cheap at 4$ each or ~2.5$ each in 1000pcs, see for example TLC5954 : https://www.digikey.com/product-detail/en/texas-instruments/TLC5954RTQR/296-40587-1-ND/5178464

You can chain these or give each one unique i2c address ... no problem.

The Jaycar chips are most likely very old stock or they're clones/fakes.
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2418
  • Country: us
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #17 on: July 22, 2019, 10:45:30 am »
A decoder doesn't work so well if you want more than one LED lit at a time, although you can likely cycle it so several appear lit. All of them at once is probably out of the question.
The hef4000 were sturdy, IIRC  25mA per output max total 250mA or so, with modern he leds @4 to 6mA it is more then enough, so wouldn't be a problem at all.

I think you missed the point. If a decoder has more than one output enabled at a time, it would not be working properly, so there will never be more than one output pin drawing current.

My point was that trying to make them all appear lit at 1/16th (or 1/12th) duty cycle per LED is just asking for unsatisfactory results.
 

Offline Dabbot

  • Regular Contributor
  • *
  • Posts: 198
  • Country: au
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #18 on: July 22, 2019, 11:06:10 am »
Go to Banggood and get a WS2812B strip.
Cheap? Yes.
Overkill? Yes.
MCU pins: 1
Interface libs: Written.
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2507
  • Country: gb
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #19 on: July 22, 2019, 11:13:15 am »
https://playground.arduino.cc/Main/MAX72XXHardware/ provides easy control of up to 64 LEDs.... or go with the NeoPixels if you want colour!  :)
 

Offline autotel

  • Contributor
  • Posts: 24
  • Country: cl
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #20 on: July 22, 2019, 11:28:47 am »
Many ways to do it! It depends on the details.
  • My favorite for this case is using a 595 shift register because it can be tested easily on a breadboard. It just so happened I had kicad opened with a project that uses one (picture attached). The limitation with this approach is that the LED's can only be on or off.
  • If you are trying to use RGB led's or trying to have different levels of brightness, then the WS2812 is the best option.
  • You can also go with an analog multiplexor, however, the LED's will remain on only while their register is selected (meaning that they will look dim).
 

Offline kjr18

  • Regular Contributor
  • *
  • Posts: 205
  • Country: pl
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #21 on: July 22, 2019, 01:46:13 pm »
Go to Banggood and get a WS2812B strip.
Cheap? Yes.
Overkill? Yes.
MCU pins: 1
Interface libs: Written.

There are also some variants, like SK6812, they are warm white- cold white-amber type, compatible with WS2812 libs. Fast Led lib supports many types of leds. This might be a good way to start.
 

Offline max_torque

  • Super Contributor
  • ***
  • Posts: 1327
  • Country: gb
    • bitdynamics
Re: Want to control 12 LEDs but there is not enough pins on Arduino?
« Reply #22 on: July 22, 2019, 06:01:32 pm »
Hi,

 Can't use bigger Arduino due to other reasons


And those reasons are?  Generally these days, the cost of swapping in a micro with a few more pins is less than any other solution, especially if you only need a few more pins! (ie, if yo want to drive 300 leds, then yesm, you're going to have to use some form of serial to parallel conversion external to the micro, but if you just need say 5 more pins, just use a "bigger" micro. In the long terms that's often cheaper and easier!


If you really want to stick with the micro your have (and you need to have a very good reason to do that imo) then you need to tell us how many of the 12 leds need to be lit simultaneously, and how many "free" pins you currently have.  Without knowing that info, there are many ways to skin this particular cat.......
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf