### Author Topic: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield  (Read 24685 times)

#### Memnon

• NewMember
• Posts: 28
##### Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« on: July 23, 2015, 06:18:23 PM »
Hi

I connected this moteino up with a pir motion sensor and loaded the MotionMote example code from github. I use 4x aa batteries, giving me 6.4 volts.
How does this code work, what are the dif numbers. And how do i get the right Volt reading?

Battery sensing hardware is this as shown in the picture
« Last Edit: July 30, 2015, 09:58:21 AM by Felix »

#### Felix

• Administrator
• Hero Member
• Posts: 6587
• Country:
##### Re: Battery sensing on the MotionMote
« Reply #1 on: July 23, 2015, 11:50:47 PM »
This circuit divides your battery voltage through a 1Meg and 470K ratio. So your Moteino analog pin sees [470k/(1000k+470k)] of your battery voltage, or ~0.32 of it. This would allow a max range of 10.31v before the analog pin sees 3.3v and maxes out the 10bit analog range. For instance a hypothetical 5V battery would divide to 1.6v into the analog pin, which will give a reading of about (1024 * 1.6v/3.3v) ~= 496 from analogRead(batteryMonitorPin).

So to get the actual voltage of your battery you have to multiply the analog reading to 0.0032 (=3.3v / 1024, ie 10bits resolution of analog reads). This gives you the voltage at the divider point, or the 1.6v in the hypothetical example of the 5V battery. Then you multiply that result to the inverse of the ratio of the divider (1/0.32), to arrive at the input voltage of 5V:

1.6v =====>   32% (at dividing point)
X      =====> 100% (at input point/battery)
So solving X = 1.6 * 100/32 = ~5V as expected

The division by 10 in the code shown is just an average of 10 analog samples (to even out any differences in analog reads).
The code you pasted is not using the same ratio since 1/0.32 != 1.42, that was probably more like a 1Meg:2Meg ratio. So each resistor divider ratio will have that third term different, you will have to determine that from your resistors:

Code: [Select]
``battVolts = analogReading * 0.0032 * dividerRatio;``

#### Memnon

• NewMember
• Posts: 28
##### Re: Battery sensing ratio calculation on the MotionMote
« Reply #2 on: July 24, 2015, 12:52:03 PM »
Thank you you very much!!

#### EloyP

• Jr. Member
• Posts: 58
##### Re: Battery sensing on the MotionMote
« Reply #3 on: July 24, 2015, 01:54:44 PM »
Great explanation!

This circuit divides your battery voltage through a 1Meg and 470K ratio. So your Moteino analog pin sees [470k/(1000k+470k)] of your battery voltage, or ~0.32 of it. This would allow a max range of 10.31v before the analog pin sees 3.3v and maxes out the 10bit analog range. For instance a hypothetical 5V battery would divide to 1.6v into the analog pin, which will give a reading of about (1024 * 1.6v/3.3v) ~= 496 from analogRead(batteryMonitorPin).

So to get the actual voltage of your battery you have to multiply the analog reading to 0.0032 (=3.3v / 1024, ie 10bits resolution of analog reads). This gives you the voltage at the divider point, or the 1.6v in the hypothetical example of the 5V battery. Then you multiply that result to the inverse of the ratio of the divider (1/0.32), to arrive at the input voltage of 5V:

1.6v =====>   32% (at dividing point)
X      =====> 100% (at input point/battery)
So solving X = 1.6 * 100/32 = ~5V as expected

The division by 10 in the code shown is just an average of 10 analog samples (to even out any differences in analog reads).
The code you pasted is not using the same ratio since 1/0.32 != 1.42, that was probably more like a 1Meg:2Meg ratio. So each resistor divider ratio will have that third term different, you will have to determine that from your resistors:

Code: [Select]
``battVolts = analogReading * 0.0032 * dividerRatio;``

#### pyndot

• NewMember
• Posts: 6
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #4 on: December 13, 2015, 06:38:49 PM »
Hi All,

The details of this post all make perfect sense, a simple circuit we should all become familiar with. But I'm wondering - since this is in the Low Power Techniques category - Has anyone done measurements on what attaching a circuit of this nature does to the battery life?

At what size of resistor combination does the current draw become negligible and therefore useful to implement in ultra low power applications?

#### Felix

