Pages: [1] 2   Go Down
Author Topic: LDR response time  (Read 2296 times)
0 Members and 1 Guest are viewing this topic.
Croatia
Offline Offline
Sr. Member
****
Karma: 11
Posts: 443
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I built a small night light that turns on when the room light is turned off, glows for a bit and then dims down, giving me enough time to find my bed, in what used to be pitch black darkness, once I turn off the lights.

It works as advertised. LDR connected to an ATtiny45, driving a few LEDs through an ULN2003.
The only thing that somewhat bothers me is the fact that LEDs don't light up the moment I turn off the room light, but it takes about half a second (eyeballing here).
When I envisaged this project in my mind I had an image of it turning on so fast that the delay is not noticeable, yet here it is. Certainly less than a second, but noticeable.

To troubleshoot I added detection of turning on the lights, and it seems significantly faster.

I found some old datasheet for an LDR (not the one I'm using), and surprise, surprise, rise and fall response times differ by the factor of 4-6 (depending on the model), but are still below 90ms (usually below 35 ms) on the slower end.

So, while this is more of a rant than a cry for help, is there a way to decrease the response time? Phototransistor, maybe?
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 82
Posts: 3846
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not just re-wire the room lights with 2x SPDT switches like traditionally done on a stairway, with one by the bed, so you can just switch the light off from the bed or by the door?  smiley-lol
Logged

The Elders of the Internet know who I am.....
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Croatia
Offline Offline
Sr. Member
****
Karma: 11
Posts: 443
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, the thought of demolishing half a wall is kind of putting me off.
We still build our houses with bricks and concrete here.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

90ms is certainly LONG but human should find it "lightning fast", as matter of fact, lightning usually lasts 100ms. Not sure how you build it, but when I built my sound/light detector using analog read, it was not noticable at all.

Basically I put a photodiode + into A0 of Arduino and - into ground and do analogRead and compare values, when difference between two reads exceeds certain value, it is an event . . . here is the thread:

http://arduino.cc/forum/index.php/topic,148666.0.html

Logged

Search for "mjkzz" on eBay :-)

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course, a photodiode like the PiN diode sold by Sparkfun at https://www.sparkfun.com/products/9541 will be faster than an LDR, your circuit should be fast to a human.  How about posting a schematic and you code?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Without seeing your schematic and your code we can't really help.  All I can say is that it is generally taken that any two actions which take place at around 20mS or less apart are perceived as happening at the same time by the average human.  20-50mS most see them as pretty much simultaneous.  >50mS and the majority of people see them as completely separate events.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Croatia
Offline Offline
Sr. Member
****
Karma: 11
Posts: 443
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Really, majenko? 50 ms? That is in the ballpark of what datasheets said.

Code, and partial schematic.

Code:
int ledPin = 0;
int ledState = 0;
int sensorPin = 3;
int sensorVal;
int limit = 20;
int pause = 100;

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

void loop ()
{
  sensorVal = analogRead (sensorPin);
  if (sensorVal > limit && ledState == 0)
  {
    ledState = 1;
    analogWrite (ledPin, 255);
    delay (30000);                            // 30 seconds of light
    for (int i = 255; i >= 0; i--)            // start the dimming
    {
      analogWrite (ledPin, i);
      delay (pause);
    }
  }
  if (sensorVal <= limit-1 && ledState == 1)
  {
    analogWrite (ledPin, 255);                // indicates that the room light has been detected
    delay (300);
    analogWrite (ledPin, 0);
    delay (300);
    analogWrite (ledPin, 255);
    delay (300);
    analogWrite (ledPin, 0);
    delay (300);
    analogWrite (ledPin, 255);
    delay (300);
    analogWrite (ledPin, 0);
    ledState = 0;
  }
}


* nigthLight_640x413.png (28.18 KB, 640x413 - viewed 40 times.)
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

LDRs are known for having slow response times in low light. A phototransistor is more suitable for that application.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 158
Posts: 2888
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Besides the possibility of going to phototransistors, it should be possible to use adaptive code
for quicker responses. The turn-off time will be dependent upon what threshold you're using
- ie, your limit variable - and also the ambient LDR level with the lights on. So, maybe you
can read the average ambient level with the lights on, and select a threshold level based upon
some percentage of that, maybe 75 or 80%, etc, rather than using a fixed threshold value.
Logged

Croatia
Offline Offline
Sr. Member
****
Karma: 11
Posts: 443
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys.
I had some problems with determining the value for threshold (limit variable). When using Uno and serial print to read the value it was significantly different than what worked on ATtiny.
Also the value 20 is rather fragile. For some, if I reason put anything else there the detection stops working (neither 21 or 19 work).

In the second part "if (sensorVal <= limit-1 && ledState == 1)" limit-1 is also the only way for it to work.

For now, I'm fairly happy how all this functions and I don't think I'll be messing with the code unless I find a better light sensor (transistor).

When I started this I knew LDRs were slower than phototransistors, I just didn't think that the delay would be noticeable to the naked eye. Also, I had a few LDRs sitting around and no transistors, so the choice was obvious smiley
Logged

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 158
Posts: 2888
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Also the value 20 is rather fragile. For some, if I reason put anything else there the detection stops working (neither 21 or 19 work).
This doesn't sound at all reasonable. There is something going on here you're not telling
us about. For one thing, do you have any idea what the actual LDR readings are for
the lights-on, lights-off conditions in the room? You have to know that info as a starting
point.
Logged

Croatia
Offline Offline
Sr. Member
****
Karma: 11
Posts: 443
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An unconnected LDR of the same make and model shows:
Lights on, 10 kOhm
Lights off, more than 40 MOhm (that's how far the meter goes and it shows no connection). If I add a little bit of light I can measure it in tens of MOhm range, but you get the picture, pretty high resistance.

The one soldered on the device shows 220 Ohm in darkness and 205 Ohm with lights. So it appears I might have burned it while soldering.

In any case,
analogRead on Uno returned around 680 to 750 (depending which direction LDR was facing relative to the light bulb) with lights on and 950 to 1020+ in semi darkness produced by my monitors shining from 50 cm away.

But like I said, those values did not work when I transfered everything to tiny, possibly due to my pyrolytic actions.
Logged

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 158
Posts: 2888
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If the actual analog readings are roughly 700 in one case and 1000 in the other
case, it doesn't sound like limit = 20 is gonna work. ???

In any case, you might swap out the LDR, verify a good set of readings, and go
from there in selecting the threshold value.

Also, take a good look at your check statements, there are actually 4 possible
conditions, but you only check 2 of them. This is probably not the best scenario.
Is it possible to get stuck in one of the other 2 conditions? A good state-machine
will actively account for all possible states.
Code:
  if (sensorVal > limit && ledState == 0)
....
  if (sensorVal <= limit-1 && ledState == 1)
Logged

Croatia
Offline Offline
Sr. Member
****
Karma: 11
Posts: 443
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I intentionally left the gap between the condition for turning on and off.
The resistor value is not exact and fluctuates somewhat.
Usually that would be no problem since there would be a significant difference in operating values (light / darkness), but for some reason there is virtually no gap in my case. Darkness is at around 18 and light is detected at 22 or so. The gap is there to ensure that that fluctuation does not enter the wrong condition.

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I made up a similar circuit, and can't reproduce your problem at all.

When I turn off my test light the LED comes on instantly (as far as I can see).

I made it up on a Uno and added some serial prints to confirm the values I was getting on the analogRead.
Logged

Pages: [1] 2   Go Up
Jump to: