Pages: [1] 2   Go Down
Author Topic: Using LEDs as a light/dark sensor  (Read 2604 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 4
Posts: 218
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I found this site I searched this forum for some info and found none . So I thought to post it here as it works well and gets rid of the Cds LDR
http://provideyourown.com/2011/cheap-alternative-for-hard-to-find-cds-light-sensor/


Here is an example of its use,Reversed LED to Sense on D2 no resistor ,LEDs on 5&6 normal resistors

Code:
class AmbientLightSensor {
public:
  AmbientLightSensor(int ledPin) : mLedPin(ledPin), mMeasureAnalog(false) {}
 
  void setAnalogMeasurement(int thresholdLevel); // measure from an analog pin
  void setDigitalMeasurement(); // measure from a digital pin (default)
 
  int measure();
 
protected:
  int mLedPin;
  bool mMeasureAnalog;
  int mAnalogThresholdLevel; // (0 to 1023)
 
  void charge();
  void discharge();
 
  int measureUsingAnalogPin();
  int measureUsingDigitalPin();
};
 
void AmbientLightSensor::setAnalogMeasurement(int thresholdLevel)
{
  mAnalogThresholdLevel = thresholdLevel;
  mMeasureAnalog = true;
}
 
void AmbientLightSensor::setDigitalMeasurement()
{
  mMeasureAnalog = false;
}
 
void AmbientLightSensor::charge() {
  // Apply reverse voltage, charge up the pin and led capacitance
  pinMode(mLedPin, OUTPUT);
  digitalWrite(mLedPin, HIGH);
}
 
void AmbientLightSensor::discharge() {
  // Isolate the diode
  pinMode(mLedPin, INPUT);
  digitalWrite(mLedPin, LOW); // turn off internal pull-up resistor, see http://arduino.cc/en/Tutorial/DigitalPins
}
 
int AmbientLightSensor::measure() {
  charge();
  delay(1); // charge it up
  discharge();
  return (mMeasureAnalog)? measureUsingAnalogPin() : measureUsingDigitalPin();
}
 
int AmbientLightSensor::measureUsingDigitalPin() {
  long startTime = millis();
  // Time how long it takes the diode to bleed back down to a logic zero
  while ((millis() - startTime) < 2000) { // max time we allow is 2000 ms
    if ( digitalRead(mLedPin)==0) break;
  }
  return millis() - startTime;
}
 
int AmbientLightSensor::measureUsingAnalogPin() {
  long startTime = millis();
  // Time how long it takes the diode to bleed back down to a logic zero
  while ((millis() - startTime) < 2000) { // max time we allow is 2000 ms
    if ( analogRead(mLedPin) < mAnalogThresholdLevel) break;
  }
  return millis() - startTime;
}
//8888888888888888888888888888888888888888888888888888888888888888888888888888888
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
   AmbientLightSensor led(12); // LED is hooked up to digital pin 12
  int led2 = 9; // led to indicate darkness is hooked up to digital pin 9
  int ledVal = led.measure();
  
  if (ledVal >300) {// a decent level of darkness
    digitalWrite(led2, HIGH);}
  else{
    digitalWrite(led2, LOW);}
 
  delay(200); // check every 0.2 secs

  // read the input on analog pin 5:
  int sensorValue = analogRead(A5);
  // print out the value you read:
  Serial.println(sensorValue);
  Serial.println(ledVal);
  
  analogWrite(6 ,( map(5,0,150,0,1023)));//( map(5,0,200,0,1023))
  delay(500);        // delay in between reads for stability
  if (sensorValue <400 && ledVal <10){
    analogWrite(5 , HIGH);
    analogWrite(6 ,( map(A5,0,150,0,1023)));
    delay(3000);
   analogWrite(5 , LOW);
  }

}
« Last Edit: October 22, 2012, 03:54:42 pm by april » Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The original idea came from Mitsubishi lab and you can google the article.

There are quite a few ways to implement it, depending on your hardware availability.
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I pay 7 US cents for CDS sensors here from a retail shop ? 

 But then I read about the RoHS aspect, which will eventually take over here in Africa ( we still have flaky unpainted asbestos roof sheets all over the place as an example )
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

0
Offline Offline
Full Member
***
Karma: 4
Posts: 218
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The original idea came from Mitsubishi lab and you can google the article.
There are quite a few ways to implement it, depending on your hardware availability.
I googled that site but did not get anything specific . I wonder if you remember anything else about the article ,as I would like to read it ,thanks
Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I feel sure Grumpy_Mike has done something on this with example video of using LED's as switches/lights.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

He has - http://www.thebox.myzen.co.uk/Workshop/LED_Sensing.html
Logged

0
Offline Offline
Full Member
***
Karma: 4
Posts: 218
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The class is in th first post .Put it in your program and then put the one line in the loop and you are doing it -pretty short and sweet . Grumpy Mikes is a nightmare to understand (excuse me Mike)

