Connecting 9 Volt battery and reading internal voltage.

I have down loaded a sketch to make a simple battery voltage indicator using LEDs.
The LEDs light correctly and the serial port gives expected readings when USB is connected but when I connect a 9V ( checked at supplying 9 volt with external meter) battery via the 2.1 mm socket the LEDs did not light. I used an external V meter to check across the 5V and GRD pin and the mav voltage a got was about 3.3 V. I also got the same reading form the 3.3 V pin. To double checked this I modifed the sketch to turn the LEDpins HIGH at lower voltages and the system worked.
Is there any reason why I can only get 3.3 V output when using the 9V batterry through the 2.1mm socket?

I am only a novice so I would aprreciate an answer even if is something I already should have worked out.
G
Original Code
**
Battery indicator for Magbot.
*/

void setup() {

// Open serial monitor at 9600 baud to see debugging messages.
Serial.begin(9600);

// Battery indicator leds
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);

}

void loop() {

batteryLevel();
delay(1000);

}

/********************************************************************************
Battery functions
********************************************************************************/

void batteryLevel()
{
// Check battery level
long vcc = readVcc();

Serial.print("Battery: ");
Serial.println(vcc);

if (vcc > 3200) {
digitalWrite(A0, HIGH);
} else {
digitalWrite(A0, LOW);
}

if (vcc > 1500) {
digitalWrite(A1, HIGH);
} else {
digitalWrite(A1, LOW);
}

if (vcc > 1000) {
digitalWrite(A2, HIGH);
} else {
digitalWrite(A2, LOW);
}

}

/**

  • Read the internal voltage.
    */
    long readVcc()
    {
    // Read 1.1V reference against AVcc
    // set the reference to Vcc and the measurement to the internal 1.1V reference
    #if defined(AVR_ATmega32U4) || defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
    ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
    #elif defined (AVR_ATtiny24) || defined(AVR_ATtiny44) || defined(AVR_ATtiny84)
    ADMUX = _BV(MUX5) | _BV(MUX0);
    #elif defined (AVR_ATtiny25) || defined(AVR_ATtiny45) || defined(AVR_ATtiny85)
    ADMUX = _BV(MUX3) | _BV(MUX2);
    #else
    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
    #endif

delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring

uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both

long result = (high<<8) | low;

result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.110231000
return result; // Vcc in millivolts
}

Hi and welcome.

Please edit that post and put code tags around your sketch. It is the <> icon. Your sketch will then look like this.

What does your meter read the battery voltage as when it is connected up? You should be able to get the probes to touch the studs even with the snap-on connector in place.

I suspect you are using a pp9 battery and these really struggle to provide enough current to run the arduino and leds. It may read 9V when not connected to the arduino but this is when no current is flowing.

Paul

Thanks for your response.
I tested the battery whist in use and got a reading of 8.5 V.
G

I think this program reads VCC. The supply of the micro.
If you measure less than 5volt on the 5volt pin, then replace the battery.
IMHO, 9volt batteries should not be used to power an Arduino.
They can't supply the required current for long.

With that program I get 5258 (5.258volt) on an UNO.
USB supply.
You can calibrate the program by changing this number slightly: 1125300
Leo..