Connecting 9 LEDs with a toggle switch for an artifical star field

Afternoon,

I am attempting to create a nine-LED layout. LEDs should all be turned on and off at the same time using a toggle switch.

I am trying to have this sorted by Friday for an astronomy demo (testing mirror alignment). I am creating a flat field for collimating telescopes by connecting fiber optic fibers to the LED's and running the fiber to the flat field.

I am using 5mm Diodi Led, 80 Pezz ulticolor.
Arduino 2560 R3 and breadboard with toggle switch.

Thank you for your time!

Welcome to the forum

Why are you using an Arduino if all you want to do is to turn on 9 LEDs ?

So I can learn something?

at the same times means the same nano-second or a few micro-seconds appart is fine?

(agree with @UKHeliBob, the easiest way is to let the switch send the current into the leds but of course it's fun to use an Arduino for that)

Well, the idea is to have an artificial starfield. As long as the light source is stable, it should be fine for testing mirror collimation/coma/tilt.

so do you know how to read a switch and turn a LED ON or OFF ?

Yes, I have a single led working atm. Though there is a strange bug where I use the toggle and have to switch it back on for it to turn on or off.

If you are determined to use an Arduino to do this then please post your current sketch and a diagram of your circuit

1 Like

Sure will do.

the code could be as dumb as

const byte switchPin  = 2;                    // pin 2 ---- button ---- GND
const byte ledPins[] = {3,4,5,6,7,8,9,10,11}; // pin --- R330Ω --- Anode [LED] Cathode --- GND

void setup() {
  pinMode(switchPin, INPUT_PULLUP);
  for (byte p: ledPins) pinMode(p, OUTPUT);
} 

void loop() {
  int state = (digitalRead(switchPin) == HIGH) ? LOW : HIGH; // HIGH when released, LOW when pressed so we inverse it
  for (byte p: ledPins) digitalWrite(p, state );
}
1 Like

Just don't attempt to drive all 9 LEDs from one pin. One pin per LED, with resistor, please!

better

const byte ledPins[] = {54,55,56,57,58,59,60,61};// not really used, just for reference

and then

digitalRead(switchPin)  ? PORTF=0 : PORTF=255;

sure, that's faster, but not newbie friendly :slight_smile:

he also mentioned 9 pins not 8, so will go beyond one port

you could also read the status of the switch using the port register

i hope if he learn to switch 8 the one additional pin is a piece of cake

still coud write this with proper type matching and using the ternary operator in the usual way and binary notation to show which bytes are touched :slight_smile:

PORTF = (digitalRead(switchPin) == HIGH) ? 0b00000000 : 0b11111111 ;

So each led requires its own pin and resistor?

Ah, so each led is connected to a pin, and has a 330 ohm resistor then goes to ground. Smart. Thank you.

something like this

330Ω is a current limiting resistor. The exact value depends on your LEDs and how bright you want them to be (and max current the port can give)

It's not required - but that way you need no extra hardware.

As well as the per-pin current limitation, remember that there is also a limit on the total combined current that a chip can take simultaneously through multiple pins...

1 Like

Yes, and at the same time no. The issue is how much current your particular type(s) of LEDs need, vs how much current a single output pin can sink/source. We generally simplify this to an LED per pin, but it needn't be that restrictive.
LED current - if you don't know the spec for your LED, best to use a selection of resistors and a 9V battery, start at 2 kohm and work down until you have the brightness you desire, then calculate the current. Say it's 4 ma.
Output sink/source - The manufacturer of your particular processor(depends on which Arduino product, or clone) will have a specification for absolute maximum and typical maximum per pin, plus a specification per a set of pins, plus a maximum for the whole device, and all of this typically derated depending on operating temperature. It gets complicated really quickly.
And then, of course, there's the likelihood that tomorrow, you'll decide, well, I really would like to be able to have three LEDs on, then six, then nine. Or some other variation. So easy if you're already driving them individually, otherwise it's rewire time.
So as you can see, "it depends" is about all we can say.
Sorry for the long read, if you even got this far.
Good news is, with the Mega, you're not pin-limited.