Want to control 12 LEDs but there is not enough pins on Arduino? - Page 1
Forum section: electronics / beginners

author: engineheat
author_rank: Frequent Contributor
timestamp: July 21, 2019, 07:20:15 pm
content: 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

author: mathsquid
author_rank: Frequent Contributor
timestamp: July 21, 2019, 07:31:46 pm
content: You could try charlieplexing, or use a couple of 74hc595 shift registers.

author: Kjelt
author_rank: Super Contributor
timestamp: July 21, 2019, 08:51:46 pm
content: As they say above or what we used back in the good ol days a HEF4515B 4 bits to 16 outputs decoder.

author: Nusa
author_rank: Super Contributor
timestamp: July 21, 2019, 09:00:20 pm
content: led direct = 12 pins for 12 led'sled multiplexing = 7 pins for 12 led'sled charlieplexing = 4 pins for 12 led'sled 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.

author: mariush
author_rank: Super Contributor
timestamp: July 21, 2019, 09:20:00 pm
content: 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/2772290There'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/1654043You 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 holesAlso 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

author: soldar
author_rank: Super Contributor
timestamp: July 21, 2019, 09:25:41 pm
content: Quote from: Kjelt on July 21, 2019, 08:51:46 pmSimplest and best.

author: Nusa
author_rank: Super Contributor
timestamp: July 21, 2019, 09:34:40 pm
content: Quote from: soldar on July 21, 2019, 09:25:41 pmA 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.

author: malagas_on_fire
author_rank: Frequent Contributor
timestamp: July 21, 2019, 09:47:24 pm
content: Search keywords for i2c gpio expanders:arduino i2c gpio expander pcf mcp

author: soldar
author_rank: Super Contributor
timestamp: July 21, 2019, 10:27:25 pm
content: Quote from: Nusa on July 21, 2019, 09:34:40 pmVery true. You can multiplex or you can have latches to sample and hold.

author: BU508A
author_rank: Super Contributor
timestamp: July 21, 2019, 10:32:17 pm
content: 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/

author: Nusa
author_rank: Super Contributor
timestamp: July 21, 2019, 10:56:37 pm
content: Quote from: BU508A on July 21, 2019, 10:32:17 pmIf 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.

author: rstofer
author_rank: Super Contributor
timestamp: July 21, 2019, 11:03:48 pm
content: 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!

author: mariush
author_rank: Super Contributor
timestamp: July 21, 2019, 11:06:18 pm
content: Quote from: BU508A on July 21, 2019, 10:32:17 pmOr 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.

author: james_s
author_rank: Super Contributor
timestamp: July 22, 2019, 12:56:45 am
content: 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.

author: Kjelt
author_rank: Super Contributor
timestamp: July 22, 2019, 05:19:58 am
content: Quote from: Nusa on July 21, 2019, 09:34:40 pmThe 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.

author: Dabbot
author_rank: Regular Contributor
timestamp: July 22, 2019, 08:05:36 am
content: Quote from: james_s on July 22, 2019, 12:56:45 amWhat is with the pricing for these chips?Digikey / Mouser / element14: $15Jaycar: $5TLC5916 is approaching something more reasonable.

author: mariush
author_rank: Super Contributor
timestamp: July 22, 2019, 08:20:04 am
content: 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/974558748 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/5178464You 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.

author: Nusa
author_rank: Super Contributor
timestamp: July 22, 2019, 10:45:30 am
content: Quote from: Kjelt on July 22, 2019, 05:19:58 amI 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.

author: Dabbot
author_rank: Regular Contributor
timestamp: July 22, 2019, 11:06:10 am
content: Go to Banggood and get a WS2812B strip.Cheap? Yes.Overkill? Yes.MCU pins: 1Interface libs: Written.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: July 22, 2019, 11:13:15 am
content: https://playground.arduino.cc/Main/MAX72XXHardware/provides easy control of up to 64 LEDs.... or go with the NeoPixels if you want colour!

author: autotel
author_rank: Contributor
timestamp: July 22, 2019, 11:28:47 am
content: 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).

author: kjr18
author_rank: Regular Contributor
timestamp: July 22, 2019, 01:46:13 pm
content: Quote from: Dabbot on July 22, 2019, 11:06:10 amThere 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.

author: max_torque
author_rank: Super Contributor
timestamp: July 22, 2019, 06:01:32 pm
content: Quote from: engineheat on July 21, 2019, 07:20:15 pmAnd 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.......