Reading analog input on the UNO

I have done several learning sketches demonstrating how to read from a potentiometer and a photoresistor through A0 and they work. I am somewhat confused. These tutorials and Arduino documentation state that analog voltages input thru A0 to A5 are converted to digital 0-1023. I get that. But in the photoresistor example the skrtch reads directly from the the photoresistor and I can see the reading on the serial monitor (ranges from 100's to 800's) directly. Why am I not seeing the 0-1023 values instead of the photocell values?
SAMPLE

int lightPen=A0;
int lightVal=1;
int dv=250;
int voidCtr=1;
void setup() {
pinMode(lightPen, INPUT);
Serial.begin(115200);
Serial.println("Begin the loop ");
}
void loop() {
lightVal=analogRead(lightPen);
Serial.print(voidCtr);
Serial.print(" = ");
Serial.println(lightVal);
delay(dv);
voidCtr=voidCtr+1;
}

The only thing I can think of is that I am in fact seeing 0-1023 but there is not enough room light to get above approx. 850. There is no formula to calculate a direct relationship from 0-1023 to something else (like 0-5 volts). Can anyone confirm or explain otherwise?
Brewster

A photo resistor is a resistor that changes value with the amount of light on it.

To get a voltage it is normally connected to another resistor and you measure the voltage between the 2... like this.
image

So the voltage you get will vary but it will not range fully from 0 to 5v... because R1 is never 0, and never infinite.

EDIT: sorry the original snippet I included had an incorrect/confusing formula for the Vout calculation. This one is better...

image

You can feed it with a higher voltage if you want to get the full count be sure not to put more then 5V into the Arduino. You can also use the MAP() function to get the range but it will not be as accurate as raising the voltage. If you know the light intensity you can convert your reading to lumens or whatever you want.

Thanks red_car and gilshultz. As I said I understand the photoresistor and the 2nd resistor. I believe it is called a voltage divider. Without the 2nd the voltage drop across just the photor would be a constant 5 volts (input voltage) or so and only the amperage would vary - possibly damaging the photr itself. Let me re-phrase. In the sketch I showed it does a simple analog read and then a print of the value read. In my office that varied from about 100+ to around 850 tops pointing a desk lamp right at it. Were those number being printed analog voltages converted to digital internally in the board to the rage 0-1023 (and I was just seeing the low end of them (0-850)? In other words if I had a really bright light source right over the photor, could it have crept up close to 1023? In other sketches I am learning the 0-1023 is converted via a formula to for example a delay number for switching on and off an LED or a passive buzzer. Maybe I answered my own question. Usually that 0-1023 doesn't really mean anything to us and it has to be related to a different range of values like frequencies, temperatures etc.

It really depends on the photo-resistor... you need the data sheet to confirm its upper and lower resistance values. Then if you also know the value of the static resistor, and Vin in the circuit, you can calculate the voltage you should be seeing... and therefore the analog values you should expect between 0 (0v) and 1023 (5v). Usually the value you receive is mapped to something that is of more use to you... for example if you only ever receive 100-900, and you want to drive a PWM output pin you could use...

int PWMvalue = map(analogRead(A0), 100, 900, 0, 255);

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.