Voltage checker for Node MCU

Hey hi guys. I am making a voltage checker for my board- Node MCU(ESP 8266). I had already tried the below code with 21K ohm resistors connected in series. But the output voltage that I am getting isn't correct. I am getting 3.4V instead of the USB powered 5V. I am using 2 18650 batteries so that's a combined max voltage of 8.4 V. So how do I make my code/schematics right?

float R1,R2 = 1000;

void setup() {
  // put your setup code here, to run once:
  pinMode(A0, INPUT);

void loop() {
  // put your main code here, to run repeatedly:
  int sensorValue = analogRead(A0); //read the A0 pin value
  float voltage = sensorValue * (5.00 / 1024.00); //convert the value to a true voltage.
  float v1 = voltage / (R2/(R1+R2));
  Serial.print("voltage is: ");

And then I had used these connections(excluding the led)

Also, is there a way to check the voltage without the use of resistors or any other devices?

Please supply a schematic of YOUR connections, NOT a random image of a board that has nothing to do with YOUR question.

Your question is about a NodeMCU, but you post a picture of an Uno.

An UNO needs a voltage divider, but a NodeMCU does not.
Because it already has a 100k:220k voltage divider built-in.
For a NodeMCU you just use a 560K resistor between battery and analogue-in.

Forget all the code, and simply use this.
float voltage = analogRead(A0) * 0.0086;

1 Like

Oh no, I couldn't talk a photo of my schematics but I followed the exact ones that is shown in this image. So that's why I had pasted this.

But your question is about a 'node mcu', NOT an Arduino UNO.

I don't know if you are aware of this, but thay are nothing to do with each outher! The ADC on them are connected differently.

I'll ask one more time, post a schematic of YOUR setup.

I currently don't have a 560K ohm resistor. I have 1k, 2.1k and 10K resistors. Is there a way to use these?

These are my connections. I couldn't build a virtual schematic but the Blue wire, connected to A0, is connected to the intersection of the two resistors. The red and the dark brown wires are connected to an external power supply.
Hope this is a better representation.
Sorry for the trouble.

What results with --
Serial.println (sensorValue);

what's that knocking out?

In your photo I see 2x10k, not 2x21k. Where is the red wire going to exactly?
Are you aware the ESP microcontrollers run at 3.3V and that their pins are NOT 5V safe? This means that trying to measure e.g. your 8.4V power supply is NOT safe with this setup as it would put 4.2V onto the ADC pin. Putting 5V from USB onto it will be OK, and it is also OK to put the NodeMCU's 3.3V onto the voltage divider. But in case of the latter, I think you can measure the ESP's supply voltage directly without external components.

1 Like

For the sensorvalues variable, I was getting around 700 something with the above resistors. Is was charged with only a USB cable.

The top red wire is going into the vin pin and the bottom one to the positive point of the battery pack.

So in that case, what/ where should my connections be?

Ok, so that's NOT good. Luckily for your the ESP microcontrollers have fairly good overvoltage protection at their input pins and the 10k resistor limits the current through the clamping diode in the microcontroller, preventing it from being fried. Nevertheless, please do not power up the device this way anymore. Apart from the voltage you're applying to the ESP pin being higher than its supply voltage, the ESP8266 furthermore has a limit of 1V you should apply to the ADC pin.
See here: ESP8266 ADC Tutorial | NodeMCU ADC for Analog Input Arduino IDE

The ADC pin can be left floating. Please see the datasheet of the ESP8266 for further information; particularly pages 17-18: https://www.espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf

I am not powering the device with this wire. The way I am powering the device is through the USB cord and I am using a 4*AA battery pack that powers the nodeMCU through the vin and the ground points. This is a detailed photo. Hope this is better.

So according to this, you are saying that the A0 pin is getting too high of a voltage as the resistors I used are not that high right? So the only way is to use higher ohm resistors?

You need to make R1 larger, not R2. It's a voltage divider, make it so that the voltage on A0 never goes above 1V, and certainly not above the supply voltage of the ESP.

If you expect eg a maximum voltage of 9V from the battery, you want to make R1 at least 8 times bigger than R2.

So I just found out that I had a potentiometer of 100k ohms. If I put the min value as R1 and the 10K ohm as my R2, will that be fine?

Also, with these connections, I am checking the input voltage of my NodeMCU right?

Get a 560K resistor.
There must be some TV/stereo repair business in your town.

What on earth is that? :astonished:

I haven't seen such a thing for ten years or (probably a lot) more! :face_with_raised_eyebrow:

So like how does this system work? Why use only a 560k ohm resistor? Also, won't I waste some % of battery in the process?

There is already a 100k:220k voltage divider on a NodeMCU board, because the ESP8266 on that board has an A/D with ~1volt Aref. The divider changes that 0-1volt to 0-3.2volt (3.3volt).
560k between battery(+) and A0 changes the built-in divider to 100k:780k, and extends the voltage range ( to ~8.8volt).

Not using a second voltage divider uses less current from the battery.
In theory only 8.4/880k= ~9.5uA.

1 Like