I am using a Adafruit Feather M0 with RFM95 LoRa module connected to a 3.7V, 1200mAh LiPo battery. I am trying to figure out a way to print out the voltage and battery level of the battery using a sketch I wrote after reading some blogs but I think I am doing something wrong since the value of battery charge is not changing.
According to the documentation, GPIO 9 (analog input is connected to a voltage divider for the lipoly battery ).
But A0 - This pin is analog inputA0 but is also an analog output due to having a DAC (digital-to-analog converter). You can set the raw voltage to anything from 0 to 3.3V, unlike PWM outputs this is a true analog output.
So I am a not sure which pin I should be using. But Below is the code I am using.
const float MAX_BATTERY_VOLTAGE = 4.2; // Max LiPoly voltage of a 3.7 battery is 4.2
void setup() {
Serial.begin(9600);
}
void loop() {
int rawValue = analogRead(9);
float voltageLevel = (rawValue / 4095.0) * 2 * 1.1 * 3.3; // calculate voltage level
float batteryFraction = voltageLevel / MAX_BATTERY_VOLTAGE;
Serial.println((String) "Raw:" + rawValue + " Voltage:" + voltageLevel + "V Percent: " + (batteryFraction * 100) + "%");
delay(1000);
}
Why can't You use one pin for analogRead and another for the output? I guess that switching from output to analog input there might be some settling time to pay attention to.
Please explain the significance of this. Are you concerned that you have made a mistake and used the pin incorrectly, or do you need DAC functionality for something?
#9 - GPIO #9, also analog input A7. This analog input is connected to a voltage divider for the lipoly battery so be aware that this pin naturally 'sits' at around 2VDC due to the resistor divider