# Measuring battery voltage with an MCU while charging and discharging

#### YoTech

Mar 21, 2018
41
Ok. The problem that I meet is that I have to measure the battery voltage while the battery is charging also or at least when its plugged and when the charger stops (about 6h). It will be charged from an ABC-1220D FST charger, rated for 20 ampers. The load will consume 24V, 25A. And the shit of having to charge separately and discharge toghether while measuring. That is the reason behind the analog components (the comparator circuit) which are then checked by the MCU. Because I dont see another way to measure the battery while its charging except to measure charger voltage until the charger stops and the battery voltage starts dropping from 14.7 to 13.7V. At 13.7 the charger will start maintain mode and the battery will not fall more.

#### Attachments

• Good.battery.monitor.12V.jpg
31.2 KB · Views: 94
• Idea.png
93.7 KB · Views: 105

#### YoTech

Mar 21, 2018
41
Also will this transistor be good enoug? Given that I have 24 volts from 2 batteries, this transistor is 20V, but I dont think the whole voltage will be applied to it?

MOS FET: SI9926CDY

#### kellys_eye

Jun 25, 2010
6,514
What's wrong with using the MCU (via an ADC port) to measure the battery voltage directly? Equally you can fit a sense resistor and measure the charge current.

Using the data from the battery manufacturer you can find out the ideal charge/discharge rates for the system and monitor and/or charge appropriately.

#### YoTech

Mar 21, 2018
41
The wrong is that the voltage measured will not be of the battery, but of the charger, or at least it will not be accurate, because the battery needs to be stopped until the voltage settles.

Can you give a circuit for the sense resistor please?

This is the proper circuit for my idea, if I have 3 grounds like on this circuit, not connected to Batt1- and Batt2-, will it work?:

#### Attachments

• Idea.png
93.7 KB · Views: 66
Last edited:

#### (*steve*)

##### ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
During charging the battery voltage will be the charger voltage.

If you want to measure the battery voltage whilst it is not under load (the charger represents a type of load -- albeit one that has a negative power) then you need to disconnect or otherwise disable the load.

#### YoTech

Mar 21, 2018
41
So this should work?

#### Attachments

• Battery.monitor.MOS.png
229.5 KB · Views: 71

#### YoTech

Mar 21, 2018
41
How to use the comparator circuit from above with an MCU?

#### (*steve*)

##### ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Using n channel MOSFETs like that will require a gate voltage of about 30V to turn them on.

I would recommend you use a p channel MOSFET which can be turned on by pulling the gate toward ground.

You will have to make sure you don't exceed Vgs(max), and that you don't try to source current into the microcontroller. The easiest way is to use the microcontroller to turn on a bjt which pulls the gate low against a resistor from gate to source. You probably also want a 10V zener diode between gate and source, and a resistor in series with the collector (or gate) to limit the current.

I may be able to draw you a circuit later.

#### YoTech

Mar 21, 2018
41
I think a logic NPN MOS FET will work at 3,3V. It has Rds on at 2 to 4V, so its ok?
A P MOS might be good, but I cant find 1 at 3.3V, there I will have to use the suggestion you made.

#### (*steve*)

##### ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
The way you have that mosfet, the gate will have to be raised to 24V + the Vgs, so 28 volts or so.

#### (*steve*)

##### ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
This is a suitable high side switch for a microcontroller:

R1 limits the current into the base of Q2

Q1 is a P channel mosfet used to switch the load (whatever it is)

D1 prevents the gate from being destroyed by too high a Vgs. This is important if the supply voltage is above 20V (the typical Vgs(max)). This limits Vgs to 10V which should be sufficient.

R2 holds the mosfet off by pulling the gate high through R3 when Q2 is off.

R3 limits the current through D1 when Q2 is turned on.

Placing a logic high on the input turns the load on. Leaving it open or pulling it low will turn the load off.

#### (*steve*)

##### ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Another problem your circuit has is that the resistors at the bottom of all of your voltage dividers are in parallel. If all the voltage dividers are the same, then either pick a value 3 times as large as you need, or eliminate all but one of them. This assumes that you'll only turn on one mosfet at a time (which seems sensible).

#### YoTech

Mar 21, 2018
41
The MCU will be 3.3V. I meet 2 more problems with the practical realisation of this project. I have to be done by Tuesday, so thank you very much for you help!

First: The batteries will be connected in series while discharging and separated when charging. What will happen with the upper voltage divider if its still connected to the lower battery's minus, while they are both charging separately with 2 chargers?

Second: I intend to put a copper fill on the bottom copper, which will act as ground and be connected to the lower battery's minus. Do I need a second copper fill on the internal layers to use as a second ground and should the 2 ground layers be connected? Can these 2 grounds for the upper and lower battery be replaced with 1 external ground?

On the other hand if the circuit is connected not to ground, but only to each battery's plus and minus, will that not feed some electricity from the lower battery into the ADC pin?

I attach 2 block diagrams.

#### Attachments

• Grounds.circuit.jpg
140.1 KB · Views: 58
• No.grounds.jpg
140.8 KB · Views: 59

#### (*steve*)

##### ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
If you connect all the batteries negative terminals together while charging, you had better disconnect them before you connect them in series -- unless you like pyrotechnics in your batteries.

I'll try to address the other questions later.

#### YoTech

Mar 21, 2018
41
I edited the post:
They will be disconnected before they are connected in series. But the PCB will stay mounted on a battery bank of
2 batteries 12V, 120A/h. The idea is that there is no real ground. Instead everything has to go to the upper battery's "-" or the lower battery's "-". And if I connect the upper battery's voltage divider to the lower battery's "-", while they are in series it will be ok, but when I flip the switch and disconnect the series connection separating the 2 batteries and connect them separately to the charger what will happen with the PCB, which has a voltage divider that is still connected to the upper battery's "+" and the lower battery's "-"? There will be a PCB layer (bottom copper) which will act as ground, but it still goes to the lower battery's "-".

Last edited:

#### YoTech

Mar 21, 2018
41
I will post the diagram for the other connection when we review this one if possible.

#### Attachments

• On.off.diagram.jpg
140 KB · Views: 67

#### (*steve*)

##### ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
You can't use a simple single switch like that to swap between a series config and one where the negatives are connected.

#### YoTech

Mar 21, 2018
41
I am not switching between a series and parralel connection. All I do is disconnected the series connection or reconnect it, thats why its a problem what will happen if after I disconnect the series connectin there is still a voltage divier between the upper battery's "+" and the lower battery's "-".

#### (*steve*)

##### ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
But in this post you show the negative ends of the batteries connected together

#### (*steve*)

##### ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
At this point, all I can tell you is that unless you have a common ground, your voltage divider scheme won't work. And if you have a common ground the series connection won't work.

Perhaps you need to describe exactly what you want to do so we can suggest how you can do it rather than you chase two mutually incompatible ideas in separate threads.

Why do you need to separate the batteries?

Replies
26
Views
4K
S
Replies
8
Views
7K
sreenath
S
Replies
6
Views
1K
Replies
6
Views
2K
A
Replies
13
Views
2K
Watson A.Name - \Watt Sun, the Dark Remover\
W