I pay 7 US cents for CDS sensors here from a retail shop ?  

 But then I read about the RoHS aspect, which will eventually take over here in Africa ( we still have flaky unpainted asbestos roof sheets all over the place as an example )

Funnily enough some truths on asbestos have been conveniently lost.
It was the blue asbestos product that was causing the trouble . It has tiny hooks on the fibers when magnified which hook into and stay in the lungs.Grey asbestos gave no known problems unless you were silly with it. The hooks were much smaller as were the fibers.Roof sheets are grey asbestos and fine sand and  cement and I would use them anytime. So were the "fibro" sheets used on the outside and inside house walls. Now they use a cellulose fibre ,fine sand and cement instead.and warn you of the silica dust . Ho hum

So much money to be made by hyping up the asbestos problem , they just don't bother telling you the truth.
« Last Edit: October 26, 2012, 03:06:40 am by april » Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this: http://www.merl.com/papers/docs/TR2003-35.pdf

and this: http://www.altera.com/literature/wp/wp-01076-led-driver-reduces-power-adjusting-intensity-ambient-light.pdf
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
they just don't bother telling you the truth.

It isn't about telling the truth. It is about "how can I benefit from that?".
Logged

0
Offline Offline
Full Member
***
Karma: 4
Posts: 218
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Thats good info right there - Thanks
Logged

0
Offline Offline
Full Member
***
Karma: 4
Posts: 218
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have just finished a program using this method. I tested to see what size difference it made .
On a tiny85 with 8192 bytes of space on the flash this method used 526 bytes and using a LDR used 442 bytes so this method adds 84bytes only. I was very impressed with that !
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would think it should take far less than that to implement, 100 - 200 bytes maybe? Maybe your figures contain overhead?
Logged

0
Offline Offline
Full Member
***
Karma: 4
Posts: 218
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I have to pay my expenses somehow.
I simply removed the function call to calculate this but I left the class definition in the code as I did not think that would affect things .It has to turn the pin on ,charge it for a millisecond ,turn the pin off and measure the discharge time to get a reading.
« Last Edit: October 28, 2012, 03:18:24 pm by april » Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The other approach is to charge up the capacitive sensor, and then perform an adc on it. The Chold in the adc will cause the voltage to drop a little - charge transfer effect. The lower the capacitance of the sensor, the more the voltage drops. That forms a basis for sensing if the sensor has been touched.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This week I spent some time messing around with an led light sensor. Hopefully someone will find the attached code useful, or, pardon the pun, enlightening...

Code:
/* LED_LIGHT_CONTROL_IBLE
 * -----------------------
 * This program uses separate led's for light and sensing light.
 * Works with room ambient or daylight.
 * LED light sensing modified from Hunter's Instructible. Acheives sharp
 * cut off and on according to "int light = X". Could also fade lights
 * in/out on analog pins.
 *
 * Hunter Carlson
 * June 9 2009
 * Edits added Nov. 9, 2012 -fabelizer
 *
 * Requires at least 2 (or 3 or more) LEDs, one to analog pins 4 & 5, other
 * to D8 & GND through a resistor. I used a water clear superbright
 * red led for the sensor led, white for led1.
 * See comments below for polarities and connections.
 */

int sense01 = 5;        // sensing LED anode connected to analog pin5
int sense02 = 4;        // sensing LED cathode connected to analog pin4
                        // you can switch out different leds on these
                        // pins to see which works best. Run the
                        // serial monitor and watch the changes.
                                               
int LED01 = 8;          // LED anode to dig pin8, cathode to 220R
                        // 220R to GND.
int LED02 = 9;          // LED anode to dig pin9, cathode to 220R
                        // 220R to GND.
                       
int val01 = 0;          // variable to store the value read from sense01
int val02 = 0;          // variable to store the value read from sense02

int light1 = 110;       // set light threshold for led1
int light2 = 80;        // set light threshold for led2

void setup()
{
  Serial.begin(9600);        // setup serial (comment out to save memory)
  pinMode(LED01, OUTPUT);    // led1 pin set to output
  pinMode(LED02, OUTPUT);    // led2 pin set to output
}

void loop()
{
  val01 = analogRead(sense01);      // read sense01 led
  val02 = analogRead(sense02);      // read sense02 led
 
   //debug print
  Serial.print(val01              // comment this section out to save memory
  Serial.println(val01-val02);    // result is printed and compared with light1 or 2
  Serial.println();               // blank line between values
 
                                    // first led 'led1'
 if ((val01 - val02) >= light1) {   // check if light in area
      digitalWrite(LED01, LOW);     // if light enough, turn off led1
    } else {                     
      digitalWrite(LED01, HIGH);    // if dark enough, turn on led1
    }
   
                                    // second led 'led2'
 if ((val01 - val02) >= light2) {   // check if light in area
      digitalWrite(LED02, LOW);     // if light enough, turn off led2
    } else {                     
      digitalWrite(LED02, HIGH);    // if dark enough, turn on led2
    }
   
delay(100);                         // just to slow things down a bit

}

enjoy!
-fab
Logged

Pages: [1] 2   Go Up
Jump to: