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

Memnon

  • Newbie
  • *
  • Posts: 28
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: 6329
  • Country: us
    • LowPowerLab
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

  • Newbie
  • *
  • 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

  • Newbie
  • *
  • 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: 6329
  • Country: us
    • LowPowerLab
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

  • Newbie
  • *
  • Posts: 18
  • Country: us
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: 6329
  • Country: us
    • LowPowerLab
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: gb
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: 6329
  • Country: us
    • LowPowerLab
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: gb
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: 147
  • Country: us
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: 6329
  • Country: us
    • LowPowerLab
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: 147
  • Country: us
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: 6329
  • Country: us
    • LowPowerLab
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.

ssmall

  • Full Member
  • ***
  • Posts: 147
  • Country: us
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #15 on: January 03, 2017, 04:20:20 PM »
Those two P-fets look like they are surface mounted.  How would I find ones that I could try out on a breadboard?

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6329
  • Country: us
    • LowPowerLab
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #16 on: January 03, 2017, 04:26:49 PM »
Just look for a P-FET with low Rds resistance.

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #17 on: January 03, 2017, 07:25:10 PM »
Don't forget you'll also need an N-channel FET to turn that top P-channel FET on. And if you do it that way you'd be better off using lower value resistors like 10k:20k and maybe a 10nF cap rather than 100nF to give a low RC constant for charging that cap.
Mark.

ssmall

  • Full Member
  • ***
  • Posts: 147
  • Country: us
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #18 on: January 04, 2017, 10:54:31 AM »
perky, thanks for feedback.  I understand lowering the value of the resistors and using a smaller cap so that the charging time is reduced.  I didn't know I would need another N-channel FET to turn the P-channel FET on, not sure how I would hook that up.  I found the following article that explains how to use a P-FET to measure voltage: http://fettricks.blogspot.com/2014/01/reducing-voltage-divider-load-to-extend.html
Going the transistor route seems more straight forward to me although I understand the drawback is that the transistor leaks current.

I did find this P-FET at Mouser http://www.mouser.com/ds/2/427/91080-69932.pdf  It has an Rds resistace of 3 ohms.

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #19 on: January 04, 2017, 12:55:12 PM »
You can use the pulsed gate approach with the capacitor as shown in your links, but personally I'd use an N-channel FET. What you're creating is a high side load switch:
http://www.mbedded.ninja/electronics/circuit-design/power-management/load-switches

BTW the criteria you're looking for is low RDSon, and low gate threshold Vgs (especially for the N-channel FET which has to work at logic levels). The P-FET you posted has way too high Vgs.

Mark.

ssmall

  • Full Member
  • ***
  • Posts: 147
  • Country: us
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #20 on: January 04, 2017, 05:53:19 PM »
The circuit you posted looks understandable.  :D  As far a a P-FET I found this one: http://www.mouser.com/ds/2/149/NDP6020P-1011713.pdf The data sheet says it is a logic level FET.

Thanks again for the help.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6329
  • Country: us
    • LowPowerLab
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #21 on: January 05, 2017, 07:52:51 AM »
The NPN is only needed if you want to drive your load from an active HIGH logic. Otherwise you can directly do it from a digital pin with inverted logic (p-fet is active low). It's a bit less intuitive and the pin has to be left HIGH-z (arduino pin set to INPUT, and write HIGH to it to turn the pfet switch OFF).
But yeah otherwise use a NPN to drive it just as any other active high load.

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #22 on: January 05, 2017, 08:43:04 AM »
The NPN is only needed if you want to drive your load from an active HIGH logic. Otherwise you can directly do it from a digital pin with inverted logic (p-fet is active low). It's a bit less intuitive and the pin has to be left HIGH-z (arduino pin set to INPUT, and write HIGH to it to turn the pfet switch OFF).
But yeah otherwise use a NPN to drive it just as any other active high load.

Remember that the I/O pin cannot go above Vdd+0.3V of the processor, otherwise the substrate diode will conduct and your P-FET will be permanently on (assuming you're switching a voltage that is above Vdd). You either need a capacitor in series with the I/O pin to create a pulse or limit the voltage on that pin by driving an NPN or N-FET to turn the P-FET on.
Mark.

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #23 on: January 05, 2017, 08:58:51 AM »
@ssmall BTW the NDP6020P you quoted has a Vgs max of +/- 8V, you'll violate that spec if you're swiching voltages above 8V unless you use an additional resistor to create a voltage divider. I'd choose one with +/- 20V spec. Also very low RDSon is not really needed if you're switching into a 10k load, even 10 ohms RDSon would only represent a 0.1% error. What you need is a P-FET with a low enough gate threshold to ensure it is hard on under lowest voltage switching conditions and with a max Vgs that is above your maximum swiching voltage.
Mark.
« Last Edit: January 05, 2017, 09:01:31 AM by perky »

ssmall

  • Full Member
  • ***
  • Posts: 147
  • Country: us
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #24 on: January 05, 2017, 06:02:41 PM »
@perky The input voltage will be a max of 6V.  I found this P-FET that has a Vgs of +/- 20, an RDSon of 10 ohms a Vgs(th) min -1 to max -2.  http://www.mouser.com/ds/2/268/TP2104%20C081313-846395.pdf  I am not confident I am looking at the correct values on the data sheet.  :o
I also want to make sure I get an Enhancement type P-FET, correct?  I have also been reading up how FETs work.  Thanks again

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #25 on: January 05, 2017, 08:26:41 PM »
This won't do unfortunately, the TO92 P-FETs appear to have a very high zero gate voltage drain current (i.e. leakage). The best ones for through-hole appear to be TO220. Try this, it has low leakage, low RDSon and low gate threshold:
http://www.infineon.com/dgdl/Infineon-SPP15P10PLH-DS-v01_04-en.pdf?fileId=db3a304325afd6e001264b6ae1090c81

Mark.



ssmall

  • Full Member
  • ***
  • Posts: 147
  • Country: us
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #26 on: January 15, 2017, 08:45:10 PM »
I wanted to report back on my progress.  I was able to implement both circuits successfully.   :)  I do have a few more questions.

1) Why is a resistor needed between the Gate and Source of the FET(s)?
2) I had to put a 200ms delay after pin A3 is set to LOW and the analog measurements are taken.  Is that because the capacitor needs to charge?  I am using a .1uf cap in parallel with a 470k resistor, just like the picture.

Thanks for the help.  This has been a great learning experience.

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #27 on: January 16, 2017, 12:05:35 PM »
Can you post the schematic of the circuit you're using? Setting A3 low implies you're driving the gate of the PET directly from the MCU. If the voltage your trying to measure is above the MCU's Vcc the FET might be on permanently. The resistor is because the gate is usually driven by an open drain or open collector signal, or at least one with capacitor in series from an MCU pin.

Post the circuit and it'll be easier to discuss.

Mark.

ssmall

  • Full Member
  • ***
  • Posts: 147
  • Country: us
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #28 on: January 16, 2017, 09:03:24 PM »
Mark, attached is a diagram of the circuit I built to monitor the battery voltage.  I basically eliminated the N-Channel FET from the load-switch diagram you posted.

-Steve

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #29 on: January 17, 2017, 08:19:41 AM »
OK, thanks for the diagram.

Firstly the MCU has things called substrate diodes, basically these are diodes at an I/O pin that stop the voltage on it going above Vcc+0.3V or below GND-0.3V. If the input to A3 goes above the MCU's Vcc by 0.3V the diode will conduct, and the voltage at A3 will be clamped to Vcc + 0.3V. Let's ay your Vin is 6V and Vcc is 3.3V, this means the A3, and hence the gate, will be at 3.6V and current will flow through the resistor (in this case with a 10k resistor it will be about 240uA). It also means the FET will be permanently on.

There are two solutions to this, either place a capacitor in series at the A3 pin which will create a negative going pulse on the gate due to the RC constant it creates with the resistor, or use an open drain or open collector driver such as the N-channel FET to drive the gate. In either case you prevent voltages at the A3 pin going above Vcc+0.3V.

Also as you saw the capacitor will need to charge up, and the RC constant for that depends on the values of your potential divider resistors and the capacitor value. This constant is quite long in your diagram. If you are going to switch the supply you don't need high value resistors for this (and 100nF is probably excessive), it would be better to use maybe 10k and 4k7 resistors and a 10nF capacitor, that means you'll only need to wait about 3ms to get a stable voltage.

Mark.
« Last Edit: January 17, 2017, 08:27:29 AM by perky »

ssmall

  • Full Member
  • ***
  • Posts: 147
  • Country: us
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #30 on: January 17, 2017, 12:15:47 PM »
To give you more information what I am doing, I have a solar panel that charges a Lipo battery during the day and the battery provides power at night.  The solar panel puts out a max of 6 volts.  What size capacitor would I use between A3 and the Gate of the FET?

-Steve

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #31 on: January 17, 2017, 02:29:59 PM »
If you remember your theory, the resistance times the capacitance is the RC constant. That is the time taken for the capacitor to charge up to about 2/3 of its final value. Under off conditions (after the capacitor has fully charged with A3 in the high state) the voltage at the gate will be at Vin. When you drive A3 low the voltage at the gate will immediately drop by Vcc, turning the FET on. You want this voltage to remain well below the gate threshold voltage to keep it on during the measurement (so that's at least 3ms plus the time to measure). As soon as measurement is made A3 can be driven back high which will immediately drive the gate to above Vin by the amount that it has been charged up with, turning the FET back off.

So you need an RC constant big enough. About ten times the time to measure should be OK, that'll guarantee the FET is hard on for the duration, thats RC = 0.03 (assuming you use 10nF and 10k/4k7 divider), so for your 10k pull-up that's a C of 3uF. I would probably use a 100k pull-up resistor instead of 10k and use a 330nF or 470nF to give roughly the same time constant. The procedure is then drive A3 low, wait 3ms, make the mesurement, and drive A3 back high.

Mark.
« Last Edit: January 17, 2017, 02:39:55 PM by perky »

ssmall

  • Full Member
  • ***
  • Posts: 147
  • Country: us
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #32 on: January 18, 2017, 09:30:00 AM »
Mark, I will change the battery monitor voltage divider as you suggested and make the changes you suggested on the P-FET side of things as well.  I need to order some parts.  :)  I appreciate all your help.

-Steve

MrGlasspoole

  • Newbie
  • *
  • Posts: 35
  • Country: de
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #33 on: January 27, 2017, 11:09:07 AM »
What I'm missing that you use this circuit?
Isn't that code enough:
https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #34 on: January 27, 2017, 12:25:38 PM »
That link you supplied was for calibrating by reading the 1.1 bandgap voltage (which is not particularly accurate in absolute terms but is reasonably stable over temperature) and comparing it with a known input accurate voltage. This problem though is about reading voltages that are above Vcc and keeping the power down at the same time.

Mark.

MrGlasspoole

  • Newbie
  • *
  • Posts: 35
  • Country: de
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #35 on: January 28, 2017, 02:15:18 AM »
Ah yes. Sorry i did forget not everybody is using 2x 1.5v batteries.

ssmall

  • Full Member
  • ***
  • Posts: 147
  • Country: us
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #36 on: February 05, 2017, 06:38:12 PM »
Mark, I made the changes to the circuit you suggested.  A 10nF cap and 10k/4k7 resistors for the voltage divider.  I used a 100k pull-up resistor between the gate and the source instead of the 10k.  Everything appears to work correctly.  I can see the voltage is zero across the voltage divider when A3 is set to INPUT (high-z) and I can see a voltage drop when A3 is set to OUTPUT, LOW.  If I put a 330nF capacitor in series with A3 the voltage is always zero.  Am assuming I am missing something.

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Battery monitor/sensing ratio calculation on MotionMote/WeatherShield
« Reply #37 on: February 05, 2017, 08:51:34 PM »
The thing you're missing is that with the 330nF capacitor in circuit A3 should always be an output (and note, the capacitor is connected to A3 on one side of the capacitor and the gate/pullup on the other side). From my previous post then: "The procedure is then drive A3 low, wait 3ms, make the measurement, and drive A3 back high." The default state for A3 should be driven high.

Mark.
« Last Edit: February 05, 2017, 08:56:54 PM by perky »