You didn't say, but the project looks like a battery meter. In this limited case, you do not need any external regulator. The Nano has one on board, you can connect your battery directly to Vin.
You are aware that the minimum voltage for Vin on a Nano is above 5V?
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project See About the Installation & Troubleshooting category.
Bear in mind your 5v may not be exactly 5v and affects the accuracy of your battery tester .
See what voltage you actually have on the 5v pin and use that . You may also need to adjust for resistor tolerance
While I’m here …
Look at the internal voltage reference for better accuracy.
You could also skip a lot of the maths . If you know the raw A/D value for 9v you can use your calculator to work out what the raw A/D values would be for for a low or dud battery .
That way you can just work in integers and have a simplier more efficient program ( possibly more accurate too).
You can then simply have a button , press that with a known voltage ( say 10v) , then that A/D value will be equal to 10volts , and scale off that
Redundant && clause. In the 'else if' statement you have already checked for avg >= 3.6 in the previous 'if' statement. There is no need to check it twice.
1023 is at the top of the A/D range , or maybe over it .
As a new battery might be a bit over 9v I would adjust your voltage divider to give say 1023 at 10volts
First of all: don't map ADC values inside the reading loop (you'll lose accuracy). Add the values, then do the average outside the loop.
Second, since you are using a loop for an average value, use a delay after each ADC read (even 1ms should do it).
And third (many already told you here): your voltage divider is not suitable for your input voltages. 6.2K and 10K gives you voltages above 5V when using a 9V (or even more with a fresh battery) at input.
Better use 20K (instead of your 6.2K) + 10K and map the input values to voltages from 0 to 15V.
Sample code:
#define ADC_READS 5
u16 adc_sum = 0, val, mV;
for (int i = 0; i < ADC_READS; i++) {
adc_sum += analogRead(adc_pin);
delay(1);
}
val = adc_sum / ADC_READS;
mV = map(val, 0, 1024, 0, 15000);
I would not use an 7805, because that ancient regulator has a high (~2volt) dropout voltage. When the battery reaches <7volt, the 5volt output will start to drop too, making the reference you compare battery voltage against also dicey.
The 5volt regulator that is already on the Nano has a much better <1volt dropout, so connect the 9volt battery directly to the V-in pin.
I would dial down to 1volt with the voltage divider (10k:8k2), and switch to the more stable 1.1volt Aref in setup(). Then the 9volt battery can even drop to <5volt without affecting battery measurement and working of the Nano.
OP wanted battery percent, not voltage, so why not map A/D value directly to percent. byte percent = map(analogRead(A0), 400, 1000, 0, 100);
Leo..
lots of great comments here @dhruvi_s - let me pick some for you:
1: use integer arithmetic.
2: see Leo's response post #13 above: use the internal reference and a divider of 180k:22k which for 9V in will give you 1V to the analog input.
except: OP only wants indication of 4 battery levels. And remaining capacity doesnt map linearly to voltage.
So just do the comparison directly from the averaged raw readings:
eg int val75 = 850; val50 = 750; val25 = 600;
if (val >= val75){show 100%}
else if (val >=50){show 75%}
else if( val >= val25){show 25%)
else {show 0%}
Suppose the cell counts as "discharged" when only 25% of its capacity is left.
So you have 2200mAh * 0.75 = 1650mAH usable.
You say this lasts 3h
so your current drain is 550mA.
What is using that current? You need to measure the current with a meter.