43oh

# how read the battery voltage

## Recommended Posts

hi,

I've a msp430g2253 board powered by a battery. I'd like to read the voltage of the battery.

I've found a function online (http://blog.elevendroids.com/2013/06/code-recipe-reading-msp430-power-supply-voltage-level/).

```uint16_t Msp430_GetSupplyVoltage(void)
{
uint16_t raw_value;
// first attempt - measure Vcc/2 with 1.5V reference (Vcc < 3V )
// start conversion and wait for it
// stop conversion and turn off ADC
// check for overflow
if (raw_value == 0x3ff) {
// switch range - use 2.5V reference (Vcc >= 3V)
// start conversion and wait for it
// end conversion and turn off ADC
// convert value to mV
return ((uint32_t)raw_value * 5000) / 1024;
} else
return ((uint32_t)raw_value * 3000) / 1024;
}
```

It works but the first time I call the function it uses the 1.5V reference (Vcc < 3V ) and the second time it uses 2.5V reference (Vcc >= 3V).

The real voltage is 3,12V

What is wrong?

Regards

ilpaso

##### Share on other sites

From the comments, it seems you should use a voltage divider (for example maybe 2 100K resistors), so you have VCC/2 on your analog input.

The first try is using the 1.5V internal reference, so it's good for voltage below 3V.

If the conversion returns 0x3FF, it means that VCC is >= 3V, so the software switches to the 2.5V reference that allows up to 5V for VCC (but of course you can't go higher than 4V on these chips).

##### Share on other sites

Shouldn't need a voltage divider, there is an on-demand one built in to the ADC (so power isn't wasted when you're not sampling). Will have to try this code later to see what's wrong.

Sent from my Galaxy Note II with Tapatalk 4

##### Share on other sites

Try getting rid of ADC10SR. You are running the ADC pretty fast and short so it should need the higher buffer strength available when ADC10SR=0 I think.

Sent from my Galaxy Note II with Tapatalk 4

##### Share on other sites

Hi,

Voltage without divider? Is there a way to do the same in energia?

Thanks

##### Share on other sites

It works but the first time I call the function it uses the 1.5V reference (Vcc < 3V ) and the second time it uses 2.5V reference (Vcc >= 3V).

The real voltage is 3,12V

What is wrong?

So are you asking why it's using the  1.5V reference and then the 2.5V one? But in fact it's working ok?

##### Share on other sites

Voltage without divider? Is there a way to do the same in energia?

Maybe I'll have some time to try it at lunchtime.

Maybe there's already a constant for this in energia.

##### Share on other sites

Thank you for the replies.

It works in Energia but the first time the function uses the 1.5V reference cycle and the second it uses the 2.5V reference cycle and so on.

I'll try  with analogRead (11) this evening.

##### Share on other sites

Ok, then as I explained in my first response, it first tries with 1.5V reference to get a better reading, if it overflows it switches to 2.5V reference.

##### Share on other sites

It seems to be working, the following sketch gives me 3.5V with USB VCC

```void setup()
{
Serial.begin(9600); // msp430g2231 must use 4800

Serial.println("VCC value:");
analogReference(INTERNAL2V5);
}

void loop()
{
long data = map(analogRead(11), 0, 1023, 0, 500);
Serial.print (data/100);
Serial.print (".");
Serial.println (data%100); // this is wrong if the tens are 0
delay(1000);
}
```
##### Share on other sites

Based on the MSP430G2553 datasheet, the 1.5V ref requires Vcc>= 2.2V and the 2.5V ref requires Vcc >= 2.9V to operate.

If your Vcc is below 2.9V you can't use 2.5V ref. So this code first uses the 1.5V ref to check Vcc.

##### Share on other sites

Makes sense, it'd be hard to generate a 2.5V reference with a 2.4V VCC.

##### Share on other sites

Makes sense, it'd be hard to generate a 2.5V reference with a 2.4V VCC.

Not if there a voltage regulator embedded?

And why pin 11?

##### Share on other sites

Not if there a voltage regulator embedded?

And why pin 11?

Voltage regulators can only lower voltages...

For analogRead the value isn't really the Energia pin# but rather the ADC channel (INCH_xx constant), 11 just happens to be an internal "(Vcc-Vss)/2" generated channel used for measuring the MSP430's Vcc voltage.

Sent from my Galaxy Note II with Tapatalk 4

##### Share on other sites

Well it's possible to generate higher voltages with a dc/dc boost converter for example, I just don't know if it's even possible to embed something like that in a chip without external components and keeping the power usage and costs low.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
• Blog

• #### Activity

×
• Create New...