I am currently playing around with the battery level but have difficulties to interpret what is happing.
I am using an analog read resolution of 12 bytes:
analogReadResolution(12);
and read the battery level with:
int sensorValue = analogRead(ADC_BATTERY);
What I get is e.g. 3380 which I assume are 3.38 V....the working voltage(?). So, this does not indicate any battery level, right? Only if the battery level is on the final percentage this value would trop shortly before dying. But I could not see if it is 90 %, 80 % or 40 % charged.
Any suggestions or hints? I feel like I am doing something wrong here.
And what is actually the minimum Voltage for the SARA chip to work? It seems like at some point the MKR is still operating but can't establish a connection to the web. Any experience?
No, That is not how an ADC works. The ADC takes the reference voltage and divides into 4096 steps. (reference voltage Γ· 4096 = voltage value of 1 bit)
To convert a analog reading to voltage you must use this formula: float myVoltage = analogReading * (reference voltage/4096.0)
note that adding the ".0" to 4096 will force this equation to use floating point math.
I tried out the suggested tutorial and used a naked MKR NB1500 Board, with the 1800 mAh battery and copy basted the code from the tutorial. I only changed the battery size. The resistor where the same according to the schematic. My output however is not as expected:
The ADC on PB09 reads a value of 3333.00 which is equivialent to 2.69V. This means the battery voltage is 3.26V. Which is equivalent to a charge level of -34%
So either the tutorial is not working, or I have a an error in thinking. Any idea?
The USB connection should still make thinkgs work.
This is so confusing...I followed the tutorial. You think it is wrong?
Stupid question, but is 2.69V not a little low? I thought the operation Voltage is 3.3V so I assumed the input Voltage should be at least this.
Yes it's wrong.
The schematics show that R1=330K and R2=1.2M NOT 1.0M as given in the tutorial
So the battery voltage is actually 2.69V x 1.275 = 3.43V
2.69V is scaled down battery voltage read by the ADC. You need to multiply that by 1.275 to get the actual battey voltage
Okay, things are getting clearer now. You are totally right @anon95813201.
Rounding things up: The battery voltage ranges from 3.2V to 4.2V leading to a voltage of 2.5V to 3.29V on the boad, right? So, how can the board then provide 3.3V operating voltage if it is only supplied with e.g. 2.8V?
No. That is only the voltage going into the ADC pin so that you can read the battery voltage. It has nothing to do with the 3.3V used for the board power.
okay...even though all this makes sense, reallity hits with another hurdle. Which is, the battery seems not to charge. I have attached a USB cable to the MKR NB 1500 using this code:
/*
Read battery voltage on MKR WIFI 1010 and log values to the serial monitor
ββββββββ VBatt
β
/
\ 330k
ββββββββββββββ /
β SAMD21 β \
β β β
β PB09 ββββββ¬βββββββββ€
β β β β
β β β 100n /
β β βββ΄βββ \ 1M
β β βββ¬βββ /
β β | \
β β β |
β β β β
βββββββ¬βββββββ | |
β | |
βΌ βΌ βΌ
Author: Ali Jahangiri & Karl Soderby
Last Edit: 9th August 2022
*/
// Include the library for the BQ24195 IC
#include <BQ24195.h>
//define variables
float rawADC; //unprocessed ADC value
float voltADC; //ADC converted into voltage
float voltBat; //calculated voltage on battery
//define the resistor values in the voltage divider
int R1 = 330000; // resistor between battery terminal and SAMD pin PB09
int R2 = 1200000; // resistor between SAMD pin PB09 and ground
int max_Source_voltage; // upper source voltage for the battery
// define voltage at which battery is full/empty
float batteryFullVoltage = 4.2; //upper voltage limit for battery
float batteryEmptyVoltage = 3.3; //lower voltage limit for battery
float batteryCapacity =1.8; //set battery capacity in Ah
void setup() {
Serial.begin(9600); // start Serial port with a baudrate of 9600bps
analogReference(AR_DEFAULT); // the upper value of ADC is set to 3.3V
analogReadResolution(12); // this will give us 4096 (2^12) levels on the ADC
//configure BQ24195 PMIC
PMIC.begin(); // start the PMIC I2C connection
PMIC.enableBoostMode(); // boost battery output to 5V
PMIC.setMinimumSystemVoltage(batteryEmptyVoltage); // set the minimum battery output to 3.5V
PMIC.setChargeVoltage(batteryFullVoltage); // set battery voltage at full charge
PMIC.setChargeCurrent(batteryCapacity/2); // set battery current to C/2 in amps
PMIC.enableCharge(); // enable charging of battery
// The formula for calculating the output of a voltage divider is
// Vout = (Vsource x R2)/(R1 + R2)
// If we consider that 3.3V is the maximum that can be applied to Vout then the maximum source voltage is calculated as
max_Source_voltage = (3.3 * (R1 + R2))/R2;
}
void loop()
{
rawADC = analogRead(ADC_BATTERY); //the value obtained directly at the PB09 input pin
voltADC = rawADC * (3.3/4095.0); //convert ADC value to the voltage read at the pin
voltBat = voltADC * (R1 + R2)/R2; //we cannot use map since it requires int inputs/outputs
int new_batt = (voltBat - batteryEmptyVoltage) * (100) / (batteryFullVoltage - batteryEmptyVoltage); //custom float friendly map function
//report information over Serial
Serial.print("The ADC on PB09 reads a value of ");
Serial.print(rawADC);
Serial.print(" which is equivialent to ");
Serial.print(voltADC);
Serial.print("V. This means the battery voltage is ");
Serial.print(voltBat);
Serial.print("V. Which is equivalent to a charge level of ");
Serial.print(new_batt);
Serial.println("%.");
delay(1000*60);
}
And the result looks like this:
14:55:19.517 -> The ADC on PB09 reads a value of 3349.00 which is equivialent to 2.70V. This means the battery voltage is 3.44V. Which is equivalent to a charge level of 15%.
14:55:20.008 -> The ADC on PB09 reads a value of 3365.00 which is equivialent to 2.71V. This means the battery voltage is 3.46V. Which is equivalent to a charge level of 17%.
14:55:20.536 -> The ADC on PB09 reads a value of 3364.00 which is equivialent to 2.71V. This means the battery voltage is 3.46V. Which is equivalent to a charge level of 17%.
14:55:21.036 -> The ADC on PB09 reads a value of 3358.00 which is equivialent to 2.71V. This means the battery voltage is 3.45V. Which is equivalent to a charge level of 16%.
.
.
.
16:34:36.821 -> The ADC on PB09 reads a value of 3358.00 which is equivialent to 2.71V. This means the battery voltage is 3.45V. Which is equivalent to a charge level of 16%.
16:35:36.791 -> The ADC on PB09 reads a value of 3357.00 which is equivialent to 2.71V. This means the battery voltage is 3.45V. Which is equivalent to a charge level of 16%.
16:36:36.792 -> The ADC on PB09 reads a value of 3355.00 which is equivialent to 2.70V. This means the battery voltage is 3.45V. Which is equivalent to a charge level of 16%.
16:37:36.769 -> The ADC on PB09 reads a value of 3353.00 which is equivialent to 2.70V. This means the battery voltage is 3.45V. Which is equivalent to a charge level of 16%.
looks like nothing changed over the course of 1.5 hours, can't be true right?
Update:
I measured the battery voltage using a voltmeter and it showed that the battery us fully charged at ~4.17 V. So the problem is the ADC reading which is not somewhere near the expected max value of 4096. How come?
This should work in theory (when using read resolution of 10), but in my case I get 3.3V on a fully charged battery (measured ~4.17 V with a Voltmeter). Do you see an error in my sketch or anything else I might make wrong?
I am trying to read the Voltage of a 1.8 Ah Battery which is fully charged according to a Voltmeter showing 4.17 V. However using the following code, I would have expected a rawADC reading of ~4096 but it stays somewhere around 3250.
int rawADC;
void setup() {
Serial.begin(9600); // start Serial port with a baudrate of 9600bps
analogReference(AR_DEFAULT); // the upper value of ADC is set to 3.3V
analogReadResolution(12); // this will give us 4096 (2^12) levels on the ADC
}
void loop()
{
rawADC = analogRead(ADC_BATTERY); //the value obtained directly at the PB09 input pin
Serial.println(rawADC);
delay(2000);
}
This is a continuation of : https://forum.arduino.cc/t/battery-level-interpretation/1103521/14 which was resolved but this more detailed erros deserves its own thread.