Hi, there i am working on a project in which my system is connected to 3.7v~4.2V lithium battery, I have to read constant battery voltage irrespective of the current battery voltage and to do that I am using internal voltage reference (1.1V). My issue is that before adding sensor code my program was reading constant voltage irrespective of any battery level but the moment I add sensor code to it, the battery voltage readings fluctuate.
float val;
float voltage;
void setup(){
Serial.begin(9600);
pinMode (led,OUTPUT);
pinMode (A0, INPUT);
pinMode (A1, INPUT)
}
void loop() {
printVolts();
//REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
ADMUX |= B11000000;
//We read A1 (MUX0)
ADMUX |= B00000001;
// Start AD conversion
ADCSRA |= B11000000;
// Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));
val = ADCL | (ADCH << 8);
val = val * 5.7; //Multiply by the inverse of the divider
Serial.print("val: ");
Serial.print(val);
}
void printVolts()
{
int sensorValue = analogRead(A0);
voltage = sensorValue * (val / 1023.00) ;
delay(1000);
Serial.println("voltage: ");
Serial.println(voltage);
}
Please let me know what is the issue with the code, the value 5.7 in the code is the voltage divider resistors value R1=1k, R2=4.7K. The reason behind using these resistor is to reduce the battery voltage level below AREF voltage which is 1.1v respectively.
@polish_girl
Creating a multiple accounts in order to bypass the blocking there is a violation of forum rules.
Your question is absolutely meaningless, as you were already pointed out in your previous thread.
You cannot expect the battery voltage to be constant as the battery discharges. It does not depend on how you measure it - with using an external AREF or through an internal one.
@polish_girl In order to measure battery voltage you do not need a voltage divider at all.
You wrote that you read a bunch of links on this topic - but did not understand the main idea. It is necessary not to measure the battery voltage relative to the internal reference 1.1v, but vice versa - to measure the internal reference relative to the battery voltage, using the default reference.
The code for this is in hundreds of places on the Internet.