lerxstrulz
Well-known member
Hey guys,
I've been working on converting a project that uses a Teensy 3.2 + Audio Adaptor to work on a lipo battery and have the lipo charger (https://www.adafruit.com/product/2124) connected and working fine. I really need to be able to tell how much voltage the battery has, but have not been able to find a reliable way to do that. I found a snippet from another thread that reads pin 39 and tried this code to start reading the battery once a minute and then decrease the interval it is read as the voltage gets lower:
I do not get consistent results with this. Sometimes it works fine, sometimes the battery just drops off without warning. Sometimes the Teensy will not go into sleep mode. (The other problem with this is that I cannot tell when the battery is being charged....)
So I got this:
https://www.sparkfun.com/products/10617
But I am not sure how it should go into the setup, or if it will even work. It uses the SDA and SCL pins (18 & 19) to communicate to the Teensy, which are already in use by the Audio Adaptor. However, according to the Teensy schematic, there is another set of pins (16 & 17) that it could possibly use? (see PINOUTS section here https://www.sparkfun.com/products/10617)
If that's the case, I'm pretty sure I would plug the battery into the charger, then run the VCC/GND to the fuel gauge and then out to the Teensy, correct? Has anyone done something like this before? I am using 500mAh batteries and need the 500mAh recharge rate. I saw another board on Tindie that handles the charge, but it does not have a status pin.
Any ideas appreciated! Thank you!
I've been working on converting a project that uses a Teensy 3.2 + Audio Adaptor to work on a lipo battery and have the lipo charger (https://www.adafruit.com/product/2124) connected and working fine. I really need to be able to tell how much voltage the battery has, but have not been able to find a reliable way to do that. I found a snippet from another thread that reads pin 39 and tried this code to start reading the battery once a minute and then decrease the interval it is read as the voltage gets lower:
Code:
/***
* Main program loop
*/
elapsedMillis batteryMillis;
int batteryCheck = 0; // set to 0 so that a check is done as soon as the loop starts (it will be reset.)
int minutes = 0;
// in mA
const int lowBatteryThreshold = 3300;
const int criticalBatteryThreshold = 3100;
const int fatalBatteryThreshold = 3000;
void loop()
{
// every 5 minutes
if (batteryMillis >= batteryCheck) {
batteryMillis = 0;
// ready mA
int mv = 1195 * 4096/analogRead(39);
if (mv <= fatalBatteryThreshold) {
// battery almost dead...go to sleep!
audioShield.unmuteHeadphone();
audioShield.unmuteLineout();
// plays a WAV file to notify user it is shutting down
long l = playSound(SHUTDOWN_WAV);
delay(l+500);
// turn everything back off
audioShield.muteHeadphone();
audioShield.muteLineout();
MUTED = true;
// Need to wait a second...
delay(1000);
// see this method below
gotoSleep();
} else if (mv <= criticalBatteryThreshold) {
// battery really low...change check time to 10 seconds
batteryMillis = 0;
int batteryCheck = 10000;
// warn user battery is critically low and about to die
long l = playSound(CRITICAL_BATTERY_WAV);
delay(l+500);
// turn off all sound in case the battery dies
// before the fatal check
audioShield.muteHeadphone();
audioShield.muteLineout();
} else if (mv <= lowBatteryThreshold) {
batteryCheck = 60000;
// warn user battery is low
playSound(LOW_BATTERY_WAV);
} else {
// set to 1 minute
batteryCheck = 60000;
}
}
}
/********************* go to deep sleep *********************/
#define SMC_PMPROT_AVLLS_MASK 0x2u
#define SMC_PMCTRL_STOPM_MASK 0x7u
#define SCB_SCR_SLEEPDEEP_MASK 0x4u
// see SMC section (e.g. p 339 of K66)
#define VLLS3 0x3 // RAM retained I/O states held
#define VLLS2 0x2 // RAM partially retained
#define VLLS1 0x1 // I/O states help
#define VLLS0 0x0 // all stop
#define VLLS_MODE VLLS0
void gotoSleep(void)
{
// /* Make sure clock monitor is off so we don't get spurious reset */
// MCG_C6 &= ~MCG_C6_CME0;
//
/* Write to PMPROT to allow all possible power modes */
SMC_PMPROT = SMC_PMPROT_AVLLS_MASK;
/* Set the STOPM field to 0b100 for VLLSx mode */
SMC_PMCTRL &= ~SMC_PMCTRL_STOPM_MASK;
SMC_PMCTRL |= SMC_PMCTRL_STOPM(0x4); // VLLSx
SMC_VLLSCTRL = SMC_VLLSCTRL_VLLSM(VLLS_MODE);
/*wait for write to complete to SMC before stopping core */
(void) SMC_PMCTRL;
SYST_CSR &= ~SYST_CSR_TICKINT; // disable systick timer interrupt
SCB_SCR |= SCB_SCR_SLEEPDEEP_MASK; // Set the SLEEPDEEP bit to enable deep sleep mode (STOP)
asm volatile( "wfi" ); // WFI instruction will start entry into STOP mode
// will never return, but call ResetHandler() in mk20dx128.c
}
// END
I do not get consistent results with this. Sometimes it works fine, sometimes the battery just drops off without warning. Sometimes the Teensy will not go into sleep mode. (The other problem with this is that I cannot tell when the battery is being charged....)
So I got this:
https://www.sparkfun.com/products/10617
But I am not sure how it should go into the setup, or if it will even work. It uses the SDA and SCL pins (18 & 19) to communicate to the Teensy, which are already in use by the Audio Adaptor. However, according to the Teensy schematic, there is another set of pins (16 & 17) that it could possibly use? (see PINOUTS section here https://www.sparkfun.com/products/10617)
If that's the case, I'm pretty sure I would plug the battery into the charger, then run the VCC/GND to the fuel gauge and then out to the Teensy, correct? Has anyone done something like this before? I am using 500mAh batteries and need the 500mAh recharge rate. I saw another board on Tindie that handles the charge, but it does not have a status pin.
Any ideas appreciated! Thank you!
Last edited: