7

I use an ESP8266 dev board from NodeMCU with Lua. I power my chip with two AA batteries, which gives me 3V. See this:

https://www.hackster.io/noelportugal/ifttt-smart-button-e11841

enter image description here

How do I check the battery status using NodeMCU?

dda
  • 6,030
  • 2
  • 25
  • 34
Michael
  • 32,527
  • 49
  • 210
  • 370
  • Btw, if you don't want to have extra Lua code for the OTA WiFi setup part you may want to add the [enduser setup module](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en-(dev)---partial!#enduser_setup-module) to your firmware. – Marcel Stör Nov 14 '15 at 16:45

2 Answers2

10

With a recent firmware you can use adc.readvdd33(). That should be enough for your case

seblucas
  • 825
  • 1
  • 7
  • 17
  • Great. What is the lowest voltage the ESP8266 can operate with? – Michael Nov 14 '15 at 11:56
  • 2
    A friend of mine who is using 2*AA batteries usually replace the batteries as soon as the voltage drops below 2.5V. Below that point the ESP sometimes does not restart after a deep sleep. – seblucas Nov 14 '15 at 13:50
3

I read somewhere that adc.readvdd33() was deprecated? Effectively it is for many of the ESP8266 modules available, the docs say, "If the ESP8266 has been configured to use the ADC for sampling the external pin, this function will always return 65535". So that means that any ESP8266 that has an ADC pin (like ESP8266-07 or -12, etc.) has this shunted in firmware.

But by adding a couple of resistors to make a voltage divider, you can still use the ADC pin for this.

[![schematics][1]][1] [1]: https://i.stack.imgur.com/FEILF.png Those resistor values will allow it to read 0-12V, as a value between 0-1024. (The voltage at the ADC pin must be less than 1V.)

val = adc.read(0)

Addendum: Adding this to your circuit incurs a power draw of approx. 0.01 milliamps, small but more than nothing. Multiply the values by 1000 to reduce it to infinitesimal. Or use 18 megaohm for r1 and 2 megaohm for r2, which divides the voltage by 10, and (wild guess) drains less current than most if not all batteries will attenuate when disconnected.

Mark McGinty
  • 756
  • 7
  • 13
  • It is not deprecated, and there is an example showing what to read: https://nodemcu.readthedocs.io/en/master/en/modules/adc/#example – tito Apr 24 '18 at 08:02
  • @mark-mcginty I am using NodeMCU v1.0 and connecting it with 9V battery. So can you please tell me, what should be the resistors value? And how you calculated it? And this 5V and ground supple can come from NodeMCU builtin pins? – user3201500 Mar 20 '19 at 08:17
  • @user3201500 The values shown in my diagram above will work for 0-12 volts, 9 volts is within that range. Calcs: http://www.ohmslawcalculator.com/voltage-divider-calculator I'm not sure I understand your 3rd question, but if you're asking if any ESP8266 pins can supply 5 volts the answer is no. – Mark McGinty Jul 25 '19 at 15:23