wvmarle:
If you have a problem with your darkness value being too low (an analogRead() threshold of 2 is not the best imho, I'd want to bring it up to 10 or 20), you could replace the resistor wired with the LDR (I assume you wired this as voltage divider) to a much higher value. It'll of course also push higher the "day" value.
A second way of preventing jitter is to implement a delay before another change can be made to the door - e.g. can't open within 30 mins after closing and the other way around. That would give you more flexibility in the open/close darkness values.
Hi wvmarle,
The reason I had the darkthreshold at 2 was I thought that the lower the number the darker it has to be before it will trigger?
If I am understanding what you suggested above - if I increase the darkthreshold to say 10 and increase the resistor value it could fix the problem?
Current resistor value is 410K
I was also thinking of replacing the LDR at present have -
Cadmium Sulphide (CdS) light dependent resistor cell. -
(Similar to Philips ORP12- Dark Resistance:
min10M- Light Resistance:
48k - 140k- Max Dissipation:
30mW- Rise Time: (0~63%) 40mS- Decay Time: (100~37%) 10mS- Dimensions: 5.1mm x 4.3mm ±0.2- Lead spacing: 3.4mm)
I can replace with -
(Cadmium sulphide (CdS) light dependent resistor cell. As used in our Short Circuits projects.
larryd:
Did you use the exact code in post #13 or did you change anything in it?
Do some testing, change the following line, then cover the sensor to see what happens.
if(millis() - delayMillis < 30 * 60 * 1000ul)
For testing, change to this for 30 second delay.
if(millis() - delayMillis < 30 * 1000ul)
darkthreshold = 2 <-----<<<< this is very very low, try ~10
You could add some code that, once darkness is triggered, only allow the door open if the sensor > ~60 for a period, but first check with 30 seconds.
.
Hi larryd,
Used the exact code.
As the code and door is all set up and not close by I will have to do some testing using code and making up a simple sketch. maybe a LDR and LED ?
Also read the above post to see if that makes any sense.
BTW, best source resistance for an analog input is ~10K.
What do 'you measure' for resistance for the existing sensor during the two times of day, i.e. when 2 and 175.
This new sensor has these ranges, but what is the resistance for your desired light levels?
Dark resistance: 0.5M ohm min.
Light resistance: 2.8K ohm min 8.4K ohm max.
Did you ever post a schematic?
2 is not reasonable.
EDIT:
IMO I would feed the output from the light sensor to two adjustable LM339 comparators ccts (with hardware hysteresis.
One set for day, one set for night, feed the outputs to digital inputs.
You could still incorporate your 30 minute delay at night in your sketch.
I was also thinking of replacing the LDR at present have -
Cadmium Sulphide (CdS) light dependent resistor cell. -
(Similar to Philips ORP12- Dark Resistance:
min10M- Light Resistance:
48k - 140k-
That are very high values. You will be well into noise at a threshold of 2 (anything under 10 I wouldn't consider reliable at all - especially having seen response curves of the ADC).
To increase your voltage on the ADC, you'd have to increase that resistor value even more, but then you're running into the problem of the ADC possibly not getting enough current and becoming more prone to noise.
From the readings you post it seems like you're switching "dark" when the LDR is >10 MOhm, and "light" when it's at about 2 MOhm. In that case your resistor should be in the tune of 5 MOhm. That's a voltage divider with too high values for the ADC to work well. Those have to come down by 1-2 orders of magnitude.
The second LDR you list sounds much better. Do a few simple measurements: measure the resistance of the LDR at the light level you want the gate to open (holding the LDR in the position and direction you want to mount it of course), and when you want the gate to close. Use a resistor value that's somewhere halfway that point or at least in between, and you'll have the largest possible difference in ADC readings. Based on the specs and what you have now I expect you will end up at 100-500k for the mid-way value - so the existing 410 kOhm one may do just fine.
I have attached a diagram of the hardware for you to have a look at.
You will see there is only one resistor for the LDR.
I am going to revert back to the original sketch and change the darkthreshold to 10
and see what happens. I recon it still will close too early.
As a sidebar I now have another problem as the up down is controlled by covering or allowing light on the LDR so the 30 minutes delay is going to be an issue. May have to install a manual switch ?
A manual override switch sounds like a very good idea. This you may even wire in a way that it completely bypasses the Arduino.
I had a look at your image, considered linking it in this post, but as it's a Fritzing it's worthless. It's impossible to see what is linked where, what values of components you have, etc. You'd better draw a proper circuit diagram, there's excellent free software for this, such as KiCAD.
I have been reading up on how LDR works and discovered I had no real understanding.
So the information I posted was incorrect even though the logic was kind of right.
What through me was what the variable darkthreshold represented.
I though this represented the LDR reading at low light which would trigger the
door to close, where all it did was stop the door from jittering because of fluctuation of light.
I will now play back my understanding on how a LDR works and how to fix the door closing to early.
A LDR measure resistance of light 0 to 1023. 0 is the most light and 1023 is least light.
In theory dawn and dusk would be about the same point in the LDR reading, light would be increasing
for dawn and decreasing for dusk.
At present I have a reading of 175 which opens the door (I don’t know the exact time as I am never there to see it happen)
There is no issue if the door opens a little earlier or later.
Closing is to early so I need to increase 175 and monitor till I get the correct door closing time.
Hopefully this all makes sense, please comment if I have again missed something.
Another great way of doing this without Arduino is using a 555 to do the switching. It's got a built-in hysteresis. Should result in a simpler circuit than with the OpAmp but may be harder to adjust.