# PWM for 8 LED's with only 6 pins available?

It seems to work! Can you see a problem with the code?
You do not need series RES except for maybe the fancy blinking in setup.
Just connect 8 LED’s and 8 switches to the pins below.
The other side of the switches to GND.

``````#define UPER 50   //step period in ms 200 max for non-PWM
#define BMAX 30  //255 or 50 no need for series RES this way
#define L1a 2   //a means not PWM
#define L2  3
#define L3a 4
#define L4  5
#define L5  6
#define L6  9
#define L7  10
#define L8  11
#define SW1 7   //D7
#define SW2 8
#define SW3 12
#define SW4 13  //A0?
#define SW5 A1  //A means Analog pins
#define SW6 A2
#define SW7 A3
#define SW8 A4

#define AW(x) analogWrite(cbout[x],a[x])
#define AW2(x,v) analogWrite(cbout[x],v)
#define DW0(x) digitalWrite(cbout[x],LOW)
#define DW1(x) digitalWrite(cbout[x],HIGH)
#define V1 (int(a)*UPER/26)  //0- nearly 1000  26=255/10  when UPER=100
#define V3 (int(a)*UPER/26)

const byte cbout={0,L1a,L2,L3a,L4,L5,L6,L7,L8};
const byte cbin={0,SW1,SW2,SW3,SW4,SW5,SW6,SW7,SW8};
byte a,ud;  //0 none 1 up 2 dn
int i,j,k=0;
//long m,wasm;

void setup(){
//Serial.begin(9600);
for(i=1;i<9;i++){
a[i]=ud[i]=0;
pinMode(cbout[i],OUTPUT);
digitalWrite(cbin[i],HIGH);  //20k pullup
}
//fancy blinking after start choose 1?
for(i=1;i<9;i++){DW1(i);delay(50);}
for(i=8;i>0;i--){DW0(i);delay(50);} delay(100);
for(i=1;i<9;i++){DW1(i);delay(50);}
for(i=1;i<9;i++){DW0(i);delay(50);} delay(100);
for(i=10;i<50;i++){DW1((i%8)+1);delay(2+i/2);DW0((i%8)+1);}
for(i=0;i<100;i++){DW1(j=random(8)+1);delay(random(50)+10);DW0(j);}
}
void loop(){
//while((m=millis())==wasm) ;  //wait until next ms
mydelay(UPER);
for(i=1;i<9;i++){
if PUSH(i) dop(i);
doi(i);
}}
void mydelay(int d) {  //ms
int vv1=V1,vv3=V3;
//Serial.print(V1);Serial.write(32);
for(i=0;i<10;i++) {
if(vv1)DW1(1);
if(vv3)DW1(3);
for(j=0;j<d*9;j++) {  //10->9
if(j==vv1)DW0(1);
if(j==vv3)DW0(3);
delayMicroseconds(10);
}}
DW0(1);DW0(3); //just in case
}
void dop(byte p) {
if((ud[p]==0)&&(a[p]==0   ))ud[p]=1;
if((ud[p]==0)&&(a[p]==BMAX))ud[p]=2;
}
void doi(byte p) {  //p is pin
int ainc=1;
if(a[p]>BMAX/2)ainc++;  //BMAX should be even not 255
if((ud[p]==1)&&(a[p]<BMAX))a[p]+=ainc;  //2nd part not needed cuz below
if((ud[p]==2)&&(a[p]>0   ))a[p]-=ainc;
if(a[p]==BMAX)ud[p]=0;
if(a[p]==0   )ud[p]=0;
if((p==1)||(p==3))return;  //not PWM
AW(p);
}
/*
The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles.
This is because of interactions with the millis() and delay() functions,
which share the same internal timer used to generate those PWM outputs.
This will be noticed mostly on low duty-cycle settings (0 - 10)
and may result in a value of 0 not fully turning off the output on pins 5 and 6.
*/
``````

