Fine control of an LED

Hi Guys,

Got an Arduino Nano yesterday, so first question to the Forum.

Can I increase/decrease the value of an LED using a float ie -

float fadeValue = 255;

analogWrite(ledPin, fadeValue);
fadeValue = fadeValue - .2

I used Serial.print to see what values were being passed to the ledPin, and everything seems to be OK and the value was decreasing by .2, but the fade doesnt seem to be any smoother (just takes longer).

Unsure of the internal working of an Arduino, but, Is it only implementing the value when it gets to an integer.

The only reason I wanted to do this, was because, as the LED got dimmer, the dimming steps became noticeable.

BTW im using fade without Delay() to dim the LED, got other things going on that cant be disrupted.

nexus6:
Unsure of the internal working of an Arduino, but, Is it only implementing the value when it gets to an integer.

Correct. 0 - 255 inclusive.

BTW, if you quickly switch back and forth between
analogWrite(ledPin, 100);
and
analogWrite(ledPin, 101);

you'll essentially achieve analogWrite(ledPin, 100.5);

Thanks gfvalvo, thats quite interesting, i will try that out, I will have to do a minimum of 3 step to ensure it is still decreasing ie

101 to 100 to 101 to 100
Step1 Step2 Step3

I will have to reduce the timer accordingly (66%) so that it still takes the same amount to dim.

Thanks again.

Also remember that the human eye has a logarithmic response to light intensity. Large changes at a high brightness are less noticeable than small changes at a low brightness.

analogWrite only takes integers (whole numbers) in the range 0..255. Floating points will be rounded / truncated to nearest integer.

When working with LED's (or light in general) you should know that light intensity (or brightness) does not increase in a linear curve all the way from 0 to 255. The brighness will increase more in the lower half (0..122) than in the upper half (123..255). So in order to get a linear increase in brighness, you will need to use a logarithmic formula - further info.

EDIT: Assuming the human eye is the instrument used for measurement! :wink:

Danois90:
When working with LED's (or light in general) you should know that light intensity (or brightness) does not increase in a linear curve all the way from 0 to 255.

The optical intensity from the LED should be quite linear with PWM duty cycle. It's human eye's interpretation of that intensity as "brightness" that's non-linear.

gfvalvo:
It's human eye's interpretation of that intensity as "brightness" that's non-linear.

Yes, and what else would OP measure the brightness with other than the eye? :stuck_out_tongue:

gfvalvo/Danois90,

I sought of guessed that the LED brightness didn't follow a linear path, or the eye perceived it wasn't following one and seemed to speed up as it got dimmer. I had a look at the link that you sent, haven't implemented it yet because I want to read through it fully and understand it before I use it, but done a quick hack of my code that increases the time delay by one increment each loop cycle after the LED value fell below 40, this gives a linear drop off of brightness from 255 -> 40 and then curves to produce a smoother fade at the end, not ideal yet because the last 5 - 6 cycles are still a bit stepped, but a huge improvement.

Sorry for reviving this post, I know its been dead for an hour now :slight_smile:

Just read in the Arduino Reference for analogWrite() that the PWM Frequency for the pins is - 490 Hz (pins 5 and 6: 980 Hz).

Is this something that will effect the slight stepping of the dimming affect as it nears the end of the cycle?

nexus6:
Is this something that will effect the slight stepping of the dimming affect as it nears the end of the cycle?

No, the PWM frequency only affects how fast the LED "blinks" / flickers. It does not affect how the brightness is perceived by the eye.

Thanks Danois90

Someone should invite @GolamMostafa into this thread, so he can explain the physics to everyone.

lastchancename:
Someone should invite @GolamMostafa into this thread, so he can explain the physics to everyone.

With confusing charts and graphs!

lastchancename:
Someone should invite @GolamMostafa into this thread, so he can explain the physics to everyone.

The human physiology is involved too and could be compared do that of a squid or octopus.

evanmars:
With confusing charts and graphs!

And not to forget the enumerations. :wink:

extern const uint8_t gamma[];
int led = 3;           // the PWM pin the LED is attached to
int brightness;    // how bright the LED is
int corrected;
int dwell;

void setup() 
{
  pinMode(led, OUTPUT);
  dwell = 500;
}

void loop() 
{
  for (brightness = 20; brightness < 240; brightness ++)
  {
    corrected = pgm_read_byte(&gamma[brightness]);
    analogWrite(led, corrected);
    delay(dwell);
  }
  
  for (brightness = 240; brightness > 20; brightness --)
  {
    corrected = pgm_read_byte(&gamma[brightness]);
    analogWrite(led, corrected);
    delay(dwell);
  }
}

const uint8_t PROGMEM gamma[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255};