4 LEDs to 1 Arduino pin

Hello,
I need to connect 4 RGB LEDs to an Arduino Uno. All 4 should lit the same way (like in series). Can I simply connect all the 4 LEDs to the same 3 pins + ground of the Arduino?

The pins are 9,10 and 11 + GND.

EachLED pins needs a 330 oh resistor. Will it work if I plug the same 330 ohm resistor into an Arduino pin and then connect 4 wires of the 4 LEDs to it? Or how can I do this?

Hi
What is your LED model?
You can inform the link with the datasheet of it.
To help we need to know the current and voltage of each color.

Your description is confusing, which is we always insist on a schematic.

In fact, the more I read your description the more confused I am, so POST A SCHEMATIC. It doesn't have to be fancy, just a pencil on paper will do.

Just remember that the Uno can only provide 20mA per GIPO pin. You can attach as many LEDs to a pin as you want as long as you never exceed 20mA.

Are these RGB LEDs? You can't connect RGB LEDs in series because there is a common terminal (4-pins for 3-internal LEDs).

In series you just need one resistor, but the maximum is usually 2 in LEDs series and it depends on the color because the voltage depends on the color. i.e. Red LEDs require about 2V, so that's 4V for the LEDs plus 1V across the current limiting resistor. And the resistor can have a lower value (Ohm's Law). If the LED requires 3V, that's 6V for two in series and since you only have 5V they won't work properly.

In parallel each LED should have it's own resistor. 330 Ohms puts you at about 10mA per LED (with 3V dropped across the resistor). 4 LEDs with 330 Ohm resistors puts you at about 40mA which is the "absolute maximum" for a single I/O pin.

Hello,
Thanx for the answer, unfortunately the seller page doesen’t have a data sheet except the input power is 2.1 - 3.4 VDC, common catode, and this picture:

Thank you. I could connect just 2 LEDs, both to Arduino pins 9,10 and 11, and the other two on pins 3, 5, 6, but I don't know how to make the 3, 5, 6 pins output the same code as 9, 10, 11 pins, because I want all 4 LEDs to change colors in the same time.

You could use this scheme for each color, resistor may need to be smaller for green and blue.
New-Project(1)

@ JCA34F - thanks for the drawing - will help on an upcoming project and save me looking for a part number

Your original subject, " 4 LEDs to 1 Arduino pin" suggests the use of "NeoPixel" or WS2811/ WS2812 LEDs, with which a large number - or just four - can be controlled individually or as a group, by one single Arduino pin.

The further advantage is that you require no more than one resistor, and the LEDs themselves execute the PWM with no microcontroller action other than setting the desired colour at any one time.

Thank you. I am not an experienced user to read schematics, but I think for LEDs you don’t need a separate 5v supply since the Arduino ports are the 5v supplier. And I don’t understand why the resistor on the ground wire is necessary, or maybe is not on the ground wire, and I don’t know how to read the schematic.

Can someone tell me how can I make my code to output the same RGB code also for pins 3, 5, 6 so I can connect more than 1 LED? Because now the code is made to output the code just using 11, 10, 9.
11 = Red
10 = Green
9 = Blue


int redPin = 11;
int greenPin = 10;
int bluePin = 9;

int r = 0;
int g = 0;
int b = 0;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() { 
  setColor(255, 0, 0);    // red
  setColor(0, 255, 0);    // green
  setColor(0, 0, 255);    // blue
  setColor(255, 255, 0);  // yellow
  setColor(80, 0, 80);    // purple
  setColor(0, 255, 255);  // aqua
}

void setColor(int red, int green, int blue) {
  while ( r != red || g != green || b != blue ) {
    if ( r < red ) r += 1;
    if ( r > red ) r -= 1;

    if ( g < green ) g += 1;
    if ( g > green ) g -= 1;

    if ( b < blue ) b += 1;
    if ( b > blue ) b -= 1;

    _setColor();
    delay(10);
  }
}

void _setColor() {
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b); 
}

Thank you!

You haven’t got a slightest idea how your code works, have you? Add 3 more analogWrite to different pins

Yes, I don’t know anything about Arduino :))
Because no help in life is free, you either pay with money or by giving your self-esteem to others. Thank God I got enough to be able to share with others. I can’t help with coding, but if anyone here needs some free energy I’m willing to help, just post some critics about me here and you will feel much better afterward. I’m willing to help with that in exchange for showing me how to make my code output to another 3 pins.

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

int redPin = 3;
int greenPin = 5;
int bluePin = 6;

int r = 0;
int g = 0;
int b = 0;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  pinMode(redPin2, OUTPUT);
  pinMode(greenPin2, OUTPUT);
  pinMode(bluePin2, OUTPUT);
}

void loop() { 
  setColor(255, 0, 0);    // red
  setColor(0, 255, 0);    // green
  setColor(0, 0, 255);    // blue
  setColor(255, 255, 0);  // yellow
  setColor(80, 0, 80);    // purple
  setColor(0, 255, 255);  // aqua
}

void setColor(int red, int green, int blue) {
  while ( r != red || g != green || b != blue ) {
    if ( r < red ) r += 1;
    if ( r > red ) r -= 1;

    if ( g < green ) g += 1;
    if ( g > green ) g -= 1;

    if ( b < blue ) b += 1;
    if ( b > blue ) b -= 1;

    _setColor();
    delay(10);
  }
}

void _setColor() {
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b); 

  analogWrite(redPin2, r);
  analogWrite(greenPin2, g);
  analogWrite(bluePin2, b); 
}

Would that work?

