Inaccurate voltage readings using BatterySense library, resistors and N-Mosfet

Hi everyone,

I’m trying to measure battery voltage and level of two 6 x AA batteries with Arduino Mega and Battery Sense library. Both battery packs are wired in parallel to make up ~6A of 9.66V. The circuit diagram of my setup is attached below.

Here is my code, strictly based on the example BatteryMonitor.ino provided by the library. The only differences are in the parameters for Battery object which are voltage sensing pin number, mosfet activation pin, minimum millivolt value and maximum millivolt value.

#include <Battery.h>

# define POWER_SENSE A15 // must be analog pin
# define POWER_ACTIV 41 // either analog or digital

Battery battery(500, 9660, POWER_SENSE, POWER_ACTIV);
// Battery(uint16_t minVoltage, uint16_t maxVoltage, uint8_t sensePin, uint8_t activationPin = 0xFF);

void setup() 
{
  Serial.begin(115200);
  while (!Serial);
  battery.begin();
}

void loop() 
{
  Serial.print("Battery voltage is ");
  Serial.print(battery.voltage());
  Serial.print(" (");
  Serial.print(battery.level());
  Serial.println(")");
}

The level of voltage measured with a digital multimeter is 8910 millivolts which isn’t matched with the reading returned by the library. The readings are also unsteady. See below the reading result captured from the serial monitor:

...
Battery voltage is 4960 (0)
Battery voltage is 34960 (100)
Battery voltage is 40496 (100)
Battery voltage is 34960 (0)
Battery voltage is 14960 (0)
Battery voltage is 24960 (100)
Battery voltage is 24960 (0)
Battery voltage is 496 (0)
Battery voltage is 14960 (100)
Battery voltage is 34960 (0)
Battery voltage is 24960 (0)
Battery voltage is 24960 (0)
Battery voltage is 4960 (100)
Battery voltage is 34960 (0)
Battery voltage is 60496 (0)
Battery voltage is 24960 (100)
Battery voltage is 24960 (100)
Battery voltage is 14960 (100)
Battery voltage is 14960 (0)
Battery voltage is 24960 (100)
Battery voltage is 14960 (100)
Battery voltage is 34960 (0)
Battery voltage is 24960 (100)
Battery voltage is 24960 (0)
Battery voltage is 24960 (100)
Battery voltage is 4960 (0)
Battery voltage is 24960 (100)
Battery voltage is 24960 (100)
Battery voltage is 24960 (0)
Battery voltage is 34960 (0)
Battery voltage is 34960 (0)
Battery voltage is 14960 (0)
Battery voltage is 14960 (0)
Battery voltage is 34960 (100)
Battery voltage is 24960 (100)
Battery voltage is 34960 (100)
Battery voltage is 24960 (100)
Battery voltage is 34960 (0)
Battery voltage is 24960 (0)
Battery voltage is 24960 (0)
Battery voltage is 24960 (100)
Battery voltage is 34960 (0)
Battery voltage is 34960 (0)
Battery voltage is 14960 (0)
Battery voltage is 34960 (100)
Battery voltage is 24960 (0)
Battery voltage is 34960 (100)
Battery voltage is 24960 (0)
Battery voltage is 34960 (100)
Battery voltage is 34960 (0)
Battery voltage is 34960 (100)
Battery voltage is 24960 (0)
Battery voltage is 24960 (100)
Battery voltage is 14960 (100)
Battery voltage is 24960 (100)
Battery voltage is 40496 (0)
Battery voltage is 24960 (100)
Battery voltage is 46032 (0)
Battery voltage is 14960 (0)
Battery voltage is 34960 (0)
Battery voltage is 14960 (0)
Battery voltage is 34960 (100)
Battery voltage is 24960 (0)
Battery voltage is 34960 (100)
Battery voltage is 34960 (0)
Battery voltage is 14960 (100)
...

Note that I am using F3055L Mosfet instead of 2n7000 mentioned in the library documentation. I’m not sure if this causes the problem.

I can’t see what wrong I did with the code or circuit or what I’m missing. Any suggestion or comment is appreciated.

Correction of the circuit diagram attached.

Why is the FET in the circuit, and why is it upside down?

MarkT:
Why is the FET in the circuit, …

Sorry for not making this clear earlier. The FET is used to switch ON/OFF the activation pin, preventing R1 and R2 from burning energy at all time.

MarkT:
… and why is it upside down?

Thanks for pointing out. That’s a mistake I made while rushing to finish the diagram. A correction of the FET orientation is attached in this post.

voltmeter_batterySenseLib_0_3_schem.png

Moderator edit: image linked / embedded

When your FET is off, you are attaching 10V to pin A15 through R1.

Normally I’d say this is a horrible thing, although earlier on the forum somebody pointed out that Atmel actually accepts using their internal protection diodes as long as the current is limited to <1mA.

Still, given you want to have no battery current when the FET is off, I doubt you intended to attach 10V to pin A15.

@BigBobby , I might be wrong, but I think OP is using the FET to allow the voltage measure only when he wants to, and he doesn't want the resistor divider to waste power when he doesn't measure the battery voltage - As you can see, the FET has nothing to do with the arduino power itself, the arduino is powered even if the FET is OFF

Edit : but you are right, there is still 10V on A15 pin when FET is OFF, which is..... bad :wink:

Well, current will be within spec (half a ma) so it's safe, but it completely defeats the point of using the fet :wink:

alnath:
... but I think OP is using the FET to allow the voltage measure only when he wants to, and he doesn't want the resistor divider to waste power when he doesn't measure the battery voltage - As you can see, the FET has nothing to do with the arduino power itself, the arduino is powered even if the FET is OFF

Exactly, I wouldn't like to waste power when the voltage measurement is not needed that is why I added a FET in the circuit.

Also as it's mentioned in this thread, I got about 10V fed into A15 of the Arduino, you are right but I checked the pin with some other input/output functions i.e. digitalRead() and digitalWrite(), it is still working after all. I'm aware that this is a bad practice but I am desperate to be able to save some power of the battery so still looking for ways to use a transistor in the circuit.

I actually achieved measuring the power supply with just only the voltage divider Vout connected to pin A15 and bypassing the FET, which was working fine.

Can someone give me some advice on how to use the FET correctly in able to achieve this.

Thanks.

nandakrpp:
Exactly, I wouldn't like to waste power when the voltage measurement is not needed that is why I added a FET in the circuit.
...
Can someone give me some advice on how to use the FET correctly in able to achieve this.

Using a P-FET/N-FET combination is the best way, similar to how the OP fixed their issue here -> http://forum.arduino.cc/index.php?topic=381711

That thread is also where I learned Atmel permitted applications to use their protection diodes, and there's a simpler circuit there that avoids the FETs. I see a lot of low power battery applications each month, however, and virtually all of them use the FET combo that Crossroads recommended in that thread.