Battery Voltage measurement 9V battery

If I want to use my Node MCU v1.0 to measure the Voltage of 9V battery, I'll have to use a voltage divider because the A0 pins max is 5V. so I took a voltage divider consisting of two 500Ohm resistors, which should cut the 9V by half. if i take a multimeter, i got my 4.5V but the MCU says its 5V and i dont get if its the inaccuracy of the board or if i made a mistake and slowly grilling it. the + side of the battery goes to Vin the minus to ground and the middle of the resitors to A0.

Welcome to the forum

Please post you sketch and a schematic of your project

Is that correct ?

int batteryPin = A0; // use A0 analog input pin to measure battery voltage
float batteryVoltage;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // read the voltage level from the battery pin
  int sensorValue = analogRead(batteryPin);

  // convert the analog reading to voltage
  batteryVoltage = sensorValue * (5.0 / 1023.0) * 2;

  // print the battery voltage to the serial monitor
  Serial.print("Battery voltage: ");
  Serial.print(batteryVoltage);
  Serial.println("V");

  delay(1000); // wait for 1 second before taking another measurement
}

so thats the sketch

image

and thats the build

In fact, the ADC pin of esp8266 (called A0 in Arduino compatible boards) can measure only up to 1.0V. To give more voltage range, the board already includes a voltage divider:


This gives a total range of 1.0*(220+100)/100 = 3.2V.

Any voltage over 3.2V will give the maximum 1023 reading. In your code, this will incorrectly calculate the voltage as 5.0V.

ok thank you very much so its not possible to measure my 9V battery except i use voltage divider until im under one volt an after multiply it up again? what doesnt sound very nice

If you add a 680K resistor in series with the pin, that will change the built-in voltage divider and allow you to measure up to 10V.

Thanks man

No problem.

But can I ask what the 9V battery is for in this circuit?

I hope it is not powering the NodeMCU. It would be pretty useless for that!

It is for powering haha, but the purpose is of the sketch is only to send a message if a button ist prest an afterwards it should got to deep sleep.

Ok, but if you change your mind, I would recommend using a 3.7V LiPo battery. These come in many sizes and capacities and have built-in over-discharge protection circuits (which is very important for LiPo batteries).

Unfortunately, the built-in voltage regulator on NodeMCU is not suitable for use with such low voltage batteries, so I would recommend using an external low-dropout regulator like mcp1700-33 or ht7333. You can then power the NodeMCU through the 3V3 pin.

https://www.baldengineer.com/9v-batteries-suck.html#:~:text=At%20100mA%20the%20Lithium's%20life%20is%20almost%208%20hours.

Til yesterday that was the plan haha, i put to of them in series and used the voltage dividwer for them but, both of them are empty and iI fdont have a charger for them

Charging modules for them are very cheap (< £3). You might want to solder a suitable in-line socket to them to match the plug on your LiPo batteries. Then you can use an old phone charger.
s-l300 (5)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.