I have a problem with a simple project (servo moving depending on LDR value), and I an not too sure what the problem is.
Now, if I take the servo out the equation (not wired into the circuit), the serial output gave me values around 46-48 in ambient light and between 10 and 7 with my finger on the LDR. When I wire the servo in (as described below) I get a value between 75-79, then a value of 46-48 and this repeats (in ambient light) [79, 45, 79, 45, 79...]. With my finger on the LDR, I get the value between 75-82, then a value of 10-7 (and this repeats) [79, 10, 79, 10, 79...].
I don't understand why I get one read in the high 70's, then a good read, then another high 70's, then another good read (repeating).
I have a LDR as a voltage divider (with a 680R resistor). The circuit is wired as follows: VCC is going to one leg of the LDR, with the "analogue read" wire (going to A0) and one leg of the resistor. The other leg of the resistor is going to ground. I also have a servo with the VCC and GND connected and the "data" wire going to pin 9.
How are you powering the servo? You shouldn't run a motor or servo from the
5V rail on the Arduino, that's asking for trouble. For small servos a 6V 1A supply
is a reasonable choice, larger powerful servos may need 2A or more peak.
I think you're getting electrical noise from the servo into the input. But usually, noise is more random. A capacitor across the resistor (between the analog input and ground) will help to filter-out noise. Try 0.1uF, or maybe something bigger. A big capacitor will slow-down the changes when light is added/removed, but this often is not an issue in LDR applications.
Long wires on the input make a "better antenna" and they will pick-up more noise. If you are running long wires, and/or if the wires to the LDR run next to the servo wires, shielded cable should help.
You may also need to do some simple digital filtering (averaging a few readings, etc.) to "smooth-out" whatever you are doing.
Also, a larger resistor will give you bigger readings. That might make it easier to program. (But, a bigger resistor will also make it more sensitive to noise pick-up, so be sure to include the capacitor.)
As I said in my last post, thank you for your replies.
As MarkT said, I needed to separate the power supply for the servo. Also, I notice when I put a 0.1uF capacitor across the resistor (as DVDdoug suggested), the deviations of the value when in ambient light was much less. So, thank you again guys......
For some reason, all my LDR circuits are wired with the resistor between +5 and analog input, and the LDR between analog input and ground. When light is off, the LDR resistance is higher and the analog voltage input is higher than when the light is on. Not sure if it makes a difference or not.