How to get the present analogReadResolution()

I am trying to take some of my routines and make them into header files. I haven't done that in ages.

  1. I want to get the analogReadResolution set for the device
  2. I want to read the battery Voltage using the built in resistor divider {if there is one for that device-- will need a lookup table i imagine)

Rough draft follows:


```cpp
#define versionInfo "See if i can remember how to write & use a .h file"

// ~9 (A7) is the Feather Vbatt measure pin.  DO NOT USE IT FOR ANOTHER INPUT!
// battery pin -- ** this will eventually need to be set for each platform with a lookup table ***
#define VBatPin A7  // THIS IS THE D9 BATTERY CHECK PIN - has built in 100k/100k divider (called D9, ~9, PA07, Y5, or AIN7 on schematics) note: AIN7 doesn't work!

// analog Voltage ref-- again, needs to be set by device.  Manual override could have it passed to the routine as an option or just make fuinction invalid
#define Vref 3.35  // volatge reference for AD converter

#define battDeadVoltage 3.55         // we need to flush the SD Card and post flashing light- ADC inflates below 3.5V.  Data loss is impending.
#define battLowVoltage 3.7           // This value sets where the warning starts for Vbat being low
#define battFull_ChargeVoltage 4.17  // If there is a 3.7V lithium battery, if the value is higher than this it is probably not connected to the M0


float getBatValue(void) {
  // built-in double-100K resistor divider on the BAT pin connected to {D9 (a.k.a analog #7 A7) on Feather}.
  // Read built in divider voltage, double it  --> battery voltage.
  // Actual Feather Calculated Battery Value including ADC error as Vcc drops
  // warning: nonlinearity with low Vbatt: 3.75 V	--> 3.75 V, 3.50 V -->	3.50 V, 3.25 V -->	3.42 V, 3.00 V -->	3.43 V
  float measuredVBat = 0;

  // NEED SOMETHING TO GET ANALOG READ RESOLUTION HERE


  measuredVBat = analogRead(VBatPin) * 2 * Vref / (pow(2, getAnalogReadResolution()) - 1);  // resistor-divider is Vbat/2, so double it.  Mult by ref Voltage (3.3). Convert AD -> Voltage
  return (measuredVBat);
}  // end getBatValue


double getAnalogReadResolution(void) {
  double readResolution = 0;  // pow by definition wants a double, even though we know this is an analog (bits) value

  // code here to read the analog read resolution and return it to calling program
  return (readResolution);
}

I don't think that function exists. I've not looked into the complexities of making it, but given the variety of boards these days, I'd guesstimate it as approximately "too-darn-difficult-to-bother-with-it".

https://www.arduino.cc/reference/en/language/functions/analog-io/analogreadresolution/

[quote]
analogReadResolution() is an extension of the Analog API for the Zero, Due, MKR family, Nano 33 (BLE and IoT) and Portenta
[/code]
For any arduino with a fixed resolution, you could check for the processor type.

2 Likes

Yes. Note that it sets the resolution - it doesn't read it.

Are you expecting it to change during a run? Or just be different when compiled for different hardware?

Maybe #defining a MAX_ADC_READ_VALUE inside a bunch of hardware-identifying #ifdefs would do what you need?

Ah, I was a bit too quick posting.

Might be necessary to read back the processor registers (if possible) to find the current resolution.

Indeed, and if cros-platform compatibility is a requirement (which I take it is, given what's provided in #1), this will be, well...a bitch, really.

Crude, but effective and probably the most realistic solution.

Thank you everybody. I was afraid that would be the case-- i don't want the hardware default as i often change it depending on the application.

Again- thanks for all the help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.