analogReadResolution() is an extension of the Analog API that let you set the resolution (in bits) of the value returned by analogRead(). It defaults to 10 bits (returns values between 0-1023) for backward compatibility with AVR based boards.
Within the Arduino boards you can find a variety of resolutions that lead to different ranges:
10 bits: allow values between 0 and 1023. 12 bits: allow values between 0 and 4095. 14 bits: allow values between 0 and 16383. 16 bits: allow values between 0 and 65535.
Use the following function to set the ADC resolution of your board:
analogReadResolution(bits)
The function admits the following parameter:
bits: determines the resolution (in bits) of the value returned by the analogRead() function.
You can set this between 1 and 32. You can set resolutions higher than the supported 12 or 16 bits, but values returned by analogRead() will suffer approximation. See the note below for details.
The function returns nothing.
The code shows how to use ADC with different resolutions.
void setup() {
// open a serial connection
Serial.begin(9600);
}
void loop() {
// read the input on A3 at default resolution (10 bits)
// and send it out the serial connection
analogReadResolution(10);
Serial.print("ADC 10-bit (default) : ");
Serial.print(analogRead(A3));
// change the resolution to 12 bits and read A3
analogReadResolution(12);
Serial.print(", 12-bit : ");
Serial.print(analogRead(A3));
// change the resolution to 16 bits and read A3
analogReadResolution(16);
Serial.print(", 16-bit : ");
Serial.print(analogRead(A3));
// change the resolution to 8 bits and read A3
analogReadResolution(8);
Serial.print(", 8-bit : ");
Serial.println(analogRead(A3));
// a little delay to not hog Serial Monitor
delay(100);
}
If you set the analogReadResolution() value to a value higher than your board’s capabilities, the Arduino board will only report back at its highest resolution, padding the extra bits with zeros.
For example: using the UNO R4 with analogReadResolution(16) will give you an approximated 16-bit number with the first 14 bits containing the real ADC reading and the last 2 bits padded with zeros.
If you set the analogReadResolution() value to a value lower than your board’s capabilities, the extra least significant bits read from the ADC will be discarded.
Using a 16 bit resolution (or any resolution higher than actual hardware capabilities) allows you to write sketches that automatically handle devices with a higher resolution ADC when these become available on future boards without changing a line of code.