Voltage regulator output

Hello
This is the circuit diagram.

Serial.begin(9600);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  
  float sensorValue, sum1, voltage;
  sum1 = 0;
  
  for (int i = 0; i <= 9 ; i++)
  {
    sensorValue = analogRead(check1);
    Serial.println(sensorValue);

     voltage= sensorValue * (5.0 / 1024.0);
    
    sum1 =  sum1 + voltage;
  }
  avg = sum1 /10;
  Serial.println(avg);
  
} //END setup

void loop()
{
oled.clearDisplay();
   if (avg >= 3.6)` {
    Serial.println("100%");
    oled.drawBitmap(100, 2, Battery_100 , 26, 26, WHITE); //battery 100%
  }
`
  {
    Serial.println("100%");
    oled.drawBitmap(100, 2, Battery_100 , 26, 26, WHITE); //battery 100%
  }

  else if (avg >= 3.4 && avg < 3.6)
  {
     Serial.println("75%");
    oled.drawBitmap(100, 2, Battery_75 , 26, 26, WHITE); //battery 75%
  }

  else if (avg >= 3.1 && avg < 3.4)
  {
     Serial.println("50%");
    oled.drawBitmap(100, 2, Battery_50 , 26, 26, WHITE); //battery 50%
  }

  else if (avg > 3 && avg < 3.1)
  {
    Serial.println("25%");
    oled.drawBitmap(100, 2, Battery_25 , 26, 26, WHITE); //battery 25%
  }
oled.display();
}

I have defined 4 bitmaps of battery images of different levels which 100%, 75%, 50% and 25% to show in the OLED display.

Having problem with getting this voltage levels in the output.

When I increase the voltage from external supply the voltage at divider is decreasing.Reverse result is getting.

5V does not go into Vin use the 5Volt pin instead.

These,


suck as power sources.

I only use 2 resistors for the Vdivider. R2 as a 10K and the proper R for R1 for the V's to be measured.

Ok.Let me try and check.

Thank you

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.

1 Like

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 :wink: See About the Installation & Troubleshooting category.

1 Like

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

1 Like

Yes, but... you can do better using the compiler for math.

Like this:

const float VCC = 5.0;
const int BATT_100 = 3.6 / VCC * 1024;
...
  int sensorValue, sum1;
  sum1 = 0;
  for (int i = 0; i <= 9 ; i++)
  {
    sensorValue = analogRead(check1);
    Serial.println(sensorValue);
    sum1 =  sum1 + sensorValue;
  }
  avg = sum1 /10;
  Serial.println(avg);
...
   if (avg >= BATT_100) {
    Serial.println("100%");

1 Like

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.

1 Like

You could make your code much nicer and simpler by using a switch/case statement rather than all the nested if's.

Your diagram does not show how you can control the external voltage. Can you explain that please?

If you are applying 9V as shown then the voltage on the analog pin is 9V * ( 10/16.2) = 5.56V which is too high.

Also you need to connect the regulator output to the 5V pin; Vin needs >7V

Can you show the raw analog values you are getting as a function of supply voltage?

1 Like

Thank You for your reply.

This are the results when I apply 9v battery and connection as per circuit diagram (Voltage regulator output at Vin)
11:08:15.325 -> Loop Avg 10 Mins: 5.00
11:08:19.802 -> 1023.00
11:08:19.802 -> 1023.00
11:08:19.802 -> 1023.00
11:08:19.802 -> 1023.00
11:08:19.836 -> 1023.00
11:08:19.836 -> 1023.00
11:08:19.836 -> 1023.00
11:08:19.836 -> 1023.00
11:08:19.870 -> 1023.00
11:08:19.870 -> 1023.00
11:08:19.870 -> SETUP AVG: 5.00
11:08:35.206 -> Loop Avg 10 Mins: 5.00
11:08:45.278 -> Loop Avg 10 Mins: 5.00

I have to use battery but for the testing purpose I am using PSU to change the voltage levels and check what is the output.

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

1 Like

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);
1 Like

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..

1 Like

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%}

1 Like

Thank you.

Problem solved.

Thank you for reply.

Problem solved by removing voltage regulator and some change in voltage divider.

Hi,
So can you please post a schematic to show how your final circuit is constructed?

This will help to close the thread and provide information for anyone who may use this thread to solve their problem.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Can you help me.I have a query.

After this solutions I get all the battery voltage levels.

I want 50 hr battery backup in the circuit.So when I use Lithium cells It is getting discharge in 3 hours.

This is the link of Li cell battery:

https://robokits.co.in/batteries-chargers/skycell-li-ion-battery/7.4v-skycell-li-ion-batteries/li-ion-battery-7.4v-2200mah-2c-wth-inbuilt-charger-protection

which battery should be compatible which gives backup and this voltage levels also.?

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.