Page 1 of 1

Feather M0 - Lipo Voltage reading - Bug in tutorial ?

Posted: Fri Apr 29, 2016 8:56 am
by mchobby
Hi,

In the M0 tutorial... https://learn.adafruit.com/adafruit-fea ... ng-battery

you are reading the Lipo Voltage and convert the reading to voltage with the following code.

Code: Select all

#define VBATPIN A7
float measuredvbat = analogRead(VBATPIN);
measuredvbat *= 2;    // we divided by 2, so multiply back
measuredvbat *= 3.3;  // Multiply by 3.3V, our reference voltage
measuredvbat /= 1024; // convert to voltage
Serial.print("VBat: " ); Serial.println(measuredvbat);
However, the M0 ADC have a resolution of 12 bits so the value should go from 0 to 4095 when reading the voltage.
So the line

Code: Select all

measuredvbat /= 1024; // convert to voltage
should become for the feather M0.

Code: Select all

measuredvbat /= 4096; // convert to voltage
Am'I right?

Re: Feather M0 - Lipo Voltage reading - Bug in tutorial ?

Posted: Fri Apr 29, 2016 7:38 pm
by mailhouse
I get sane values returned for 1024 but not 4096. I dont have an explanation why your thought process seems correct but the results dont jive, but i'm new to all this.

Re: Feather M0 - Lipo Voltage reading - Bug in tutorial ?

Posted: Sat Apr 30, 2016 3:23 am
by adafruit_support_mike
The SAMD21's ADC has configurable resolution. The default is 10-bit to make it compatible with all the other Arduino code that already exists.

You can change it with the analogReadResolution() function: http://www.arduino.cc/en/Reference/AnalogReadResolution