Hi guys, I would like help on designing a dark sensing circuit which outputs either a logic high or low depending on the threshold set.
I have included a schematic of the circuit I am using at the moment. The component values were chosen mainly by trial and error. I am using a 10Kohm potentiometer to set the threshold. Any ideas on improving the circuit? I was hoping that someone could help me go through with calculation on choosing component values. I'm having trouble setting the threshold to the exact point I want.
For further reference what other sort of circuit could I have used to get the desired output. I also thought of using a comparator (good idea?).
I also thought of using a comparator (good idea?).
Yes a good idea as the threshold will be more consistent.
As it is there is not a lot wrong with your circuit although the threshold will not be as precise as using a comparator.
It is difficult to calculate the values because it will depend on the exact gain characteristics of the transistor's turn on voltage. This will change with temperature and will be "soft" if the light fades slowly.
If so, I suggest you forget the transistor or comparator and connect the junction of VR1 and R3 to an analogue input and see what values you get from the A2D converter at the point you want to switch.
I agree, the transistor is a complication, the analog inputs on an Arduino will give a fully programmable threshold
which means you can apply some hysteresis, often necessary for such circuits to avoid on-off chatter during
the transition over the threshold.
Selecting a fixed resistor instead of a variable resistor will be much more stable over time. Arrange that
the threshold voltage is roughly mid-rail (halfway between ground and supply), but its not crucial as the
actual threshold is programmable.
Note that light sensitive resistors are often quite sensitive to temperature too, which may give you problems if there
is a lot of temperature variation and accuracy is important.
Photodiodes are very much superior for temperature stability except at very low light levels when leakage
currents become significant.
Thanks everyone. Normally I would use the A/D converter of the arduino, but for this particular project I'm using two LDRs and I'm adding two individual potentiometers to adjust the thresholds.
The reason why I want to get the solution from the hardware side is because
It keeps the overall code simple.
The value received from the LDRs are irrelevant.
I'm under the impression that reading from the analog pin (especially if there are many pins to read) - it becomes quite unstable and overall it takes longer time for the whole process compared with doing a digital read. Please correct me if I wrong.
Yes, an analogRead() is very slow compared to a digital read, about 1,000 times slower.
Mind you: that's slow in microcontroller terms. A digitalRead indeed takes more like 0.1-0.2 microseconds. An analogRead() takes about 0.11 ms - you can do about 9,600 reads per second. That in turn is about 1,000 times faster than the reaction time of an LDR.
For a dark sensing circuit, reading 10 times a second will give an "instant" reaction for a human observer. The relative slowness of an analogRead() is still very fast compared to this.
And if that's really still taking too much processor time (I've never encountered a situation where you really need that last 0.1% of processor time - never even gotten close), there is a ways to make taking an analog reading as fast as a digital reading. Set it in free running mode, it'll do conversions in the background, and you just read the register for the latest reading when you need one. No need to wait that 110 us.
So to summarise your concerns:
when you do it in hardware your code is marginally simpler, but you will have a hard time adding a useful hysteresis. Without it you'll see a lot of on/off around the trigger point.
the actual value is irrelevant indeed - still you have to measure it once. Either by setting your pot, or by setting it in your software. The latter is more stable.
wvmarle:
Yes, an analogRead() is very slow compared to a digital read, about 1,000 times slower.
Mind you: that's slow in microcontroller terms. A digitalRead indeed takes more like 0.1-0.2 microseconds. An analogRead() takes about 0.11 ms - you can do about 9,600 reads per second. That in turn is about 1,000 times faster than the reaction time of an LDR.
For a dark sensing circuit, reading 10 times a second will give an "instant" reaction for a human observer. The relative slowness of an analogRead() is still very fast compared to this.
And if that's really still taking too much processor time (I've never encountered a situation where you really need that last 0.1% of processor time - never even gotten close), there is a ways to make taking an analog reading as fast as a digital reading. Set it in free running mode, it'll do conversions in the background, and you just read the register for the latest reading when you need one. No need to wait that 110 us.
So to summarise your concerns:
when you do it in hardware your code is marginally simpler, but you will have a hard time adding a useful hysteresis. Without it you'll see a lot of on/off around the trigger point.
the actual value is irrelevant indeed - still you have to measure it once. Either by setting your pot, or by setting it in your software. The latter is more stable.
it's plenty fast enough.
Thanks for clearing the doubts I had. Interesting point about LDR's reaction time. I thought it would have been pretty instantaneous (compared to controllers A/D etc).
I had some bad experience getting erroneous results when I was using 3 LDRs and using the A/D in another project. I tried everything like adding delay nothing seemed to help the situation. I even came across some people mentioning the same behavior around the forums too. Have you had this problem? and I figured in this project using a hardware setup could get rid of this.
'Free running mode' now that's new! I need to do more research on this.
Thanks for the help.