analogReadResolution(8)

Hi

I would like to know if analogReadResolution(8); will work on Arduino nano.

When I remove the 8bit resolution line all works fine.

This is the error I get.

Arduino: 1.8.5 (Windows 10), Board: "Arduino Nano, ATmega328P"

C:\Users\mike\Desktop\Lcd 4 Wire\lcd4wire\lcd4wire.ino: In function 'void loop()':

lcd4wire:28: error: 'analogReadResolution' was not declared in this scope

       analogReadResolution(8);

                             ^

exit status 1
'analogReadResolution' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Thanks in advance.

Mike.

(deleted)

If you want to get an 8-bit value instead of a 10-bit one from analogRead(), just right-shift the value 2 bits, eg:

byte analogValue = analogRead(pin)>>2;

Another post was made while I was typing,yes thats what I thought.

Why is analogReadResolution not available on a Nano?

mikedb:
Why is analogReadResolution not available on a Nano?

Because Nano does not support 12 bit analog read and if you want 8 bit resolution you shift right by two :slight_smile:

analogReadResolution was added when boards with a >10 bit ADC were released. In order to maintain compatibility with existing code (which assumed 0~1023 output), they had to make it default to 10bit mode - however, to allow people to take advantage off the improved resolution if they were making a sketch that was only for boards with 12-bit ADCs, analogReadResolution was added to enable that.

The classic AVRs don't have a way to set the resolution - it comes out 10-bit, and if you want less than that, you right-shift it (meaning, you can't get faster ADC conversion by lowering resolution like I think you can on the newer boards)

Try this: Connect an LDR, thermistor or pot to A0.

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  int val = analogRead(A0);
  Serial.println(val);                          // 0 to 1023
  Serial.println(ADCL); // low 8 bits
  Serial.println(ADCH); // high 2 bits
  Serial.println(val >>2);                  // 0 to 255
  Serial.println();
  delay(2000);
}

Thanks a lot.

I use read0 = analogRead(A0); from DrAzzy,s post above.

Works for me.