How to control 28 LEDs with an Arduino

Hi!

It is a project in which there is one crossroad with four traffic lights (LEDs). The lights is for cars and for pedestrians. So one traffic light has three red LED, three green LED and one yellow LED. In total this crossroad has four traffic lights, with 12 piece of red, 12 piece of green and four piece of yellow LED.

But to control this traffic lights, it is enough to control just 14 LEDs out there. I want to use Arduino Nano in this project.

The idea is to control these LEDs through demux, or something else. What can I use to achieve this goal: two piece of 3 line to 8 line demux?

I don't know about multiplexers but I think you would get a better reply if you change your title (edit your Original Post) so it reads "How to control 28 LEDs with an Arduino"

...R

You could multiplex. Only multiplexing I know can only have 1 led (or 1 row) on at a time though.

You could use shift registers. https://www.arduino.cc/en/Tutorial/ShiftOut Only thing there is the 74HC595 chip can only give so much current total, you may want to put transistors between the shift registers and the leds but that chip is cheap and so are BJT's and resistors.

There are shift-register-type led driver chips that can source 150mA per pin. They cost more and do more than you need but they need less wiring.

With shift regs you send a string of bytes on the SPI bus pins, 8 per chip, and the leds light up if their bit is 1, that simple. For each condition the lights should be in there will be one string to send, in your case 3 bytes.

I forgot to mention that an Arduino Mega would have enough I/O pins so you would not need shift registers or whatever.

…R

Or just use a MAX7219, control up to 64 LEDs with just 1 chip. I even offer board to break them all out to their own pairs of wires/pins for each one.
http://www.crossroadsfencing.com/BobuinoRev17/

I think I am going to use shift registers in this project.

Okay, so daisy chain 4 shift registers, and send them data from an array of 4 bytes:

digitalWrite (latchPin,LOW); // D10 connected to RCLK of all 4 chips
SPI.transfer(array[0]; // D11 to data in first chip, D13 to SRCLK of all 4 chips
SPI.transfer(array[1];
SPI.transfer(array[2];

SPI.transfer(array[3];

digitalWrite (latchPin,HIGH); // outputs update on this rising edge

Need these also,
at the top of your sketch:

#include<SPI.h>
byte latchPin = 10;

and in setup():
SPI.begin();
pinMode (latchPin, OUTPUT);

There was another similar thread recently, and for a really 'realistic' simulation that is flexible to configure across multiple junctions is a real challenge!

Pedestrians, split & mixed turning lanes, level crossings and timed inter-junction traffic made for an interesting weekend of planning. Remember background timers for ped flashers etc. A lot of fun... all synchronised with a state machine.

lastchancename: There was another similar thread recently, and for a really 'realistic' simulation that is flexible to configure across multiple junctions is a real challenge!

Pedestrians, split & mixed turning lanes, level crossings and timed inter-junction traffic made for an interesting weekend of planning. Remember background timers for ped flashers etc. A lot of fun... all synchronised with a state machine.

Those are the conditions I mentioned, each has 1 set of correct lights and timing. If you add sensors (even pedestrian crossing buttons) then possibly some conditions would have alternate next-condition outcomes.

I wouldn't care to think of what it would take to code that without a state machine. It'd be large and messy.

@GoForSmoke wrote "I wouldn't care to think of what it would take to code that without a state machine. It'd be large and messy."

That was exactly what I started looking at recently, and the number of variables to accommodate all the sensor, indicator and timing and route options became overwhelming - considering i didn't have an application for it!

i think i captured all the variables, but decided to drop it when i saw what it was going to involve.

I did something similar about 30 years ago, called CARS (computerised adaptive routing system), but newer processing and tchnology made it interesting to revisit.

The number of elements is large. Reduced to bits though, it all might fit in a few K of flash.

Yep, though a lot of async overlapping timers used a few longs

It could be done with just one 16 bit timer (65 sec max interval), and that could make control easier.

Well, I want to create just a simplest crossroad here: |500x500 As one can see here, one traffic light has only one ( red-yellow-green ) lights for cars, and two ( red-green ) lights for pedestrians. So for control this crossroad with these lights it is enough to control just the traffic light number 1 and 2. The 3. should be wired to 1., and the 4. to 2. So what is the simplest way to control this crossroad? With shift registers, or with what?

The shift registers will allow all the leds ON at the same instant instead of multiplex flickering which is okay up to perhaps 5 at once. You will still need to connect transistors if you want all lights full bright but for demo that might not matter.

They also simplify making light patterns to bytes you send them, 22 lights needs 3 bytes, 3 8-bit registers, only when a change is needed.

With the intersection you have different car and people traffic patterns. Determine those, all of the ones you want to allow. You have cars going north and south and able to turn while cars east and west are stopped and then the switch to stop north and south and allow east and west but you also have pedestrians.. when they cross it is a pattern or part of one.

Make some drawings with arrows on each showing who can move where for each desired traffic condition. From those, which lights should be ON can be figured out and matched to each condition and time for that also noted. Make sure that yellow (caution) light conditions are separate. Figure out the flow going from one condition to the next, here is simplified no pedestrians;

N/S cars open, E/W cars blocked, 30s ----> N/S cars caution, E/W cars blocked, 10s ----> N/S cars blocked, E/W cars open, 30s ----> N/S cars blocked, E/W cars caution, 10s ----> back to start.

When you add pedestrians, it gets more. If the sequence is interrupted, always go to a condition desired and continue as usual from there. Be glad you don't have turn arrows too.

You do this and your logic only has to choose a condition. The lights will be set by what condition, each has 3 bytes to send through SPI always the same per condition. The lights figured out ahead of time, stored as bytes, no per-led code except sending the bytes is needed. ---- most of the work is done ahead and saved as data, the sketch doesn't have to at run time. These are very old techniques, by the way.