So you have 40mA+ into LEDs fo 200mS with no resistor?

Here's a typical Red LED

Supports a surge current of up to 0.1A.
But only for up to 10uS

What LEDs are you using that are tolerant of the higher current for so long?

Yes, but let’s forget about the fancy setup code for a minute.
With PWM it is far less than 10ms. Even the 2 pins without PWM are <10ms.
Oh wait you said 10 MICRO secs. Really? Are you sure?
It just works. I let it run for 3 hours.

There’s nothing special about my LED’s, but the Aroboto/Uno 5v reg seems indestructible driving 8 servos at once.
OK, why does my setup code not create the magic smoke after 800ms, nearly a second at 5v with no RES?
I agree that it should not work.
Which will go first, LED or Uno?
That question is for you.
I don’t want to find out by experimenting anymore, if it’s the latter.

Do you like how I made 2 fake PWM outputs that appear to work the same as the other 6?

If you’re careful and use <2ms pulses, you can even drive a 10mw laser directly without a series RES.
I ran this all day.

Which will go first, LED or Uno?

The Uno.

When a LED is slightly over driven the light output reduces at a much faster rate than normal so its half life is shortened. That is the time it takes for the light output to drop by half. So no instant smoke.

Have you seen what you can get out of a Uno pin:-
http://www.thebox.myzen.co.uk/Tutorial/LEDs.html
Taking more current than the rated current from a pin is just like taking crack, it won't kill you at first but it sure messes up your insides. Do it for long enough and it will kill you.

I will not drive an LED directly again for more than 10ms. How about 10ms? 10us?
I don't think as much current flows as you would expect. It takes time a finite time to get to 40ma?
This code is being used in a consumer application. But the voltage is only 2.7v after the regulator.
I will add 8 resistors!

I don't think as much current flows as you would expect]I don't think as much current flows as you would expect

Well that is what I actually measured. If it is any consolation I was surprised you could get 250mA from a pin, that is 8 times the level when damage starts to occur according to the data sheet.

It takes time a finite time to get to 40ma? It takes time a finite time to get to 40ma?

No, why should it.
The only thing that would slow the current rise down is a series inductance.

How about 10ms? 10us?

Over current is over current no matter how short it is applied for. Look at the number of people advising a series resistor for the gate of a FET because that looks like a capacitor of a few tens of pF and that would over current the pin because a discharged capacitor looks like a short circuit.

Can we look at the shape of the current (voltage) curve with an Oscilloscope?
10ms?
10us?
How would you design this experiment?

Write a simple sketch that sourced current into a load.
Vary the load, vary the on time.

PORTC = B00000001;
delayMicroseconds(10);
PORTC = B00000000;

With digital scope, capture it one time.
Or put it in a for loop and watch it repeat, see if it changes over time.

A load? I would use my LED. Scopes measure voltage. There's gotta be a small value RES in there somewhere 10 ohm? How fast does analogRead go by default 100us? I could use that instead of a scope. With the Fastsample sketch I got 80khz using prescaling.

The scope is not going to draw any current tho - you want to see the effect of resistance/capacitance/inductance/LED on the waveform.

With the Fastsample sketch I got 80khz using prescaling.

So that is a sample speed of 12.5uS and you want to see the rise time of a 10uS pulse! That is not going to happen.

In my tests I used the PWM output on pin 9 with a value of 1. I was using an expensive scope > \$8000 with a good probe, and I didn't notice any significant rise time on the pulse. You have to watch cheap scopes and probes they can add significantly to the apparent rise time of a pulse.

Of course not. I could measure a 1ms pulse with 12.5us rate.

So you measured the Voltage across the LED without a series resistor?
That is not the same as current with these 2 non-ideal components.

So you measured the Voltage across the LED without a series resistor?

No that would tell you nothing, I measured the voltage across a number of diffrent resistors. Only that way can you measure the current.

That's what I thought. Cool experiment you did!