# RGB Led fading with 0-5V

#### R!f@@

Joined Apr 2, 2009
9,749
I just started the next one I always wanted to do.
An RGB LED color changing using a VR.

PIC is 12F675

Code is already running. I have implemented soft PWM using the method John taught me during the computer table light.

So fay I can control the brightness for all or one color. Mixing is next. My approach is simple. But before that I like to figure out something.

Below is the part I like to know

C:
unsigned int Freq_temp;
unsigned char GPIOimg, Freq;

Freq = Freq_temp/10;
PWMduty = Freq;
I am reading the ADC and using it to update the Duty.
I get 64Hz flicker free brightness control.

Q is in here "Freq = Freq_temp/10"
You see the divider "10". If I go to ">100" brightness does not go to max and if low (<4) I get jumping in brightness around some ADC positions . I scoped the output and "10" seems a fine value as I can go from 0 PWM to MAX. How I got it doing trial and programing and checking......

I figures since "Freq" is like 0-255 and the ADC is more. I tried to scale it down thinking that an "int" is like 655535. So I tried divide by 257 to scale it down and brightness was way low.

I am thinking it should be 1023/255 = ~4 . Cause 4 is also working ( I just tried it ).
But wait 4 is also not as good as 10 at high brightness. "10" seems better ( gives smooth brightness change when pot is changed all the way .

So what value is "Freq" ?
Better yet if ADC is 5V max, what is ADC value, is it 1023 or something else ?

{ed}
"20" seems better at low brightness than "10"

Like to figure out the max value in which "Freq" is at max ADC

Last edited:

#### mcgyvr

Joined Oct 15, 2009
5,394
8 bit ADC = 2^8 = 256 discrete levels (0-255)
10 bit ADC = 2^10 = 1024 discrete levels (0-1023)
12 bit ADC = 2^12....
14 bit ADC = 2^14...
etc..

So if you have a 10 bit ADC your readings will be from 0-1023

#### R!f@@

Joined Apr 2, 2009
9,749
so to scale down to 255, I should use 4 or 5 ?

But I get far better brightness variation if I divide by 30

#### R!f@@

Joined Apr 2, 2009
9,749
So if use 31...

C:
Freq_temp = ADC_Read(0);
Freq = Freq_temp/31;
PWMduty = Freq;
Does the PWM Duty varies from 0 - 33 ?

#### mcgyvr

Joined Oct 15, 2009
5,394
So if use 31...

C:
Freq_temp = ADC_Read(0);
Freq = Freq_temp/31;
PWMduty = Freq;
Does the PWM Duty varies from 0 - 33 ?
Assuming a 10 bit ADC then yes..
1023/31 = 33

#### Picbuster

Joined Dec 2, 2013
1,027
I just started the next one I always wanted to do.
An RGB LED color changing using a VR.

PIC is 12F675

Code is already running. I have implemented soft PWM using the method John taught me during the computer table light.

So fay I can control the brightness for all or one color. Mixing is next. My approach is simple. But before that I like to figure out something.

Below is the part I like to know

C:
unsigned int Freq_temp;
unsigned char GPIOimg, Freq;

Freq = Freq_temp/10;
PWMduty = Freq;
I am reading the ADC and using it to update the Duty.
I get 64Hz flicker free brightness control.

Q is in here "Freq = Freq_temp/10"
You see the divider "10". If I go to ">100" brightness does not go to max and if low (<4) I get jumping in brightness around some ADC positions . I scoped the output and "10" seems a fine value as I can go from 0 PWM to MAX. How I got it doing trial and programing and checking......

I figures since "Freq" is like 0-255 and the ADC is more. I tried to scale it down thinking that an "int" is like 655535. So I tried divide by 257 to scale it down and brightness was way low.

I am thinking it should be 1023/255 = ~4 . Cause 4 is also working ( I just tried it ).
But wait 4 is also not as good as 10 at high brightness. "10" seems better ( gives smooth brightness change when pot is changed all the way .

So what value is "Freq" ?
Better yet if ADC is 5V max, what is ADC value, is it 1023 or something else ?

{ed}
"20" seems better at low brightness than "10"

Like to figure out the max value in which "Freq" is at max ADC
Do not try to divide an integer and expect a real as answer.

#### R!f@@

Joined Apr 2, 2009
9,749
So how should I divide the int to vary the duty ?
It is working so far....!

#### ErnieM

Joined Apr 24, 2011
8,167
So how should I divide the int to vary the duty ?
It is working so far....!
Your PIC has ab A2D capable of either 8 or 10 bits of resolution. You do not state the resolution of your soft PWM.

Unless you disclose your secret numbers no one can answer your query.

#### R!f@@

Joined Apr 2, 2009
9,749
umm .. That is kinda problem. I trying to figure out my PWM resolution. I not very good at soft PWM yet.
How do I figure out if I am using 8 or 10 bit.

#### djsfantasi

Joined Apr 11, 2010
7,756
What's the maximum value your ADC returns?

If it's 255, it is an 8 bit ADC.
If it's 1023, it is a 10 bit ADC.

#### R!f@@

Joined Apr 2, 2009
9,749
crap! 12F675 has no ICD. Tried to step thru the code to check that and MikroC said go fish.

I believe I am using 10bit... So how can I found that out with this little bugger ?