Maybe it's because I don't have enough experience with electronics but I have a hard time understanding how the analogRead values are set. I understand what it measures and how. But how on earth does it know what the minimum and maximum values are of different parts like a potentiometer, CdS resistor and so on?
Ofcourse a zero resistance would be a good minimum value but it still doesen't explain how it knows when it hits max.
You usually hook "things" up to an anlog in on Arduino so it acts as a voltage divider between 5V and ground. so you will always get from 0V to 5V on the pin.
If you hook up something (like a LDR) that does not go all the way down to 0 Ohm then you just don't get the full 0 - 1023 range from the ADC.
If you hook up something that is not a voltage divider between Arduinos 5 and ground, you are responsible for securing that the voltage is allways between 0V and 5V.
Ok I got the voltage divider concept figured out. But that raises further questions. Actually theories, would this assumption be correct?
Lets say I use a potentiometer I have +5V in the first pin and ground in the third and in the middle I have a voltage divider connected to my analog in pin.
R1 = 0Ohm to 1KOhm, I´ll use 500Ohm for this calculation = 500Ohm
R2 = Full potentiometer resistance pin 1 and pin 3 = 1KOhm
Vin = +5V
But if I have a CdS resistor I have the first pin in +5 and the second one in ground/analog in. But I then I would be measuring after only one resistor. That would give +5V constantly. So therefore I must use a resistor with the highest resistance of the CdS resistor to connect the circuit to ground.
R1 = CdS resistor 2-5 KOhm, I'll go with 4KOhm here
R2 = 5 KOhm ordinary resistor
Vin = +5V
Almost right. The potentiometer IS a voltage divider in it self.
So you can hook the middle pin up directly to an analog in, turning the pot from 0 - max will give 0 - 5V on the middle pin. Arduinos ADC is optimized for using a 10K pot for this. It has something to do with hom much current is running through the pot.
And for the CdS cell yes you are right you need another resistor and then connect the junction of the CdS and the resistor to the analog in pin. You could even use a trim pot as the second resistor, this would give you adjustable sensitivity.
Yes, the value of the resistor connected to a CdS photocell will affect the sensitivity. It's difficult to adjust, though, because light levels vary so much. The eye is able to see in an incredibly wide range of illumination! Just going from night-time indoor light to daytime daylight may be too wide a range for a CdS photocell with a resistor.
Just mentioned that because I can imagine a case where the gadget work fine during development in the evenings in indoor light, then fails miserably as soon as it is tried in full daylight!
Actually, it gets a little better, if just slightly more complicated than the above answers indicate.
analogRead returns a "meter reading". Its a voltmeter, and it gives a reading from 0 to 1023, which is proportional to the voltage it sees.
0 is zero volts, but what does 1023, full-scale, mean? Normally it means 5 volts. But it doesn't have to.
By means of a simple command, one can change it to 1.1 volts full-scale. So the 1024-step scale then covers 1100 millivolts rather than 5000 millivolts. (Its different, 2.56 volts, on the older/smaller ATmega8 processors.)
And if you care to provide a different known reference voltage, and attach it to an Arduino pin, you can use that as your full-scale value.
Forum rules seem to mean the link has to go in my second post...
So the question is, "how does it know what the maximum is"? It has to have a reference voltage. As that_chap mentioned, you can change the reference voltage. But if you don't, then the 5VDC is the reference voltage.
The analog-to-digital converter is just describing a ratio, not a voltage. If the signal is equal to GND, or lower, you get 0.000000; if the signal is equal to the reference voltage, you get 1.000000; if it's somewhere in between, you get a number that is the same ratio in between. So halfway between GND and 5VDC gives 0.500000. One third of the way between GND and 5VDC gives 0.3333333.
Rather than return a complicated floating-point value, it multiplies the result into an easy-to-use integer instead: (ratio * 1023).
(If you're comfortable with thinking in binary, it's just the fractional part of the ratio, without the decimal binary point, to make it an integer. If that made no sense, ignore it. )
I meant integer in the mathematics sense, not the IEEE sense. The fractional part of a number has no sign. The mantissa of a floating-point encoding does.
Thanks for all the answers.. I think I acutally got most of it. Well except for the binary part. But then again I didn't even try to understand that.
My initial reason for asking was trying to figure out why some tutorials online used a resistor between ground and the sensor, and I certainly understand why and how that works now. Even more actually.
Big thanks to everyone who chipped in with advise!