1

I am trying to get an LED dimming reversely proportionate to natural light level in a room. So as the sun rises, the LED dims and vice-versa. I am using the standard LDR+ 10k Ohm resistor divider, an LED, and an Arduino Leonardo Clone.

The issue is, regardless of the values I adjust in map(), it does not change the flickering rate or brightness of the LED. What range of map shuold I be using?

The LED ought to shut off while the LDR takes a reading. I have two variations of the code; one uses PWM, the other simulates PWM using delay()

Here are my sketches:

/set pin numbers
//const won't change
const int ledPin = 1;   //D1 the number of the LED pin (OR to MOSFET input wire for controlling high power LED
const int ldrPin = A0;  // A0 the number of the LDR pin
//10K ohm resistor as voltage reference.


void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
  pinMode(ldrPin, INPUT);   //initialize the LDR pin as an input
}

void loop() {
  digitalWrite(ledPin, LOW); //Shutoff LED invisibly rapid to get a reading of the ambient light without the LED on
  int ldrStatus = analogRead(ldrPin);   //read the status of the light sensor value
  // KEEP LED OFF TIME BASED ON AMBIENT LIGHT LEVEL...
  int dim2 = map(ldrStatus, 0, 1023, 500, 0); //OFF FOR UP TO 0.50 seconds.
  digitalWrite(ledPin, HIGH);
  delay(dim2);

   }

Sketch two:

//set pin numbers
//const won't change
const int ledPin = 1;   //D1 the number of the LED pin (OR to MOSFET input wire for controlling high power LED
const int ldrPin = A0;  // A0 the number of the LDR pin
//10K OHM resistor as voltage reference.

void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
  pinMode(ldrPin, INPUT);   //initialize the LDR pin as an input
}

void loop() {
  digitalWrite(ledPin, LOW); //Shutoff LED invisibly rapid to get a reading of the ambient light without the LED on
  int ldrStatus = analogRead(ldrPin);   //read the status of the light sensor value
  int dim = map(ldrStatus, 0, 1023, 255, 0);     // scale it to use it with the LED (value between 0 and 255)
  digitalWrite(ledPin, dim);               //Flash LED at that rate to adjust brightness.

   }
3
  • 3
    Why do you think you should be using digitalWrite()? Mar 15 '17 at 17:04
  • 1
    I was mistaken. I switched it to analogWrite() , but no changes happened.
    – triops124
    Mar 15 '17 at 18:16
  • 2
    You need to use PWM pin, like pin 3, but different sets for different Arduinos.
    – KC Tucker
    Mar 15 '17 at 18:56
1

For certain Arduino models, the PWM pins are different. For the iteaduino lite, a usable PWM pin is pin D5.

Here is the code updated to use D5 instead of D1, which lacks PWM abilities:

//set pin numbers
//const won't change
const int ledPin = 5;   //D5 PWM the number of the LED pin (OR to MOSFET input wire for controlling high power LED
const int ldrPin = A0;  // A0 the number of the LDR pin
//10K OHM resistor as voltage reference.

void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
  pinMode(ldrPin, INPUT);   //initialize the LDR pin as an input
}

void loop() {
  digitalWrite(ledPin, LOW); //Shutoff LED invisibly rapid to get a reading of the ambient light without the LED on
  int ldrStatus = analogRead(ldrPin);   //read the status of the light sensor value
  int dim = map(ldrStatus, 0, 1023, 0, 255);     // scale it to use it with the LED (value between 0 and 255)
  analogWrite(ledPin, dim);               //Flash LED at that rate to adjust brightness.
  Serial.println(dim);
   }

There is still flickering, but that is to be expected. map() ranges can be adjusted to set when the LED will be fully off or on.

It is still unclear how to allow the light sensor to take a reading while the LED is OFF, then power back on rapidly enough to not have a visible flicker. If anyone has thoughts on that, it would be appreciated! Thanks everyone for helping.

1
  • analogRead() is slow. You should consider using the ADC at a lower level if you need improved speed. Mar 15 '17 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.