Arduino: Use LED As a Light Sensor
Intro: Arduino: Use LED As a Light Sensor
A while ago, I read a blog post about using a normal LED as a light sensor. After browsing around the Internet, I found that all the examples that I found were not working well or hard to read. Then I stumbled upon this instructable: https://www.instructables.com/id/LED-as-lightsensor-on-the-arduino/
However, I found that the code was kind of hard to read/edit and it missed a serial output. Below is the cleaned up code with serial output. In addition, I found that adding a 100R resistor in series with the sensing LED greatly improves the sensitivity. This change improves the range of light on the serial port from 290-270 to 290-120.
#define READ A0
#define LED 13
int basis = 0;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(57600);
}
void loop() {
int sens = readLED(50);
basis = sens - 20; // setting sensitivity - now it will react if the LED is 20 lower than the setting above
for(int y = 0; y < 1000; y++) { // after every 1000 tests the program will reset the led to cope with changing light
sens = readLED(50);
Serial.println(sens);
if (sens < basis) // testing is the led was in the dark
digitalWrite(LED, HIGH);
else
digitalWrite(LED, LOW);
}
}
int readLED(int number) { // Read analog value n times and avarage over those n times
int totaal = 0;
for(int x = 0; x < number; x++) {
totaal += analogRead(READ);
delay(10);
}
return totaal/number;
}
However, I found that the code was kind of hard to read/edit and it missed a serial output. Below is the cleaned up code with serial output. In addition, I found that adding a 100R resistor in series with the sensing LED greatly improves the sensitivity. This change improves the range of light on the serial port from 290-270 to 290-120.
#define READ A0
#define LED 13
int basis = 0;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(57600);
}
void loop() {
int sens = readLED(50);
basis = sens - 20; // setting sensitivity - now it will react if the LED is 20 lower than the setting above
for(int y = 0; y < 1000; y++) { // after every 1000 tests the program will reset the led to cope with changing light
sens = readLED(50);
Serial.println(sens);
if (sens < basis) // testing is the led was in the dark
digitalWrite(LED, HIGH);
else
digitalWrite(LED, LOW);
}
}
int readLED(int number) { // Read analog value n times and avarage over those n times
int totaal = 0;
for(int x = 0; x < number; x++) {
totaal += analogRead(READ);
delay(10);
}
return totaal/number;
}
6 Comments
Vibhor 0001 5 years ago
Sir,
Can you give me step by step explanation of the code
Please sir.
thank you
SzymonB2 8 years ago
This does not use the LED as a light sensor but rather an antenna. It picks up 50/60Hz noise from the grid. If you put your hand close to the LED without changing the incident light you will see the thing still "working". If you remove the LED and replace it with a small wire it will continue "working".
mohamed.zidan.520125 9 years ago
sir this is great job but I notice that the led work as motion sensor not for dark sensor
mohamed.zidan.520125 9 years ago
sorry sir i forget to put the resistor great work
kenyer 10 years ago
I didn't have the same result with the resistor tough.
lekirst 10 years ago
is there a way to alter the code so the output led is the same as the sensor led?
I tried this (http://playground.arduino.cc/Learning/LEDSensor) code, but doesn't seem to be very stable.