Help with detecting battery level and when USB is connected

Status
Not open for further replies.

lerxstrulz

Well-known member
Hi All,

I have a Teensy 3.2 using the audio shield and am using a Trinket Backpack LiPoly adapter to power it. I cut the Vin/VUSB trace and have soldered the connections from the backpack to the Teensy. I can run the boards with the battery or the USB and program it...that works great.

I am having the sketch hibernate the Teensy when voltage drops below 3mA using the following code:

Code:
void setup()
{
   analogReference(EXTERNAL);
   analogReadResolution(12);
   analogReadAveraging(32);
   Serial.begin(57500);
}

elapsedMillis batteryCheck;

void loop()
{
   // check every 10 seconds
   if (batteyCheck >= 10000) {
     // return millivolts
     int mv = 1195 * 4096/analogRead(39); 
     Serial.println(mv);
     if (mv < 3000) {
       ... call hibernate ...
    } else if (mv < 3200) {
       ... play low battery warning ...
     }
   }
}

This works pretty well in telling me approx how much voltage is coming in from the battery, but I am getting some mixed results sporadically. For example, when logging the voltage to a file (recording every 10 seconds) on the SD card, most times I can watch it drop until it reaches the low battery warning threshold (< 3200) and then the shutdown threshold (< 3000), but sometimes it just drops drastically and dies before the warning code can be called and therefore just executes the hibernate code:

Code:
3359
3357
3354
3354
3352
3354
[B]3352
2478[/B]

And still other times it will just fall off altogether and neither the warning or the shutdown is called:

Code:
3359
3357
3354
3354
3352
3354
... Teensy dies here ...

It seems that once it gets around ~3V it just drastically falls off, which if I remember correctly is how newer rechargables work. However, if I only charge the battery partially, it seems the fall off is much smoother (it steps down slower instead of just falling off.)

This approach is working pretty well, however since I am only monitoring the battery, whenever you plug in the USB cable (which in this setup will still power the Teensy) the code interferes if the battery is below an acceptable level (for instance if the battery is at "shutdown" level, it will attempt to hibernate even though the USB cable is plugged in and providing plenty of power while it is recharging the battery.)

Is there a way to detect that the USB cable is plugged in and providing power in this scenario? I tried checking the internal voltage, but no matter what that is at a constant 1.2 volts (which is correct according to what I've read.)

Code:
void setup()
{
   // change to check INTERNAL
   analogReference([B]INTERNAL[/B]);
   analogReadResolution(12);
   analogReadAveraging(32);
   Serial.being(57600);
}

elapsedMillis batteryCheck;

void loop()
{
   // check every 10 seconds
   if (batteyCheck >= 10000) {
     // return millivolts
     int mv = 1195 * 4096/analogRead(39); 
     Serial.println(mv);
     if (mv < 3000) {
       ... call hibernate ...
    } else if (mv < 3200) {
       ... play low battery warning ...
     }
   }
}

Value is the same whether USB is plugged in or running off battery:

Code:
1195
1195
1195
1195
1195

So ideally I would check if the USB is plugged in and if it is, avoid the warning and shutdown code, but if it is not, check the battery voltage and warn or shutdown as appropriate, but I am not sure how to detect if USB is plugged in and providing power. Any ideas on how to check that?

Thank you!
 
I recall that Sparkfun had a battery monitor that provides information via i2c about the battery state:

In looking for the battery monitor, I saw they now have a 'battery babysitter' which combines charging and status:

I haven't used either one, so I can't say how well they work.

Thanks for those! The battery babysitter looks great, but it's too big for my project (but I might use that for something in the future...) I ordered the other battery monitor and will integrate it into my project.
 
Status
Not open for further replies.
Back
Top