Introduction: LED As Lightsensor on the Arduino

I'm writing this instructable as an answer on some of Paolo's questions.

[I did some more experimenting on this subject in this: https://www.instructables.com/id/Better-LED-as-light-sensor/ Ible]

It is not hard to use an a LED as a sensor on the Arduino. Just put a LED with the anode (positive = long leg) in analog port 0 and the cathode (negative = short leg) in GND (ground).
You can also put a LED to react on the sensor with the anode in port 13 and the cathode in GND. (you can also watch the LED already on the Arduino board).
Different colors of LEDs will have better or worse effect, so experiment with that. 

Than upload the following sketch (program):

// this program is just made to experiment with a LED as a light-sensor.

int led = 13;
int basis = 0;
int sens = 0;
int x = 0;
int y = 0;
int totaal = 0;
int totaal1 = 0;

void setup() {
pinMode(led, OUTPUT);
}


void loop() {                                              // here we do 50 readings to set the sensors
   for(x =0; x < 50; x++) {
   sens = analogRead(0);
   totaal = totaal1 + sens; totaal1 = totaal;
   }
 sens = totaal/x;                          
// divide the 50 readings by 50 again 
totaal = 0;
totaal1 = 0;
basis = sens-20;          
// setting sensitivity - now it will react if the LED is 20 lower than the setting above
    for(y=0;y<1000;y++){            // after every 1000 tests the program will reset the led to cope with changing light
        for(x =0; x < 50; x++) {      // 50 readings to see if the sensor is in the dark
       sens = analogRead(0);
       totaal = totaal1 + sens;
       totaal1 = totaal;
       delay(10); }
    sens = totaal/x;
       if (sens < basis)                
// testing is the led was in the dark
       digitalWrite(led, HIGH);  // turning the led in port 13 or on the board on if the sensor-led was 20 darker than now than in the setting
         else  
          digitalWrite(led, LOW);
// turning it of if not
   totaal = 0;  
   totaal1 = 0;  
   delay(10); } }

Comments

author
SzymonB2 made it!(author)2016-02-07

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".

author
kenyer made it!(author)2016-02-07

You might also want to see Better-LED-as-light-sensor

author
kenyer made it!(author)2016-02-07

Hi SzymonB2,
That is also cool. Perhaps it does both, because in mij window it also reacted on shadows. For me this is a really old project by now, but it is nice to see that people are still improving on this.
Thanx for your comment.

author
lekirst made it!(author)2013-11-07

The led is still blinking when touched for a long time. The code I am currently using:

if (sensA < basisA) {
digitalWrite(A0, HIGH);
delay(1000);
digitalWrite(A0, LOW);
if (sensA > basisA)
digitalWrite(A0, HIGH);
delay(10);
}
else
totaalA = 0;
totaalAtus = 0;
} }

author
kenyer made it!(author)2013-11-07

Can you explain what you want it to do?

author
lekirst made it!(author)2013-11-05

this works great,
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.

author
kenyer made it!(author)2013-11-05

Yes that is exactly what I did with my 4x4x4 sensor-led-cube. https://www.instructables.com/id/4-x-4-x-4-interactive-led-cube/
I hope this helps you a little bit further.

author
lekirst made it!(author)2013-11-06

Hey Kenyar, the code for the 4x4x4 cube is quite complex, and I do not understand how the sensors are read. However, if I change the code line below (of this code above), the sensor led turns on when touched.
digitalWrite(A0, HIGH);
else
digitalWrite(A0, LOW);
However, it is now blinking when touched. Do you have a workaround for this?

author
kenyer made it!(author)2013-11-06

Yes there is:
Turn A0 HIGH when it is touched
Then give a delay (so you can remove your hand)
Now turn it LOW, check if it is touched again, if not turn it HIGH again. This turning off will be so fast that you will not be able to see it.
And here a delay again if it was touched (and then it would stay LOW)

Now it will turn on when you touch it and turn off when you touch it again. :)

author
hunain2000 made it!(author)2013-06-16

how would you add another LED in this. I mean to make it so that one LED turns on after 10 seconds of darkness and another turns on after 30 seconds of darkness

author
amasegny made it!(author)2013-05-18

Great Instructable

author
kyle+brinkerhoff made it!(author)2013-05-11

WHY DOES THIS WORK!?!?!?!?! EXPLAIN THE SCIENCE! BTW I Just tried it and Im completely baffled as to why it works but it does......

author
kenyer made it!(author)2013-05-11

Hi Kyle,

I have no idea why it works. But I also have no idea why a light sensitive resistor works ;)

author
theargha made it!(author)2012-11-09

Great Instructable. Thanx for sharing it.

author
Manticorp made it!(author)2012-10-08

Hey,

Just thought I'd let you know I managed to get this up and running in a great fashion - I tried the instructions for doing this on the Arduino playground:

http://www.arduino.cc/playground/Learning/LEDSensor

but their program didn't work very well. Your solution of averaging the values over a short time solved this.

I am using this to create a laser tripwire to trigger an SLR. When LED registers that the laser is broken, it sends a message using an infra-red LED which fires the camera.

author
kenyer made it!(author)2012-10-09

Nice to hear. And a great use of the idea. That is what CC is all about. Continue working on the ideas of the CC community.
What kind of pictures are you taking with it?

author
ch2s made it!(author)2011-10-17

THANKS KENEYR!

author
kenyer made it!(author)2011-10-18

i'm a bit busy now, put I hope to post some extra pictures for you, somewhere next week.

author
bertus52x11 made it!(author)2011-09-05

I didn't know this. Thanks!

author
kenyer made it!(author)2011-09-06

Hi Bertus,
I see that you have several camera stands solutions. Do you have an idea for my camera problem in my other instructable?

Hoi Bertus,
Ik zag dat je aardig wat camera-statief-oplossingen hebt. Heb je niet een goed idee voor mijn camera-ondersteboven-probleem in m'n andere instructable?

https://www.instructables.com/id/camera-clamp-for-hang-glider/

About This Instructable

25,387views

33favorites

License:

Bio: I'm a social-worker, working with 12 - 23 year-olds. I used to be a printer and I worked voluntarily in Romania for a couple of ... More »
More by kenyer:Ethereum Mining RigStop Motion Puppet ArmatureLife-size Hamster Ball Dodecahedron
Add instructable to: