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