Development

» Int 06: Critical battery interrupt

This example shows how to set the Waspmote low battery detector interruption example.

Required Materials

1 x Waspmote
1 x MiniUSB wire
1 x Battery

Notes

(This example has been tested using a variable power supply, changing the voltage between 3 and 4.2 volts).
The states of the example are these:
1. Wait until the battery level is above the threshold value plus a safety margin. The red LED is ON, indicating that battery is low.
2. When the battery is "charged" the code follows the green LED is ON during 3 seconds
3. When the green LED is OFF, Waspmote goes to sleep, it wakes up when a low battery interruption is detected.

WARNING: Because the critical battery detector maintains a low level on the TXD1 pin, while the battery charge does not go above a set charge threshold (higher than the discharge threshold), it is not possible to re-enable the interruptions related with the TXD1. If they were re-enabled it would be possible to enter into an infinite loop because the microcontroller would always detect the low battery interrupt, as the level is below the threshold.

In systems in which the charge can be recovered (eg. charging through the solar panel), it is recommended to read the critical battery monitoring pin value every ‘x’ seconds, so that when a change in the value of this pin is detected, passing from low to high level, the interruptions in the TXD1 are activated again.

- This example can be executed in Waspmote v12

Code

/*
 *  ------ [Int_06] Setting battery level interrupt example -------- 
 *
 *  Explanation: This example shows how to set the  Waspmote low battery
 *  detector interruption example
 *
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *
 *  Version:                0.1
 *  Design:                 Marcos Yarza  
 *  Implementation:         Marcos Yarza
 */

#define THRESHOLD 3.4

void setup()
{ 

}


void loop()
{
  ///////////////////////////////////////////////////////////
  // 1. wait until the battery level is above
  // the threshold value plus a safety margin.
  // The red LED is ON, indicating that battery is low
  ///////////////////////////////////////////////////////////
  Utils.setLED(LED0,LED_ON);
  while( PWR.getBatteryVolts() < THRESHOLD + 0.15)
  {
    // do nothing
    // just wait to charge the battery
  }
  Utils.setLED(LED0,LED_OFF);
  ///////////////////////////////////////////////////////////
  // 2. When the battery is "charged" the code
  //    follows the green LED is ON during 3 seconds
  ///////////////////////////////////////////////////////////
  Utils.setLED(LED1,LED_ON);
  delay(3000);
  PWR.setLowBatteryThreshold(THRESHOLD);
  enableInterrupts(BAT_INT);
  Utils.setLED(LED1,LED_OFF);
  
  ///////////////////////////////////////////////////////////
  // 3. When the green LED is OFF, Waspmote goes to sleep, it
  //    wakes up when a low battery interruption is detected
  ///////////////////////////////////////////////////////////
  PWR.sleep(ALL_OFF);
  
  if( intFlag & BAT_INT )
  {
    disableInterrupts(BAT_INT);
    Utils.setLED(LED0,LED_ON);
    delay(1000);
    Utils.setLED(LED0,LED_OFF);
    // Clear flag
    intFlag &= ~(BAT_INT);
  }
}

Output

Only red and green LEDs are used as output in this example.
1. Red LED is ON, indicating that battery is low.
2. When the battery is "charged" the code follows the green LED is ON during 3 seconds
3. When the green LED is OFF, Waspmote goes to sleep, it wakes up when a low battery interruption is detected.

Quick Publish: