Batt meter

Hello, Is there any way to change this code to get its battery information from analog pin 15?
Thanks for any help.
Attached is the code

Code.rtf (36.6 KB)

JOS_TFTLCD_Shield.ino (34.6 KB)

You need to post the information for the hardware (shield) you are using as a datasheet or vendor link.
Also, I have no idea what you mean by "battery information".

You might mention which arduino you are using as well.

Here is a video that shows it 2:40 into the vid. (http://vimeo.com/27151448). There is voltage readout and if you look in the upper right-hand corner of the screen there is a battery bar. They get their information off of the 5V rail. I would like to change this to a voltage I put in on analog 15.

As far as the hardware I’m using a mega with Adafruit 2.8" TFT Touch v2 I have both the shield and the breakout but I don’t think the LCD matters much for this question.

attachments in my first post
Code.rtf works with breakout
JOS_TFTLCD_Shield.ino works with shield

Thanks for the help.

Not without a schematic of the shield that is measuring the batt voltage.

If you want the integer value couldn’t you get it from here ?

  battv = getBandgap(); // read the voltage getBandgap <======
  itoa (battv, voltage, 10); // converts integer to string
  decimate(voltage, 2); // converts string to decimal point
         tft.setTextColor(YELLOW);
         tft.setTextSize(2);
         tft.setCursor(25, 213);

The batt voltage is being measured in the code i thank any way.

I try changing.

 battv = getBandgap(); // read the voltage getBandgap

To

battv = analogRead(A15); ; // read the voltage getBandgap

That got me and input from Pin 15 put not the voltage jut the raw data.
here is the schematic of the breakout LCD
https://learn.adafruit.com/assets/15463

Not without a schematic of the shield that is measuring the batt voltage.

Now that I think about it if put the Y+ from the Breakout LCD on a pin other than A0-A7 the battery bar and the voltage display reads 0.
I'm sorry for any confusion please bear with me I am pretty new to this.

 int getBandgap(void) // Returns actual value of Vcc (x 100) 415 = 4.15v
{
        
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
     // For mega boards
     const long InternalReferenceVoltage = 1115L;  // Adjust this value to your boards specific internal BG voltage x1000
        // REFS1 REFS0          --> 0 1, AVcc internal ref. -Selects AVcc reference
        // MUX4 MUX3 MUX2 MUX1 MUX0  --> 11110 1.1V (VBG)         -Selects channel 30, bandgap voltage, to measure
     ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR)| (0<<MUX5) | (1<<MUX4) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);
  
#else
     // For 168/328 boards
     const long InternalReferenceVoltage = 1056L;  // Adjust this value to your boards specific internal BG voltage x1000
        // REFS1 REFS0          --> 0 1, AVcc internal ref. -Selects AVcc external reference
        // MUX3 MUX2 MUX1 MUX0  --> 1110 1.1V (VBG)         -Selects channel 14, bandgap voltage, to measure
     ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);
       
#endif
     delay(50);  // Let mux settle a little to get a more stable A/D conversion
        // Start a conversion  
     ADCSRA |= _BV( ADSC );
        // Wait for it to complete
     while( ( (ADCSRA & (1<<ADSC)) != 0 ) );
        // Scale the value
     int results = (((InternalReferenceVoltage * 1023L) / ADC) + 5L) / 10L; // calculates for straight line value 
     return results;
     }

This is the function “getBandgap” that gets the voltage from the processor’s internal reference voltage.

Ok can I change that to get it's voltage from analog 15?

It's not a physical voltage available outside the processor because it is never the voltage you see displayed. That value is calculated by the processor from it's internal reference voltage which is not available external to the processor. Look at the code. You can see it does not read any analogpins. It's all done INTERNALLY. You can modify the code to get the integer value from INSIDE that function.

They get their information off of the 5V rail. I would like to change this to a voltage I put in on analog 15.

There is a problem doing it that way. The 5V rail is the Vcc and Avcc voltage that powers the AVR chip. As default analogRead0 function uses the Avcc as it's reference voltage when performing the analog input measurement. So that means if the battery is running at +5vdc then performing a analogRead(15) will result in a returned digital value of 1023 counts. So later when the battery drops to say 4.5vdc and then again performing a analogRead(15) will result in a returned value of 1023 counts, see the problem. That's why that code uses a trick method of measuring the internal band-gap voltage reference and back computing what the Avcc must be which is equal to the battery voltage at that time.

Bottom line it's not easy to measure the voltage that is powering the chip via it's analog input pins, unless you use some form of fixed voltage reference that doesn't change even as the Vcc changes.

There is a problem doing it that way. The 5V rail is the Vcc and Avcc voltage that powers the AVR chip. As default analogRead0 function uses the Avcc as it's reference voltage when performing the analog input measurement. So that means if the battery is running at +5vdc then performing a analogRead(15) will result in a returned digital value of 1023 counts. So later when the battery drops to say 4.5vdc and then again performing a analogRead(15) will result in a returned value of 1023 counts, see the problem. That's why that code uses a trick method of measuring the internal band-gap voltage reference and back computing what the Avcc must be which is equal to the battery voltage at that time.

Yes i do see the problem.

I want to measure the voltage of a batt but the power for the mega is from a power supply.

CATCH-22

It seems like there shooed be a way to do it but i guess I'm just SOL.

NJ9A:

There is a problem doing it that way. The 5V rail is the Vcc and Avcc voltage that powers the AVR chip. As default analogRead0 function uses the Avcc as it's reference voltage when performing the analog input measurement. So that means if the battery is running at +5vdc then performing a analogRead(15) will result in a returned digital value of 1023 counts. So later when the battery drops to say 4.5vdc and then again performing a analogRead(15) will result in a returned value of 1023 counts, see the problem. That's why that code uses a trick method of measuring the internal band-gap voltage reference and back computing what the Avcc must be which is equal to the battery voltage at that time.

Yes i do see the problem.

I want to measure the voltage of a batt but the power for the mega is from a power supply.

Gotcha, Assuming your mega is being powered from a regulated +5vdc then to read a battery that can vary from a max of +5vdc down to some lower value you just need to scale the raw value you get from the analogRead(15) using the map function.

So lets see what we can do.

raw = analogRead(15);
externalMillivolts = map(raw, 0,1023,0,5000);

// externalMilliwatts will hold a value that can range from 0 to 5000.

Note I suggest scaling to millivolts (0-5000mv = 0-5v) as you would have to use some software voodoo to convert to float values and not lose resolution.

Big caution, if the battery ever gets higher in voltage then the voltage powering the mega's AVR chip the analog pin will be damaged.

Lefty

It seems like there shooed be a way to do it but i guess I'm just SOL.

Why do you need an analog voltage ? You can see the "results"

 int results = (((InternalReferenceVoltage * 1023L) / ADC) + 5L) / 10L; // calculates for straight line value 
     return results;

Why not just use the integer value:

 int results

raschemmel:

It seems like there shooed be a way to do it but i guess I'm just SOL.

Why do you need an analog voltage ? You can see the "results"

 int results = (((InternalReferenceVoltage * 1023L) / ADC) + 5L) / 10L; // calculates for straight line value 

return results;




Why not just use the integer value:


int results

He want to measure the voltage of an independent battery that is not powering the mega.

I want to measure the voltage of a battery, but the power for the mega is from a power supply.

Sorry , I didn't realize he meant ANOTHER battery, not the one powering the mega.
If it is not the battery powering the mega and the grounds are shared , why doesn't it read the voltage on analog A15?
Is it the battery greater than 5V?

raschemmel:
Sorry , I didn't realize he meant ANOTHER battery, not the one powering the mega.
If it is not the battery powering the mega and the grounds are shared , why doesn't it read the voltage on analog A15?
Is it the battery greater than 5V?

He just didn't know how to convert raw ADC values to an actual battery voltage scale, that's all.

He want to measure the voltage of an independent battery that is not powering the mega.

Yes that is correct. I do not see much point in measuring the voltage that is powering the mega it will be powered from a fixed voltage from a power supply. So I would like to use that voltage display and battery icon to measure the voltage of a backup battery that I can connect to a free analog pin.