Search code examples
arduinoesp32arduino-unoarduino-c++

Getting the ADC resolution


A fresh Arduino/microcontroller user here! my project uses multiple Arduino Uno, Nano, and ESP32 boards and im trying to create a library to standardize a chunk of code that all boards use. I was wondering if there is a way for me to programatically obtain the ADC resolution of my board so I can dynamically and appropriately set the ADC resolutions for my script.

Thank you!


Solution

  • The only way is to put an analog pin at the maximum level and read it to look at the measured range. If you need this pine further, just put it back to standard INPUT. Although this method is a little pulled by the hair, it works and I don't see any other ...

    void setup() {
      Serial.begin(9600);
      Serial.println("Resolution = " + String(readResolution()) + " Bits");
      delay(1000);
      //return pin to standard input
      pinMode(A0, INPUT);
    }
    
    void loop() {
    }
    
    int readResolution() {
      pinMode(A0, INPUT_PULLUP); //set input = Vcc
      int val = analogRead(A0);  //read input
      if  (val > 500 && val < 512) return 9;
      if  (val > 1000 && val < 1024) return 10;
      if  (val > 2000 && val < 2048) return 11;
      if  (val > 4000 && val < 4096) return 13;
      //etc...
    }