• Administrator
• Hero Member
• Posts: 6587
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #5 on: December 14, 2015, 01:41:14 PM »
The details of this post all make perfect sense, a simple circuit we should all become familiar with. But I'm wondering - since this is in the Low Power Techniques category - Has anyone done measurements on what attaching a circuit of this nature does to the battery life?
At what size of resistor combination does the current draw become negligible and therefore useful to implement in ultra low power applications?
It depends on the battery voltage and the threshold of the max voltage you want to measure.
The current will never be negligible. Even if it's a few uA it still matters.
At very high values the resistors will start to give a noisy reading, hence the usual use of the 0.1uF capacitor.
I think in general use a 1MEG:2MEG division plus a 0.1uF will give good stable readings while keeping a low drain current. If you want a zero drain current then add a mosfet to only enable the circuit for a few microseconds while you do your reading.

#### steve v

• NewMember
• Posts: 18
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #6 on: November 23, 2016, 02:38:27 PM »
I built the voltage divider circuit with  two    , 1 M ohm resistors and a  0.1 Cap.     I use this to  try to read the battery voltage.  However I have also removed the Linear Regulator.

I run the moteino RF95 with 2 AA  Lithium L91 Batteries.

Now I see the voltage,  measured on Pin A7,  but it doesn't seem to move away from   a digital  512  of 1024

Do I need to do something different to measure my power supply batteries ?

#### Felix

• Administrator
• Hero Member
• Posts: 6587
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #7 on: November 23, 2016, 04:15:15 PM »
@steve,
If you remove the regulator we cannot assume the system voltage is 3.3v anymore but will drift with whatever the battery voltage. So you're effectively measuring the battery voltage against the battery voltage, it will always read whatever the resistor divider ratio is

So your only option is to use the internal bandgap 1.1v reference, not the most accurate thing, but your other option is to use a dedicated voltage monitor chip.

#### perky

• Hero Member
• Posts: 873
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #8 on: November 23, 2016, 04:20:22 PM »
I think in general use a 1MEG:2MEG division plus a 0.1uF will give good stable readings while keeping a low drain current. If you want a zero drain current then add a mosfet to only enable the circuit for a few microseconds while you do your reading.

Bear in mind the RC constant, a 1M resistor with 100nF cap has an RC constant of 0.1 seconds, to get it to charge up close to it's final value you'd need at least 5 times that so you might need to wait half a second before measuring. That may or may not negate any power saving and will depend on whether you sleep the MCU during this charging process. If you're going to use a MOSFET without sleeping the MCU during this time (usually when you have other things to do as well) it's probably better to use a much lower RC constant and turn it on for a much shorter period (e.g. 10k:20k with 10nF cap, 5ms settle time).
Mark.

#### Felix

• Administrator
• Hero Member
• Posts: 6587
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #9 on: November 25, 2016, 10:27:51 AM »
@perky,
You are correct, if the R values are very high the 0.1uF would charge slower. It would make sense to keep values low as you pointed out when using a mosfet for this circuit, and give it a few ms to charge before doing the reading.

#### perky

• Hero Member
• Posts: 873
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #10 on: November 25, 2016, 11:05:12 AM »
Yes. Also when doing these RC calculations it's the Thevenin equivalent resistance to use, i.e. parallel combination of the pull-up and pull-down resistors. If measuring high voltages with the top resistor being much bigger than the bottom resistor it's the bottom resistor that dominates.
Mark.

#### ssmall

• Full Member
• Posts: 156
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #11 on: January 03, 2017, 12:28:54 PM »
Could an NPN transistor (2N3904 ) be used to turn the circuit on/off?  Much like was done in the original Mail Box notifier that used an NPN transistor to turn on/off the hall effect sensor.

#### Felix

• Administrator
• Hero Member
• Posts: 6587
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #12 on: January 03, 2017, 01:39:03 PM »
Could an NPN transistor (2N3904 ) be used to turn the circuit on/off?  Much like was done in the original Mail Box notifier that used an NPN transistor to turn on/off the hall effect sensor.

I would use a P-fet (sourcing current) not a NPN transistor (sinking). Yes it's doable, it was implemented on WeatherShield R1 but removed in R2.

#### ssmall

• Full Member
• Posts: 156
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #13 on: January 03, 2017, 02:33:39 PM »
Felix, thanks for the information.  I was just looking at the Mailbox Notifier as a reference and some of this is a bit over my head, would you know the part number for a P-fet that would work?

#### Felix

• Administrator
• Hero Member
• Posts: 6587
• Country:
##### Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #14 on: January 03, 2017, 02:38:41 PM »
Si2333 is a good choice, Si2365 also would work.