Go Down

Topic: How does analogRead know minimum and maximum value (Read 4354 times) previous topic - next topic


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.

Best regards


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.


So in a sense it doesn't know what max and min are?

I think I should read up on voltage divider before I ask any more questions.

Thanks for the quick reply.


you are right. Or one could say that in most cases min is allways 0V and max is allways 5V.

Except when the device hoode up do not go all the way down to o Ohm.

Fortunately a voltage divider is a simple and easy to understand concept.


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

Vout = (R1 / (R1+R2)) * Vin = (1000 / 1500) * 5 = 3,3333

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

Vout = (R1 / (R1+R2)) * Vin = (5000 / 9000) * 5 = 2,7

In a sence I could make the CdS resistor more or less sensitive by using a smaller or larger resistor as R2.

Have I got it right?


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...


To change the full scale voltage, you use the analogReference command



Jan 11, 2009, 08:01 pm Last Edit: Jan 11, 2009, 08:02 pm by halley Reason: 1
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. :) )


"to make it an integer.  If that made no sense, ignore it. :) ) "

Ok, I'll go along with your analogy, if you make that an unsigned integer.  ;)



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!

Go Up