Measure Li-ion battery voltage using analog input

Hello everyone.
I have built a radio transmitter using Arduino and nRF24.
I use a Li ion battery with a power bank module which has a USB port to power my circuit.
I wanted to measure the voltage of the battery and when it goes below 3.5V for example, a red LED flashes and I know it's time to charge the battery.
So I connected the positive pole of the battery to analog input 4 of my Arduino. Then I realized if I connect GND of my Arduino to the negative pole of the battery, Arduino and the rest of the circuit powers on! It seems it gets power through analog input pin
Is that normal? What should I do to avoid this thing happen? Because I don't want my circuit to power on as soon as I connect the USB to the USB port, because there is a switch which I could turn my circuit on or off with that, but now when I connect the USB, as the GND of the Arduino and the negative pole of the battery get connected through the USB port, my circuit powers on! Even if the switch is off.

Such a connection can destroy the Arduino, or at least the input pin, and may already have done so. Never apply a voltage to an Arduino input if it is not powered up the normal way, as it "ghost powers" the entire circuit through the input protection diode.

You can use the internal voltage reference to measure the voltage of a battery powering an Arduino. Search the forum for examples and code. One example https://forum.arduino.cc/index.php?topic=331178.msg2285210#msg2285210

jremington:
Such a connection can destroy the Arduino, or at least the input pin, and may already have done so. Never apply a voltage to an Arduino input if it is not powered up the normal way, as it "ghost powers" the entire circuit through the input protection diode.

You can use the internal voltage reference to measure the voltage of a battery powered Arduino. Search the forum for examples and code.

Well I've got an idea, what if I use a transistor that allows current from battery to analog input only when Arduino is powdered on and is applying a low voltage to the "Base" pin of transistor?

That won't work. Please follow the link I added above.

The arduino has protection diodes and if an input is at a higher voltage than the Vcc rail they conduct to protect the input. This allows your arduino to receive power. However its not intended for that!
You cant measure a voltage higher than Vcc directly. If you connect the supply to an analog input via a potential divider it will work.
I suggest you divide the voltage to be less than 1.1V in all conditions, then use the internal voltage reference.

So if you see this page on my own site

and refer to #2 the diagram shows such a divider.

(ignore the r560 and LM4040, thats an external reference you dont need in your application)

You could make R1 = 100k and R2 33k

making R1 large limits the current to the input pin when the arduino is off, preventing damage.

Or, don't bother with the voltage divider, and use the simple, direct measurement method discussed in this link.

The 1.1V internal voltage reference method works if you are powering your Arduino directly from the battery, so Vcc is the battery voltage. But you're using a powerbank which boosts the battery voltage to 5V no matter what the battery voltage is - until it dies.

This circuit may be more trouble than it's worth, but I think it will work. I don't know a way to do it with a single transistor.

You would turn on the NPN temporarily only when you want to measure the battery voltage.

18650 battery voltage.jpg

18650 battery voltage.jpg

@sherman - would that not work if you connected d4 directly to the FET gate and omitted the 100k resistor?

johnerrington:
@sherman - would that not work if you connected d4 directly to the FET gate and omitted the 100k resistor?

The 100K pull-up is needed to keep the fet off, I think. Without it, and with D4 connected directly to the fet, because D4 would be floating, the fet could switch on. But I do think connecting D4 directly could remove the need for the npn.

Are we right? Or missing something?

If D4 is an output why would it be floating?

If it was an output, it would not be floating. But when the Arduino is not powered, and while the bootloader is running, it would be floating then, I think.

erfan_m14:
Is that normal? What should I do to avoid this thing happen?

Just put a 47K Ohm resistor in serie with your analog input and read the voltage several times to allow the ADC to settle.

Better would be to go for analogereference(INTERNAL) and use a 47K divisor potentiometer.

Or even better, use an INA226 which gives you beside the voltage, also the battery current.

johnerrington:
@sherman - would that not work if you connected d4 directly to the FET gate and omitted the 100k resistor?

No it wouldn't. You would have the same problem described in the original post. If you turn off the power, D4 will fall towards ground just like A0 did, and that would turn on the mosfet, which would provide power to A0, and the OP is back where he started - unable to turn off the power to his circuit.

When the power is turned off, the NPN will turn off as D4 drops to ground, then the pullup resistor on the mosfet gate will keep the mosfet turned off, and the processor will stay off.