Author Topic: How do I read battery voltage?  (Read 20808 times)

nfriedly

  • Newbie
  • *
  • Posts: 7
How do I read battery voltage?
« on: March 28, 2016, 02:50:07 pm »
I just got my Oak the other day and I also got a OLED shield and a battery shield.

One of the bullet points for the Oak is "Ability to read the voltage of its power source (for battery monitoring)"

Since there's only one analog pin (that I'm aware of, anyways), I tried reading A0. But it always gives me 4, which seems low (although my code could be wrong too... I'll attach it just in case.)

Anyone have any better info on this?

defragster

  • Sr. Member
  • ****
  • Posts: 467
Re: How do I read battery voltage?
« Reply #1 on: March 28, 2016, 03:34:57 pm »
When I saw it work on generic ESP8266 it worked with this code under ESP8266 version 2 where OAK is - it broke on version 2.1 it seems:

Must be at the top of the sketch:: ADC_MODE(ADC_VCC); // to use getVcc

and then ::   uint32_t getVcc = ESP.getVcc();

Does this work at all on OAK? Note the voltage read high from what I saw.

nfriedly

  • Newbie
  • *
  • Posts: 7
Re: How do I read battery voltage?
« Reply #2 on: March 28, 2016, 03:43:10 pm »
That does something, but it doesn't appear to work:

Code: [Select]
/Users/nfriedly/Documents/Arduino/OLED_read_battery/OLED_read_battery.ino: In function 'int __get_adc_mode()':
OLED_read_battery:11: error: previous declaration of 'int __get_adc_mode()' with 'C++' linkage
 ADC_MODE(ADC_VCC); // to use getVcc
     ^
In file included from /Users/nfriedly/Library/Arduino15/packages/digistump/hardware/oak/1.0.1/cores/oak/Arduino.h:251:0,
                 from /var/folders/95/_6btw8d94sq2b91k8j794zpm0000gn/T/builde10e599308085353bc12fcc5a12ef268.tmp/sketch/OLED_read_battery.ino.cpp:1:
/Users/nfriedly/Library/Arduino15/packages/digistump/hardware/oak/1.0.1/cores/oak/Esp.h:74:58: error: conflicts with new declaration with 'C' linkage
 #define ADC_MODE(mode) extern "C" int __get_adc_mode(void) { return (int) (mode); }
                                                          ^
/Users/nfriedly/Documents/Arduino/OLED_read_battery/OLED_read_battery.ino:11:1: note: in expansion of macro 'ADC_MODE'
 ADC_MODE(ADC_VCC); // to use getVcc
 ^

Update: If I skip the ADC_MODE line and just call ESP.getVcc(), though, that compiles and reads 65535... which also doesn't sound correct (I've been using this battery on and off for a day or two, so it shouldn't be up near 100%.)
« Last Edit: March 28, 2016, 03:58:35 pm by nfriedly »

defragster

  • Sr. Member
  • ****
  • Posts: 467
Re: How do I read battery voltage?
« Reply #3 on: March 28, 2016, 04:05:09 pm »
There may be an include that was needed to support those 'ESP8266' items - do some searching?

ADC_MODE(ADC_VCC); // to use getVcc
uint32_t getVcc = ESP.getVcc();

nfriedly

  • Newbie
  • *
  • Posts: 7
Re: How do I read battery voltage?
« Reply #4 on: March 28, 2016, 08:07:13 pm »
Based on that error message, it looks like the necessary file was already included, but some other code was calling it in addition to mine::

Quote
In file included from /Users/nfriedly/Library/Arduino15/packages/digistump/hardware/oak/1.0.1/cores/oak/Arduino.h:251:0


(That corresponds to https://github.com/digistump/OakCore/blob/1.0.1/cores/oak/Arduino.h#L251 which is "include Esp.h" - https://github.com/digistump/OakCore/blob/1.0.1/cores/oak/Esp.h#L74)

defragster

  • Sr. Member
  • ****
  • Posts: 467
Re: How do I read battery voltage?
« Reply #5 on: March 29, 2016, 03:33:02 am »
I skipped reading the error message - maybe the ESP system isn't what it was - or the first line I noted "ADC_MODE(ADC_VCC);" - and detailed in public docs is not properly above everything else it needs to be.

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: How do I read battery voltage?
« Reply #6 on: March 29, 2016, 10:31:39 pm »
ADC_MODE(your_chosen_adc_mode) needs to be outside a function - i.e. at the top of your sketch like an include or define statement.

Code: [Select]
ADC_MODE(ADC_VCC);

void setup() {}
void loop() {
uint32_t getVcc = ESP.getVcc()
}

You will then get a different error if you are using Arduino 1.6.7 or 1.6.8, as it appears that the Arduino builder doesn't correctly forward declarations for functions marked with extern C. Workaround until it is fixed is to downgrade to Arduino 1.6.6 (or 1.6.5 as that appears to be the recommended version atm).

Alternately, until the next version of the ESP8266 core is pushed, you could make the changes noted in this commit to Esp.h and core_esp8266_phy.c. The line numbers for esp.h were the same for me on Oak 1.0.1, but esp8266_phy.c was out by 10 lines - I inserted the new code at 235 instead of 245 as in the commit. It compiles fine now, but have yet to see if it does anything useful.

EDIT: Yes, it does appear to work, but is reading low (needs calibrating).

Reference update: It appears that this won't be an issue for the next update to the Arduino IDE, as a fix was pushed to the arduino-builder 5 days ago, and should be in the nightly build. https://github.com/arduino/arduino-builder/pull/128
« Last Edit: March 30, 2016, 12:44:34 am by pfeerick »

nfriedly

  • Newbie
  • *
  • Posts: 7
Re: How do I read battery voltage?
« Reply #7 on: March 31, 2016, 05:14:54 am »
Awesome, thanks!