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.
}
digitalWrite()
?analogWrite()
, but no changes happened.