PWM for 8 LEDs with Arduino Nano

Hey reader!

I'm new to Arduino, so this may be really easy...or impossible.

I need to run 8 leds, independently, all with PWM, from a Nano. I know there are only 6 pins that can be used for PWM. However, someone told me in passing that I could tie all of the 8 leds together with a common lead from one of the PWM pins, and simply connect the other contact on each LED to a pin of their own. Then, when I turn one LED on with it's individual pin it will be receiving a PWM signal from the common lead and will allow the led to pulse as desired.

Is this correct? If not, is there another way to do this? Any chance you (I mean, you've read this far, right!?!), could provide more direct info as to how to do this? Both wiring and sketch?

Would be much appreciated! I'm trying to get a project done for a pitch competition this week!

Hi,

How many of the 8 leds need to be lit at a time?

The suggestion you were given sounds like only one of the eight leds would be lit at any time. However, a multiplexing strategy could make it appear that all 8 were lit, although at the cost of reduced brightness.

What is a pitch competition? A ball game?

Paul

Hey Paul!

Thanks for your time and response.

Yes, I forgot to mention that important piece of info...no more than one LED will be on at any given time.

A pitch competition is for pitching a business concept or upstart to a crowd with the hopes to win some money, and to connect with investors looking to fund startups.

Here is a link to the competition I am hoping to pitch in this Thursday and Friday!

So, it seems like this might be a workable strategy, yeah? I already have a common lead connecting all of my leds. Is there a particular pin this needs to connect to? (Also, these seem to run just fine on 3.3v output). Any recommendation as to how I would handle the sketch portion of this?

Thanks again for any recommendations, Paul, and of course anyone else interested in assisting!

The big problem, referred to as the "XY problem", is asking a question that describes only a very small part of the actual situation, presuming a particular way to address the actual problem that may in fact, be poor or even impossible.

So, step back and explain - what else does the Arduino need to do, what inputs will control it and - all the rest. :grinning:

If all you need to do is to dim an individual LED on its own, that can be performed in software and can be done for as many pins as you can connect. Even if it needs to do some other relatively simple task, this may still be straightforward.

So - give us the information!

They may appear to run ok on 3.3V but you could be shortening their life, and the life of the 3.3V regulator built into one of the Nano's chips. You should always use series resistors with leds. Normally you need one series resistor per led, but because only one of your leds will be on at a time, and they are identical, you can get away with only one series resistor. Its value should be around 150 to 300 ohms. For maximum brightness you can calculate an exact value if you know the maximum current and forward voltage of your leds.

to connect them up, connect your common wire to one of the Nano's PWM outputs using your series resistor. The other 8 led connections each go to any other 8 digital outputs (but don't use 0 or 1). This diagram shows which are pwm and digital outputs.

This is great info, thanks to both Pauls!

I do currently have a series resistor on each LED (a fact I forgot to mention).

Regarding the XY Problem, thanks for the explanation! This one is so simple, there isn't much to add, but I can flesh it out a little bit.

What I want to happen is this:

I turn my Arduino unit on and the sketch starts to run. One of the LEDs (the top one on my PCB) will slowly pulse from dim to bright and back down. It will run through this process 4 or 5 times (TBD) and then the next LED in line will run through the same pattern. This will continue all the way down (through all 8 LEDs) then start over back at the top, until the unit is turned off. So, no additional inputs, just LEDs pulsing in a pattern, over and over again.

Thanks again to you both. I will take a crack at it based on what PaulRB posted and update this post with the result!

whitetyrone:
One of the LEDs (the top one on my PCB) will slowly pulse from dim to bright and back down. It will run through this process 4 or 5 times (TBD) and then the next LED in line will run through the same pattern. This will continue all the way down (through all 8 LEDs) then start over back at the top, until the unit is turned off. So, no additional inputs, just LEDs pulsing in a pattern, over and over again.

That could be easily performed in software with one LED per output pin.

I was thinking something like this:

const int cathode[8] = {4,5,6,7,8,9,10,11};
const int anode = 3;

void setup() {
  pinMode(anode, OUTPUT);
  for (int i=0; i<8; i++) {
    pinMode(cathode[i], OUTPUT);
    digitalWrite(cathode[i], HIGH);
  }
}

void loop() {
  for (int i=0; i<8; i++) {
    digitalWrite(cathode[i], LOW);
    for (int k=0; k<5; k++) {
      for (int j=0; j<256; j++) {
        analogWrite(anode, j);
        delay(10);
      }
      for (int j=255; j>=0; j--) {
        analogWrite(anode, j);
        delay(10);
      }
    }
    digitalWrite(cathode[i], HIGH);
  }
}

Guys! I had a great, really long and in depth response written a few minutes ago...but when I submitted it I was not connected to the internet...so I lost it all. Bogus.

Anyway, I've found a computer that will allow me to actually communicate with my Nano, so I can finally test some sketches.

Looking at PaulRB's example sketch, I realized I need a different approach.

It seems as though that sketch supposes that the common lead connecting all of the LEDs is the Anode...but alas it is not. Each LED is connected to that line on its Cathode side, and each therefor has its own independent Anode.

I think there is still a workaround, but maybe you can weigh in as to how the sketch would be written?

The common ground is connected to Digital Pin 11.

The Anode leads are connected to Digital Pins 3 through 10.

So, PWM run on Pin 11 will need to start at Brightness 254, then run down to 0 and back up to 254, over and over and over.

All digital pins will initially be set to LOW. (I hope that having a voltage potential in the opposite direction that is intended for the LEDs won't cause any damage?)

I then chose one LED at a time to set to HIGH, which will cause it to FADE from 0 to 254 to 0 over and over until I set it back to LOW and switch to another LED.

Am I getting this right?

I've been able to make this work for one LED by simply modifying the FADE example sketch. Where I run into trouble, is when I try to write in the turning on and off of each LED Anode while still allowing the PIN 11 PWM loop to run throughout.

Any assistance would be much appreciated!

It worked! I'm up and running!

Thanks again, Paul and Paul.