Monitoring 3.7V battery voltage

Hello,
I have an Arduino NodeMCU V2 Lua (IOT board) that is powered by 3.7V 2600 mAh rechargeable battery (type 18650). I would like to know if there is an easy way to measure the voltage and when the voltage drops below certain level Arduino starts to beep (using piezo buzzer)?

I have found a solution that I have not tried yet.

void setup() {
  Serial.begin(9600);
}
void loop() {
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage);
}

How do I change this code to suitable for 3.7V battery?
float voltage = sensorValue * (3.7 / 1023.0);

It does not have to be very precise. It would be enough to know if the voltage has dropped from 3.7V to lets say 3.0V.

Edit:
found out that it is not possible to measure the same power source that Arduino is using, because this is used as analog reference.

Using voltage divider looks promising:

You can use the internal 1.1V reference and a voltage divider that's 4:1 ratio?

MarkT:
You can use the internal 1.1V reference and a voltage divider that's 4:1 ratio?

Sorry, I actually have NodeMCU V2 Lua, which is not based on ATMEGA, but is ESP8266 CP2102.

I've attached the datasheet for the ESP8266EX, which is actually quite a light document. Section 4.9 on page 17 describes the ADC features. You've probably got a voltage regulator on that board, so system_get_vdd33() will probably be worthless. Importantly, when measuring an external voltage the input is limited to 0V to 1.0V, very different from the range AVRs can accept. The way to set up the ADC is also quite different. You'll need to check with your core's author to figure out what they're doing, and maybe search more around Espressif's website for more documentation on the chip and toolchain.

ESP8266EX.pdf (640 KB)

the right value for 3.7v is

float voltage = sensorValue * (3.7 / 765.0);

maybe helps somebody!

1 Like

The NodeMCU V2 Lua has AFAIK a 220k:100k voltage divider on the chip's A/D pin.

If you want to measure more than ~3.2volt, then you need an additional resistor between battery(+) and the A/D pin of the board. 100k will increase measuring range to 4.2volt (a fully charged LiPo).
The formula will depend on that resistor.
Try sensorValue * (4.2/1024);
Leo..