Battery level indicator

Hey!

For a project I am working on I want to be able to display the current battery level on a display.

I am using the following circuit:

which comes from here https://github.com/rlogiacco/BatterySense

I use a 220ohm resistor instead of the 100.

Here's a photo of the circuit(without the mosfet)

As far as I understand the voltage gets divided so that the analog pin of the arduino can handle it and the mosfet gives me an option to turn the circuit off so that the battery doesn't get drained constantly.
So far so good.

I checked the voltages with a multimeter and the voltage division and the mosfet seem to work but I only get max readings at the "sense" pin.

I am an electronics noob and can't figure out what's the problem here...

Awrange:
...and the mosfet gives me an option to turn the circuit off so that the battery doesn't get drained constantly.

I'm afraid not.
The author made a vital thinking error.
When the mosfet is 'off', current will still flow through R1 and the pin protection diode of the MCU to VCC of the MCU.
That still drains the battery when battery voltage is > "VCC+0.5volt", and could be unhealthy for the input pin.
Leo..

That came to my mind as well and so I also tried to hook the battery up directly to the drain of the mosfet and added the voltage divider at the source. but that did not work either...

Maybe I hooked it up the wring way or i fried my analog pins with the other setup :confused:

What type of battery (and voltage) do you want to measure.
The 14.8volt (16.8volt charged) battery for the camera slider?
Leo..

Hi,

Wawa:
I'm afraid not.
The author made a vital thinking error.
When the mosfet is 'off', current will still flow through R1 and the pin protection diode of the MCU to VCC of the MCU.
That still drains the battery when battery voltage is > "VCC+0.5volt", and could be unhealthy for the input pin.
Leo..

OPs github image

If you only want to intermittently check the battery the simple solution would be a reed relay in series with R1.

Google 20mA coil 5v reed relay


Tom.... :slight_smile:

Re-arranging the resistors also might solve the problem.

1k resistor between source and ground.
Analogue pin to source.
Gate to a digital output pin.
15k resistor between battery(+) and drain.

Now switching on the mosfet will produce 1/16 of the battery voltage on the 1k resistor (~1volt).
You can read that with 1.1volt Aref enabled in code.

Mosfet must be logic level, because there is only ~4volt avalable for the gate.
A 2N7000 might just be ok with that with this low current (<1mA).
Leo…

Wawa:
What type of battery (and voltage) do you want to measure.
The 14.8volt (16.8volt charged) battery for the camera slider?
Leo..

Yeah exactly ;). For the V-mount batteries I probably would not need a circuit because afaik they have a dedicated pin from which you can read the battery status. But if I can get away with using the cheaper Sony NP style batteries I would like to have a battery indicator.

Just for testing out the circuit I am using a 9V block at the moment.

TomGeorge:
If you only want to intermittently check the battery the simple solution would be a reed relay in series with R1.

Tom… :slight_smile:

That would only be a replacement for the mosfet if i am not mistaken?

Wawa:
Re-arranging the resistors also might solve the problem.

1k resistor between source and ground.
Analogue pin to source.
Gate to a digital output pin.
15k resistor between battery(+) and drain.

Now switching on the mosfet will produce 1/16 of the battery voltage on the 1k resistor (~1volt).
You can read that with 1.1volt Aref enabled in code.

Mosfet must be logic level, because there is only ~4volt avalable for the gate.
A 2N7000 might just be ok with that with this low current (<1mA).
Leo…

The mosfet I am using is a IRF520 and should work if I interpret the datasheet correctly.

What exactly is the benefit of using the internal reference instead of the 5V default?

The IRF520 is NOT a logic level mosfet, and is in general NOT suitable to be used with Arduino's 5volt logic.
But you might get away with it at this low current.

The IRL520 is logic level (note the "L" in the number),
but there are much better logic level fets (lower Rds(on)) than this one.
Not that it matters for this application. The 2N7000 is good enough here.
Leo..

The IRF520 was the only mosfet i had available. As I mentioned, I am new to electronics and do not have a lot of parts stocked up. The URF520 was from the arduino starter kit i bought several years ago ;).

I have to try your suggestion regarding the circuit later, when I am back home.

Wawa:
Re-arranging the resistors also might solve the problem.

1k resistor between source and ground.
Analogue pin to source.
Gate to a digital output pin.
15k resistor between battery(+) and drain.

Now switching on the mosfet will produce 1/16 of the battery voltage on the 1k resistor (~1volt).
You can read that with 1.1volt Aref enabled in code.

What exactly is the reason for using internal reference voltage here?
Is it because you can conveniently get a value that uses the whole range of the reference for maximum resolution?

And thank you very much for your help Leo,
it is much appreciated!

Awrange:
What exactly is the reason for using internal reference voltage here?
Is it because you can conveniently get a value that uses the whole range of the reference for maximum resolution?

There is only 0-1volt available if you use the mosfet like I described.
That is not a bad thing, because the 1.1volt reference is potentially more stable than default Aref.
Yes, you still have the 10-bit A/D range (0-1023) if you call 1.1volt Aref in setup().

analogReference(INTERNAL);

Replace the 5.0 you normally have in the maths line to convert A/D value to voltage for e.g 1.075
(1.1volt Aref is never exectly 1.1volt, and varies between Arduinos).
Post your code if you want help with that.
You might have to experiment with that value to calibrate voltage readout.
Leo..

I tried your suggestion with a 10k going into Drain and a 1k going out of Source for roughly dividing 9V by 10 and it worked perfectly.

On the project arduino I will have to use the 5V reference though because the TFT-Shield I am using is occupying several analog pins and thus I have to keep the standard reference.
But the principle stays the same.

Thanks for your help, case closed!

Awrange:
I tried your suggestion with a 10k going into Drain and a 1k going out of Source for roughly dividing 9V by 10 and it worked perfectly.

On the project arduino I will have to use the 5V reference though because the TFT-Shield I am using is occupying several analog pins and thus I have to keep the standard reference.
But the principle stays the same.

Thanks for your help, case closed!

I think you will find that although the TFT is using analog designated pins, it is using them in digital mode, so changing Vref will have no effect on them.
Tom... :slight_smile:

TomGeorge:
I think you will find that although the TFT is using analog designated pins, it is using them in digital mode, so changing Vref will have no effect on them.
Tom... :slight_smile:

I tried it our but unfortunately it does not work. The Display is a Touch-TFT and the analog pins are used for the touch functionality. I tried to look up in which way they are used but the shield's documentation just stated that pin A0-A4 are used for the touch display. The main cpp file also wasn't clear about what was happening with the pins as there were 4 more headers included and I was not willing to dig through all of them ;).

Thanks for the information though!

Another way is to use a (5:12) fixed voltage divider, but with very high value resistors (1Megohm or higer).
If you go that way, you must also add a (100n) capacitor from analogue pin to ground to give the A/D something solid to sample from. And read the pin twice, and use the second reading.
Leo..