Is it possible to turn on pins 4 to 13 all at once?

Friends, I am very new to Arduino and the community as well, just started to learn sketching. Would you please tell me whether is it possible to turn on pins 4 to 13 all at once? I have connected a single LED to each pin. What I sketched is as under:

// put your setup code here, to run once:
void setup()
{
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop()
{
digitalWrite(4,HIGH);
delay(100);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
delay(100);
digitalWrite(5,LOW);
delay(100);
digitalWrite(6,HIGH);
delay(100);
digitalWrite(6,LOW);
delay(100);
digitalWrite(7,HIGH);
delay(100);
digitalWrite(7,LOW);
delay(100);
digitalWrite(8,HIGH);
delay(100);
digitalWrite(8,LOW);
delay(100);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
delay(100);
digitalWrite(10,HIGH);
delay(100);
digitalWrite(10,LOW);
delay(100);
digitalWrite(11,HIGH);
delay(100);
digitalWrite(11,LOW);
delay(100);
digitalWrite(12,HIGH);
delay(100);
digitalWrite(12,LOW);
delay(100);
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
}
But what is happening is that all the LEDs are not blinking at the same time or at once, what I want. Like LED 1 blinks once and waits for the next blink for all the LEDs to blink once.
I am sorry to put such a childish post in the community but please do understand that I only 5 days that I am with Arduino. God Bless You All.

Please help us keep the forum tidy. Edit your post using the :pencil2: in the tool bar just below your post

select the code part and press the <code/> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)

(For more tips ➜ do yourself a favour and please read How to get the best out of this forum)


To your question

Do you have a current limiting resistor per led? You do need that

At once as at the very same nano second or just that visually they appear to turn on at the same time ? (Just issue multiple digitalWrite() in a row in the latter case)


If you want non blocking code look at

2 Likes

If all pins are on the same port you could use direct port manipulation (there's your search term). To turn on a lot of pins quickly (but not exactly the same time) use a for loop.

Read the following diagram (Fig-1) and check which DPins belong to a single port and then you can use the following codes to turn ON/OFF you LEDs. There is no need to execute/write so many digitalWrite() functions

PORTD = 0bxxxxxxxx;  // x= 0 or 1 covers: DPin-0 (LSBit), 1, 2, 3, 4, 5, 6, 7
PORTB = 0bxxxxxx;   //covers DPin-8 (LSBit), 9, 10, 11, 12, 13

portstruc
Figure-1:

1 Like

Every program executes one instruction at a time and every instruction (1) requires some time, but you can certainly light-up a sequency of LEDs faster than the eye can see. :wink:

Depending the particular Arduino/processor there are Port Commands which can write 8 bits at-once in one instruction. (The ports are pre-defined so you can't choose which individual pins to use.)

An 8-bit processor is always internally processing one byte (8-bits) at a time anyway.

(1) The processor is actually running machine code (created when you compile) and one C++ instruction may be converted to several machine-code instructions, but it's still one machine-instruction at a time.

Get rid of all these delays, and the bits will all be set or reset within a few dozen microseconds. You will see the events happening "all at once".

I have connected a single LED to each pin.

Do you have the required current limiting resistor in series with each LED? If not, you will damage the port pins.

Do you mean something like this?

for (int i = 4; i <= 13; i++) {
  digitalWrite(i, HIGH);
}
delay(100);
for (int i = 4; i <= 13; i++) {
  digitalWrite(i, LOW);
}

That will (somewhat annoyingly IMHO) turn all of the pins 4 through 13 on at once, delay for 100 milliseconds, then turn them off again. When I say "at once", I mean that is what it will look like to a human observer.

Important If that is the only code in your loop statement, you will never see them turn off. They will turn off, but because they will turn back on again so quickly, you will never see it.

So you might want to use something more like this:

for (int i = 4; i <= 13; i++) {
  digitalWrite(i, HIGH);
}
delay(100);
for (int i = 4; i <= 13; i++) {
  digitalWrite(i, LOW);
}
delay(100);             // Give yourself time to see the "off cycle".

No, but I have connected a single 100 ohms resistor of 2 watt on the GND

You can use a single resistor, but only if only one led will be on at the same time. But you want to light more than one led at a time, you need a resistor per led.

2W resistors are total overkill for this. 1/4W or even 1/8W would be fine.

No to what ?

please fix post 1

// put your setup code here, to run once:
void setup()
{
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop()
{
digitalWrite(4,HIGH);
delay(100);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
delay(100);
digitalWrite(5,LOW);
delay(100);
digitalWrite(6,HIGH);
delay(100);
digitalWrite(6,LOW);
delay(100);
digitalWrite(7,HIGH);
delay(100);
digitalWrite(7,LOW);
delay(100);
digitalWrite(8,HIGH);
delay(100);
digitalWrite(8,LOW);
delay(100);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
delay(100);
digitalWrite(10,HIGH);
delay(100);
digitalWrite(10,LOW);
delay(100);
digitalWrite(11,HIGH);
delay(100);
digitalWrite(11,LOW);
delay(100);
digitalWrite(12,HIGH);
delay(100);
digitalWrite(12,LOW);
delay(100);
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
}

While others have suggested improvements, alternatives, and rabbit holes, let's go back to the original post. In the interest of brevity, I'll do two, and you'll get the idea, I'm sure.

void setup()
  {
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  }
void loop()
  {
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  delay(100); 
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  delay(100);
  }

That easy. However, read through some of the other suggestions, in particular the array one, because you'll find the amount of typing done this way is tedious.
HTH

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.