Project

General

Profile

Resolved: Measuring battery voltage?

Added by zooto68 over 4 years ago

I see there is some code in some of the Jeelib blog posts to measure voltage. However, I am presuming these are powered directly by a battery. I’ve got my circuit powered by a 3.7v lithium battery which is put through an MCP1702 voltage regulator to bring it down to 3.3v

So, considering no matter what the battery voltage is, the chip is going to getting 3.3v all the time, how do i go about measuring the battery voltage/

I want to report that back in my data packets from my nodes so I can generate alerts when batteries need replacing.

Thanks,

Mike


Replies (7)

RE: Measuring battery voltage? - Added by martynj over 4 years ago

Yes, this series of WebLog posts cover this topic with circuits and code examples.
Since the battery voltage is expected to be greater than the Vcc of 3.3 V, you need to divide it down to get within the input range of the ADC.
A different approach is to measure Vcc without any divider - yes, it is 3.3 V while the LDO is in control, but under load will dip below the set point when the battery voltage has faded below ~ 3.5 V

RE: Measuring battery voltage? - Added by zooto68 over 4 years ago

My point is more HOW do you measure it? The circuit has power going from the MCP1702 not from the battery so how can the code measure the battery voltage?

RE: Measuring battery voltage? - Added by JohnO over 4 years ago

What do see as missing in the link martynj provided? As I see it you have a voltage divider to drop the 3v7 from the lipo to a voltage around 1v7 the middle of the analogue range. Connect this voltage to a spare analogue pin and analogue read out the value in your sketch. The value will drop as the lipo voltage drops.
Martyn’s other suggestion, measuring the voltage internally is covered in the area of his link but specifically here
As I understand the proposal, when the lipo gets close to exhaustion the voltage will drop and at some point the voltage regulator won’t be able to maintain Vcc at 3v3. This will be detected by comparing this incoming voltage to the internal 1v1 reference voltage as per previous link.

RE: Measuring battery voltage? - Added by zooto68 over 4 years ago

OK maybe i’m being really stupid here but where in the code do you choose what analog pin to use? At present the code appears to be taking an internal voltage rather than one from an analog pin:

 long readVcc() {
   bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
   long result;
   // Read 1.1V reference against Vcc
   #if defined(__AVR_ATtiny84__) 
    ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84
   #else
    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);  // For ATmega328
   #endif 
   delay(2); // Wait for Vref to settle
   ADCSRA |= _BV(ADSC); // Convert
   while (bit_is_set(ADCSRA,ADSC));
   result = ADCL;
   result |= ADCH<<8;
   result = 1126400L / result; // Back-calculate Vcc in mV
   ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
   return result;
} 

RE: Measuring battery voltage? - Added by JohnO over 4 years ago

The code you have shown is the version that when the lipo gets close to exhaustion the voltage will drop and at some point the voltage regulator won’t be able to maintain Vcc at 3v3.
This will be detected by comparing the processor incoming voltage (after the LDO action) to the internal 1v1 reference voltage.

With this approach no separate connection to the LiPo is required. Both the chip Vcc and the internal 1v1 reference are accessible by appropriate setting of the ADC input matrix..

RE: Measuring battery voltage? - Added by zooto68 over 4 years ago

OK but that is not what I want. I want to measure the voltage of the battery at all times. So what do I need to do for that please?

RE: Measuring battery voltage? - Added by JohnO over 4 years ago

Doesn’t that brings us full circle back to Martyn’s first post and my echo of it.

    (1-7/7)