I need to understand how the analog read works.
It divides 5v into 1024 slots, and assigns each value to one, but what if I want the 1024 slots to be assigned to a different range of voltage, say 0-2 Volts, will it divide 2/1024 for each slot ?
You can use 2V for the analog reference and then it will read 0 to 1023 for 0 to 2V.
Or use an external voltage divider on the input to the analogue pin.
Yes, by changing the analog reference analogReference() - Arduino Reference
It divides 5v into 1024 slots,
Strictly speaking it divides the reference voltage into 1024 slots.
Normally the reference voltage is the Arduino's operating voltage - 5volts.
If you read the analogRefence documentation you will see that there are other options, including an external reference voltage that you supply.
Note that ALL the voltages must be between 0 and 5 volts.
I’ve never used the external reference, but I’ve got an application where I automatically switch between the 5V and 1.1V references.
If I’m on the 1.1V range and I read 1023, I switch to the 5V range (or maybe I switch at 1000, I don’t remember exactly).
If I’m on the 5V range and I read less than 200, I switch to the 1.1V range.
I’m not doing anything critical where I need accuracy or precision but I want it to work with a wide voltage range. (I’m doing sound activated lighting and I want it to work with a wide range of volume levels.)
It divides 5v into 1024 slots
I’d use the word “steps” instead of the word “slots”. Technically speaking the analog voltage is [u]quantized[/u] to a (digital) integer.
If you use the 1.1v reference, then you probably should use a voltage divider/pot to reduce the 2.0v to 1.1v for the analog input pin.