A microcontroller cannot understand analog voltage directly. That is why we have to use an Analog to Digital Converter or ADC in short.
Atmega328 which is the brain of the Arduino Uno has 6 channels (marked as A0 to A5), 10-bit ADC. This means that it will map input voltages from 0 to 5V into integer values from 0 to (2^10-1) i.e. equal to 1023, giving a resolution of 4.9mV per unit. 0 will correspond to 0V, 1 to 4.9mv, 2 to 9.8mV and so on till 1023.
First, we will see how to measure voltage with a maximum voltage of 5V. This is very easy as no special modifications are required. To simulate the varying voltage, we will use a potentiometer whose middle pin is connected to any one of the 6 channels. We will now write the code to read the values from ADC and convert them back into useful voltage readings.
Reading the analog pin A0
value = analogRead(A0);
Now, the variable ‘value’ contains a value between 0 to 1023 depending upon the voltage.
voltage = value * 5.0/1023;
The obtained value is now multiplied by the resolution (5/1023 = 4.9mV per unit) to get the actual voltage.