# Voltage checker for Node MCU

**URL:** <https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070>\
**Category:** General Electronics\
**Created:** [July 14, 2021, 8:56am UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070 "2021-07-14T08:56:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 14, 2021, 8:56am UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/1 "2021-07-14T08:56:12Z")

</div>

Hey hi guys. I am making a voltage checker for my board- Node MCU(ESP 8266). I had already tried the below code with 2_1K 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?

```cpp
float R1,R2 = 1000;

void setup() {
  // put your setup code here, to run once:
  pinMode(A0, INPUT);
  Serial.begin(9600);
}

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

```

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

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/5/b/d/5bdc3d4bc3b37b1cd2012106a8c67d339bfc9044.jpeg)

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

---

<div class="post-metadata">

**Author:** ![missdrew](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@missdrew](https://forum.arduino.cc/u/missdrew)\
**Post date:** [July 14, 2021, 9:21am UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/2 "2021-07-14T09:21:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [July 14, 2021, 10:01am UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/3 "2021-07-14T10:01:18Z")

</div>

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;  
Leo..

---

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 14, 2021, 3:48pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/4 "2021-07-14T15:48:39Z")

</div>

> [@missdrew](#):
>
> Please supply a schematic of YOUR connections, NOT a random image of a board that has nothing to do with YOUR question

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.

---

<div class="post-metadata">

**Author:** ![missdrew](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@missdrew](https://forum.arduino.cc/u/missdrew)\
**Post date:** [July 14, 2021, 5:11pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/5 "2021-07-14T17:11:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 14, 2021, 6:57pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/6 "2021-07-14T18:57:56Z")

</div>

> [@Wawa](#):
>
> For a NodeMCU you just use a 560K resistor between battery and analogue-in.

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

---

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 14, 2021, 7:02pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/7 "2021-07-14T19:02:55Z")

</div>

![image](https://europe1.discourse-cdn.com/arduino/original/4X/f/a/4/fa42018def036bf17d1741aeceb3cbbca1e5aa66.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![anon85221860](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon85221860](https://forum.arduino.cc/u/anon85221860)\
**Post date:** [July 15, 2021, 2:19pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/8 "2021-07-15T14:19:39Z")

</div>

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

what's that knocking out?

---

<div class="post-metadata">

**Author:** ![anon35827816](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@anon35827816](https://forum.arduino.cc/u/anon35827816)\
**Post date:** [July 15, 2021, 2:30pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/9 "2021-07-15T14:30:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 16, 2021, 8:54am UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/10 "2021-07-16T08:54:37Z")

</div>

> [@anon85221860](#):
>
> What results with --  
> Serial.println (sensorValue);

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

---

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 16, 2021, 8:57am UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/11 "2021-07-16T08:57:09Z")

</div>

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

> [@anon35827816](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![anon35827816](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@anon35827816](https://forum.arduino.cc/u/anon35827816)\
**Post date:** [July 16, 2021, 11:28am UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/12 "2021-07-16T11:28:41Z")

</div>

> [@harsh24](#):
>
> and the bottom one to the positive point of the battery pack.

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](https://www.electronicshub.org/nodemcu-esp8266-adc-tutorial/)

> [@harsh24](#):
>
> So in that case, what/ where should my connections be?

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](https://www.espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf)

---

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 16, 2021, 9:41pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/13 "2021-07-16T21:41:36Z")

</div>

> [@anon35827816](#):
>
> Nevertheless, please do not power up the device this way anymore.

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.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/3/6/7/367b7e236237948ea4ebd573b2aef9044c53d931.jpeg)

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?

---

<div class="post-metadata">

**Author:** ![anon35827816](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@anon35827816](https://forum.arduino.cc/u/anon35827816)\
**Post date:** [July 16, 2021, 9:43pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/14 "2021-07-16T21:43:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 16, 2021, 9:53pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/15 "2021-07-16T21:53:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 16, 2021, 10:01pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/16 "2021-07-16T22:01:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [July 16, 2021, 10:30pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/17 "2021-07-16T22:30:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [July 16, 2021, 11:59pm UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/18 "2021-07-16T23:59:14Z")

</div>

> [@Wawa](#):
>
> There must be some TV/stereo repair business in your town.

What on earth is that? 😲

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

---

<div class="post-metadata">

**Author:** ![harsh24](https://avatars.discourse-cdn.com/v4/letter/h/4bbf92/32.png) [@harsh24](https://forum.arduino.cc/u/harsh24)\
**Post date:** [July 17, 2021, 2:09am UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/19 "2021-07-17T02:09:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [July 17, 2021, 2:44am UTC](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070/20 "2021-07-17T02:44:39Z")

</div>

> [@harsh24](#):
>
> Why use only a 560k ohm resistor?

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.  
Leo..

[Next page](https://forum.arduino.cc/t/voltage-checker-for-node-mcu/885070.md?page=2)
