0

I wrote a sample code to measure the brightness of LED by controlling the duty cycle of LED connected to Arduino. I want to get the range of least bright light to max bright light for a specific period of period. When i put desired_brightness = 1, the LED is emitting light at 93 lux units, its not the least brightlight. Any suggestion on how to get the least bright light?

int led = 3;           // the pin that the LED is attached to
int brightness =0;    // how bright the LED is
int incrementfactor = 10;    // how many points to fade the LED by
int desired_brightness = 255   ;
int extra_delay = 1000;

void setup() {      // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  analogWrite(led, desired_brightness);
}
void loop() {
  analogWrite(led, desired_brightness);
  brightness=brightness+incrementfactor;
  if (brightness==desired_brightness) {
     delay(extra_delay);
  }
}
George luke
  • 27
  • 1
  • 8

1 Answers1

0

I've tailored your code a bit. The main problem was you were going to maximum brightness right away, and were never decreasing it. analogWrite() only takes values from 0 to 255. You were starting at 255 and increasing from there, so it just stayed bright. Try this instead, it's the "breathing effect" you see on so many electronics these days, and loops forever:

int ledPin = 3;       // the pin that the LED is attached to
int brightness =0;    // how bright the LED is
int extra_delay = 1000; // the extra delay at max
int super_delay = 5000; // super delay at min
int direction = 1;    // the dimmer-brighter direction

void setup() {     
  pinMode(ledPin, OUTPUT);
  analogWrite(ledPin, 0);
}

void loop() {
  analogWrite(ledPin, brightness); // light at certain brightness
  //delay(5); // wait a bit, stay at this level so we can see the effect!
  delay(50); // longer delay means much slower change from bright to dim

  if (direction == 1)  // determine whether to go brighter or dimmer
    brightness += 1;
  else
    brightness -= 1;

  if (brightness == 255) // switch direction for next time
  {
    direction = -1;
    delay(extra_delay); // extra delay at maximum brightness
  }
  if (brightness == 0) // switch direction, go brighter next time
  {
    direction = 1;
    delay(super_delay); // super long delay at minimum brightness
  }
}

This will go brighter, then dim, and repeat. The delay is very important -- you can reduce it or lengthen it, but without it, the change happens so fast you can't see it by eye, only on an oscilloscope. Hope this helps you!

EDIT:

Added a "super delay" at minimum brightness for measurement of that level.

One thing to keep in mind is that pulse-width modulation like this still gives full driving voltage from the output pin. PWM just alters the ratio of the time the pin is high vs. low. On this Aduino, it's still a voltage swinging instantly and quite rapidly between 0V and 3.3V. To get a true analog voltage from this output, one needs some circuitry to filter the highs and lows of PWM into a smoother average DC voltage. If you want to pursue that, search for "RC low-pass filter" or visit a site like this one.

TomServo
  • 7,248
  • 5
  • 30
  • 47
  • .Thank you for the modification. But i don't understand how do i make the lowest intensity to light to stay longer. In the code i wrote, if i put 'int desired_brightness = 1', i get the lowest light intensity. But at that intensity i am getting a lux value of 93. I tried with your code too. The lowest intensity of light i can reach is still with lux value 93 – George luke Jun 28 '17 at 09:56
  • The pin driving the LED is either on or off. The question is how much time it spend on vs. off, with human persistence of vision, lower ratios of on vs. off end up looking dimmer. The only way to get a true lower voltage is to take the PWM signal and filter it, like through and RC or LC network, to smooth out the up-and-down PWM signal into an actual varying DC voltage. You need more electronics components to do that. – TomServo Jun 28 '17 at 12:38
  • @Georgeluke I added a "super_delay" to the code above to stay at minimum for 5 seconds. However, this minimum is basically "off". Please read the remainder of my edit, and if you feel I've earned it, upvote and accept my answer so far. – TomServo Jun 28 '17 at 12:49
  • the voltage varies from 0-5 V ryt ? why 3.3 V? – George luke Jun 28 '17 at 15:45
  • Let me double check that when I can. – TomServo Jun 28 '17 at 18:25