Introduction: Better LED As Light Sensor

About: I'm a social-worker, working with 12 - 23 year-olds. I used to be a printer. In 2018 I opened a small makerspace (www.imdib.nl) in my house, where I have lasercutters, 3d-printers, Arduino's, Mindstorms and ot…
As a result of the questions from lekirst  on this ible of my hand: LED-as-lightsensor-on-the-arduino/ and the cleaner code made by hansc44 at:  Arduino-Use-LED-as-a-light-sensor/ it was time to come with a better Ible. So here it is. (I didn't solve lekirst's problems yet so she probably is open for suggestions)

What is the idea?

You might know that a LED emits light when you put the right amount of volts on the right leads. The funny thing is that it also works the other way around. Not the leads, it doesn't work when you put the leads the other way around. But what does work is that when you shine light on a LED it will GIVE current. It is a very small current but just enough for an Arduino to detect.

What will it do?

When you darken the sensor led, for instance by covering it with your hand, it will turn on the other LED.

Does this work with any LED?

As far as I know, it works with any LED, but not with any LED as good.
green and red LEDs seem to work best.
If trough experimenting you find out that other colors even work better, let me know.

Does it work with any light?

This sketch works very reliable with normal daylight. It is not fully tested in dark rooms or bright sunlight yet.

Step 1: You Will Need

You will need
  • an Arduino board (Uno, Duemilanove or something like that)
  • two LEDs
And really that's it! (you could even do it with just one LED and use the SMD-LED on the Arduino board as the second LED)

You will need no tools.

You do need a computer with the Arduino software installed and a USB-cable to connect the Arduino board. (But if you have an Arduino board, you probably have that sorted already)

Step 2: Setting Everything Up

To set this project up, you just need to put the two LED's in the Arduino.
  • Put the LED that you want to use as the sensor with the long lead (this is the anode and positive lead) in pin analog 3 in the Arduino. This could also be an other analog pin, but then you need to change this in the sketch later on.
    Put the other, short lead in the GND.
  • Put the other LED with the long lead in pin (digital) 13 in the Arduino. Put the short lead in the GND that is next to pin 13.

Now you just need to hook up the Arduino board with the computer and the fun can begin.

Step 3: Uploading the Sketch

//Copy the following sketch in your Arduino software and upload it to the Arduino board. (be sure to select the right board and com-port in the Arduino software)

int led = 13;                                        // the pin where you will put the LED
int sensorpin = A3;                           // the analog pin where you put your sensorLED
int resetteller = 0;                              // the rest are counters and variables to calculate with
int sens = 0;
int teller = 0;
int basis = 1024;
int test = 1024;
int test2 = 1024;
int test3 = 1024;

// this are the values to play with to get better (or worse) results
int marge = 5;                         // the space between a positive and negative reading
int vertraging = 1;                  // the speed of the readings; a lower number is a higher speed
int samples = 70;                   // the amount of samples to compare to make one reading
int resetsamples = 30;          // how many cycles to run the light on before you don't trust the value anymore

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);                                   // no real part of the program, just for debugging
  for(teller =0; teller < samples; teller++) {// remember the lowest value out of many readings
   sens = analogRead(sensorpin);
   if (sens < basis){basis = sens;}
   delay(vertraging);                                     // the sensor needs a delay here to catch its breath
  }
}


void loop() {                                            
for(teller =0; teller < samples; teller++) {// remember the lowest value out of many readings
   sens = analogRead(sensorpin);
   delay(vertraging);                                   // the sensor needs a delay here to catch its breath
   if (sens < test){
   test3 = sens;                                            // remember the 3 lowest readings
   test2 = test3;
   test = test2;}
  }
  if (test < basis-marge && test2 < basis-marge && test3 < basis-marge){//all 3 low readings mus be < the basis reading
    digitalWrite(led, HIGH);
    resetteller++;                                         // count how long the LED stays on
  }
    else{
      digitalWrite(led, LOW);
      basis = test;                                         // if the lowest test reading is higher than the basis, basis will be reset
      resetteller = 0;
    }
    if (resetteller > resetsamples){basis = test;}//if LED stays on to long, we don't trust it and reset basis
    Serial.print(basis);Serial.print("  ");Serial.print(test);Serial.print("  ");Serial.println(sens);//just for debugging
    test = 1024;
}

Step 4: About the Sketch

What does it do?

When you darken the sensor led, for instance by covering it with your hand, it will turn on the other LED.

Why is this one better?

This sketch works much more reliable than the previous version.
This sketch has the serial monitor functionality.
All the tweaking is done in the top of the sketch with four variables.
I did put a lot more explanations in the sketch

Is this sketch perfect?

No, it is far from perfect, but is works great most of the time. Sometimes it doesn't work at all for no apparent reason. But when it works, it will keep on working for hours.
Besides that does it still not do what lekirst wants it to do: The LED sensor is also the emitting sensor that stays on as long as it is touched.
I'm sorry that I use Dutch names for the variables some of the time, but that is because Dutch isn't Dutch to me.

How to tweak

You can change the "marge". This will set how sensitive the sketch will be for light changes. A low value is a high sensitivity.

When you change the "vertraging" you can set the speed of the sensor. A low value is a high speed. My Arduino doesn't work when I set it al the way to 0, but with 1 it works most of the time. Somehow the Arduino needs a short rest in-between sensing.

With the "samples" you change the accuracy of the sketch. More samples is a higher accuracy, but with a higher value it will also react slower.

The last value you can tweak is the "resetsamples". This value sets how long the sketch will accept a low reading on the sensor (and turn on the LED) until it doesn't trust it anymore and reset the sensor back to zero.

What else?

Well if you want to put your emitting LED in an other pin, you should change the value of "led" to the number of the pin you want to use.
The same goes for the value of "sensorpin" if you want to use an other analog pin for your sensor LED.



You might notice that the LED turns off after a short time when I keep my hand over the sensor. This is because of the "resetsamples" value. When you increase this value, the LED will stay on longer. But when the sensor makes a mistake because of changing light or something, it also takes longer for the sketch to correct this.