3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

LED as lightsensor on the arduino

video LED as lightsensor on the arduino
I'm writing this instructable as an answer on some of Paolo's questions.

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); } }
4 comments
Oct 17, 2011. 2:32 PMch2s says:
THANKS KENEYR!
Sep 5, 2011. 10:59 PMbertus52x11 says:
I didn't know this. Thanks!

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
23
Followers
8
Author:kenyer(Technisch Speelgoed)
I'm a social-worker, working with 16 - 23 year-olds. I used to be a printer and I worked voluntarily in Romania for a couple of years. One day a week I have a toy-store in technical toys. I have this ...
more »