Sorry for the newbie question but I wasn't sure where else to post it.
I'm going through the Getting Started with Arduino book (awesome by the way) and I'm playing around with a photocell, monitoring the values through Serial. 5V comes out of the Arduino, goes to the photocell, then from there it branches -- one lead to analog pin 0 and another to a 10k resistor and then back to ground. Here's the layout from the book:
I hooked everything up and am monitoring values through Serial.
I started swapping other resistors for the 10k and you can see the average analogRead() value in my living room changes based on the resistance between the photocell and ground:
920 with 10k resistor
840 with 4.7k resistor
490 with 1k resistor
320 with 470 resistor
If I remove the resistor completely then the values shoot up to 1023 and don't change.
My question is: how is this actually working internally? Does the Arduino compare the amount of voltage coming in through analog pin 0 against the voltage coming back into ground and that difference is represented by 0-1023 when read in?
The CPU is reading the amount of current that's output through the pin. The output is +5V and the voltage drops to ground though your sensor and the resistor.
If you remove the resistor, there's no connection to ground so there's no current. The resistor is there to avoid a short -- a direct connection between power and ground. This would occur if the sensor was pushed to having no resistance, say by a very bright light source.
Arduino does compare the amount of voltage coming in through analog pin 0 against a reference voltage (by default 5volts, see analogReference() - Arduino Reference)
The ratio of the input voltage to the reference is represented by values from 0 to 1023.
BTW, I think Ben meant voltage instead of current.
If the reference is always 5V then how come the value on analog 0 was different based on the resistor I was putting between the photocell and ground?
And come to mention it, why would I need another resistor at all? The photocell is doing plenty of resisting on its own, assuming it wasn't in direct sunlight...
It's the ratio of the input voltage to the reference that determines the value. This ratio will change when the input voltage changes. The input voltage will change when the voltage divider you create with your photocell and resistor change.
The CPU is reading the amount of current that's output through the pin.
This is not correct; the Arduino analog input do not measure current. In fact, the analog pins measure the amount of voltage present on the pin. This voltage may vary from 0V to +5V, and that variation will correspond to 'analogRead()' values of 0 to 1023.
ADC reported value = (Input Reading / Reference Voltage)*1023
Example:
You make a voltage divider with two 10k resistors. One end connected to 5V, one end connected to ground. If the resistors are exactly equal and the voltage supply is exactly 5V, this should be the result:
2.5V on the input pin (Input Reading) with a voltage reference of 5V (default on most boards)...
(2.5V/5V)*1023, which is around 511 to 512.
By changing the resistor in series with your photocell (while tapping your voltage reading at the junction of the two resistors), you're changing the ratio of the two resistances, thus changing the voltage at the tap/junction.
Voltage Reading = [Rphotocell / (Rphotocell + Rseries)] * 5V
Say you had a 10k Rseries and your photocell was at 5k due to lighting conditions:
Voltage Reading = ( 5k / 15k ) * 5V = 3.33...V
Your Arduino should report:
(3.33V/5V)*1023 = 682
By the way, the 1023 comes from the fact that the ADCs present on the ATMEGA's analog pins is 10-bit capable.
2^10 - 1 = 1023, which is the number of voltage levels it is capable of discerning between.
A 24 bit ADC (found in high quality digital audio I believe) would be able to discern between 2^24 - 1 or 16.7M (16,777,216) levels.
Yeah, I hadn't thought of the fact that the photocell and the resistor create a voltage divider (still pretty new to a lot of these concepts!). I'm going to play around with my multi-meter and a couple of batteries to really help solidify all this stuff in my mind. Thanks again!