Arduino nano not reading 5v attery level

Counter wiring

my battery fully charged voltage (4.5v to 5v)
my Arduino nano when powered from us from pc , reading well battery level. but giving A0=1023 , when run on battery by vin .
is it possible reading 5v battery voltage by nano with oled display??
or
Do i need to use battery great than 6v???

My wiring

My code

 
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <EEPROM.h>

#define OLED_ADDR   0x3C     //     SDA = A4   ,  SCL =A5

  Adafruit_SSD1306 display(-1);                             
const int  plus = A1;
const int  reset = A2;
const int  save = A3;

int P1 = 0;
int Last_P1=0;
int P2=0;
int Last_P2=0;
int P3=0;
int Last_P3=0;
int count = 0;
int reading = 0; 

long v = 0;
long Last_v = 1025;

void setup() {

 pinMode(plus, INPUT_PULLUP);
  pinMode(reset, INPUT_PULLUP);
   pinMode(save, INPUT_PULLUP);
    pinMode(2, INPUT_PULLUP);

    Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
Serial.println(analogRead(A0));

}
void loop () {

     P1 = digitalRead(plus);
     P2 = digitalRead(reset);
     P3 = digitalRead(save);

     v =  analogRead(A0);      
  
     if ((v != Last_v) && (v < Last_v))
     {
      Last_v = v;
     }

       if (P1 != Last_P1) {
    if (P1 == LOW) 
    {  count++;     screen  ();  } 
    else
    {   display.clearDisplay();   display.display();  }
    Last_P1 = P1;
  }

//  --------------------------------------------------------------------------
         if (P2 != Last_P2) {
   if (P2 == LOW) {   count--;    screen  ();  } 
    else {
        display.clearDisplay();    display.display();  }
     Last_P2 = P2;
  }
  //---------------------------------------------------------------------------
         if (P3 != Last_P3) {
  if (P3 == LOW) {    count=0;   screen  ();   } 
    else {   display.clearDisplay();   display.display();  }
     Last_P3 = P3;
  }   // ------------------------------------------------------------   
}  
    
       
       void screen  ()   {    
  display.clearDisplay();
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(1, 1);
  display.print(count);
  display.setTextSize(1);
  display.setCursor(104, 1);
  display.print(Last_v);
   if (Last_v<560)
 { display.setTextSize(1);
  display.setCursor(60, 20);
  display.print("Batt LOW"); }
  display.display(); 

 }



 

The A/D converter by default uses the input voltage as the reference . So in your case because of the diode Vcc is about .6 of a bolt below the battery voltage , so it always sees the battery a at a higher voltage than the supply - hence 1023.

You need to look at using the internal voltage reference and use a resistor divider on that analog input .

Google internal reference and voltage divider . This reference is very stable , but you will need to calibrate this voltmeter

BtW you risk damage having a higher voltage on the any pin than the supply voltage

2 Likes

Why are you connecting something to "Vin"? :astonished:

Yes , just noticed that !

For Using Vin you need something like 7v or greater .
5v pin must be 5v !

Rest of my reply still stands !

And as a generality, it is a Really Bad Idea! :roll_eyes:

To get a rise out of Paul :thinking:

1 Like

Hey,
For the rest of us - Why is connecting "something" to Vin bad? Not clear on that. Thousands(millions, maybe?) run their Arduino Nanos on 9 to 12V, I've never heard anyone saying "bad, bad, bad".
Just curious

It’s not bad , they did put that pin there after all ; just that the on board regulator is small and can’t dissipate much power- so if you connect anything to the output lines of the Arduino you need to be careful about power dissipated in that regulator and do the sums .

What I figured. Yea, if you know much at all you know the onboard reg is sadly lacking. Leads many newbs into trouble. "What, I can't power six servos off the 5V out on my Nano? But, But, But! My project's ruined!" But that's a whole lot different from "Why are you connecting to..." which implies it's a dumb move. No, you just need to know what the heck you're doing.
Sheesh.

And therein is the problem. :roll_eyes:

The default Arduino documentation and copycat "tutorials" suggest that operation from 12 V is in some way desirable. This is a completely outdated concept as I frequently go on to explain. It had some relevance in the distant past when two things applied.

Firstly that the commonly available power supply for the beginner was an unregulated 9 V plug pack or "wall wart", possibly reclaimed from a modem or network hub. The little problem with this is that such supplies actually rose to a substantially higher voltage when lightly loaded and it does appear as we often see here, that the regulators used on Nanos may not handle over 12 V.

The other thing is that the Arduino was basically a toy - an experimental system to teach programming - "coding" - and generate pretty patterns on a few LEDs limited to 20 mA each.

So under these limited circumstances, the on-board regulator arguably is usable. But to suggest that this is the most appropriate way to operate it, is grossly misleading and should have been removed over time in the documentation. But it has not.

"Feature creep" will bite the novice user - whom the Arduino is still marketed to. The first few projects flashing LEDs will work until the second relay module is added or of course, a servo. But it all worked before - what has gone wrong? :astonished:

The point is - operating from "Vin" via the regulator is really the exception rather than the rule and this is what should be reflected in the documentation.

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