Sorry, I noticed the code is a total mess and I can’t help until the danger of clogging my device with sand is gone, at the beach just now.

a7

Don't worry, no need to waste your time on the phone, you should enjoy the beach. Thank you for the good intentions :slight_smile:

I tried this one, it uploads successfully without any errors, but it doesn't work:

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

int redPin2 = 6;
int greenPin2 = 5;
int bluePin2 = 3;

int r = 0;
int g = 0;
int b = 0;

int r2 = 0;
int g2 = 0;
int b2 = 0;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  pinMode(redPin2, OUTPUT);
  pinMode(greenPin2, OUTPUT);
  pinMode(bluePin2, OUTPUT);
}

void loop() {
  setColor(50, 0, 100);    // red
  setColor(40, 0, 70);    // green
  setColor(100, 0, 100);    // blue
  setColor(80, 0, 100);  // yellow
  setColor(0, 50, 100);    // purple
  setColor(100, 0, 50);  // aqua
}

void setColor(int red, int green, int blue) {
  while ( r != red || g != green || b != blue ) {
    if ( r < red ) r += 1;
    if ( r > red ) r -= 1;

    if ( g < green ) g += 1;
    if ( g > green ) g -= 1;

    if ( b < blue ) b += 1;
    if ( b > blue ) b -= 1;

    _setColor();
    delay(70);
  }
}

void setColor2(int red2, int green2, int blue2) {
  while ( r2 != red2 || g2 != green2 || b2 != blue2 ) {
    if ( r2 < red2 ) r2 += 1;
    if ( r2 > red2 ) r2 -= 1;

    if ( g2 < green2 ) g2 += 1;
    if ( g2 > green2 ) g2 -= 1;

    if ( b2 < blue2 ) b2 += 1;
    if ( b2 > blue2 ) b2 -= 1;

    _setColor();
    delay(70);
  }
}

void _setColor() {
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);

  analogWrite(redPin2, r2);
  analogWrite(greenPin2, g2);
  analogWrite(bluePin2, b2);
}

should work, after you fix this

the other code you posted has second setcolor function you never call

You can see it working in the Wokwi simulator:

https://wokwi.com/arduino/projects/323867730322129491

Just added some code for the second Led and at the end of a session set the Led to off (0,0,0).

Here is the code (as a good behavior we should always post the code in the forum as well!):

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

int redPin2 = 6;
int greenPin2 = 5;
int bluePin2 = 3;

int r = 0;
int g = 0;
int b = 0;

int r2 = 0;
int g2 = 0;
int b2 = 0;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  pinMode(redPin2, OUTPUT);
  pinMode(greenPin2, OUTPUT);
  pinMode(bluePin2, OUTPUT);
}

void loop() {
  setColor(50, 0, 100);    // red
  setColor(40, 0, 70);    // green
  setColor(100, 0, 100);    // blue
  setColor(80, 0, 100);  // yellow
  setColor(0, 50, 100);    // purple
  setColor(100, 0, 50);  // aqua
  setColor(0,0,0);
  setColor2(50, 0, 100);    // red
  setColor2(40, 0, 70);    // green
  setColor2(100, 0, 100);    // blue
  setColor2(80, 0, 100);  // yellow
  setColor2(0, 50, 100);    // purple
  setColor2(100, 0, 50);  // aqua
  setColor(0,0,0);
  }

void setColor(int red, int green, int blue) {
  while ( r != red || g != green || b != blue ) {
    if ( r < red ) r += 1;
    if ( r > red ) r -= 1;

    if ( g < green ) g += 1;
    if ( g > green ) g -= 1;

    if ( b < blue ) b += 1;
    if ( b > blue ) b -= 1;

    _setColor();
    delay(70);
  }
}

void setColor2(int red2, int green2, int blue2) {
  while ( r2 != red2 || g2 != green2 || b2 != blue2 ) {
    if ( r2 < red2 ) r2 += 1;
    if ( r2 > red2 ) r2 -= 1;

    if ( g2 < green2 ) g2 += 1;
    if ( g2 > green2 ) g2 -= 1;

    if ( b2 < blue2 ) b2 += 1;
    if ( b2 > blue2 ) b2 -= 1;

    _setColor();
    delay(100);
  }
}

void _setColor() {
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);

  analogWrite(redPin2, r2);
  analogWrite(greenPin2, g2);
  analogWrite(bluePin2, b2);
}

To be frank, I am not quite happy with the code, there is room for improvement ... :innocent:

If it does not run with your installation, please check whether common is really the cathode (goes to GND) or possibly the anode (goes to 5V) ...

Show me the code where there isn't. :expressionless:

@mongol you are at a point where it will make sense for you to step back and learn about a few slightly more than basic concepts and…

I am almost sure you will want to learn about a few semi-advanced concepts.

Any time you find a need to have red, red2, green, green2, and presumably one day red3 &c., it is a sign you want to learn about arrays.

Any time you have two code blocks that are identical except for some numbers appended to the variables it is a sign you want to learn more about functions, and how they are meant to cut down on the complexity of your code and its sheer number of lines.

And I will bet money that it was your hope that the two LEDs would be operating at the same time, not one then the other.

Amirite?

That's a sign you need to learn about blocking code, why it kills you is bad and how to write code that cooperates, shares resources and allows for the appearance of doing several things at once.

google and settle in. Read and just keep pretending like you understand, it isn't rocket science, but it will seem hard until it is easy. These search terms will have the classics near the top results:

 blink without delay

 arduino several things at once

 arduino finite state machines

 arduino traffic lights

There some serious work that only you can do, but trust me, it will pay off handsomely.
HTH

a7