Power 04: Getting battery level
This example shows how to get the remaining battery level.

Required Materials

1 x Waspmote 1 x MiniUSB wire 1 x Battery

Notes

- WARNING: This example is suitable for rechargeable batteries only. Non-rechargeable batteries always show a plain discharge curve, so it is not possible to know how much power is left. - This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [PWR_4] Getting Battery Level Example --------
3
*
4
* Explanation: This example shows how to get the remaining battery
5
* level.
6
*
7
* WARNING: This example is suitable for rechargeable batteries only.
8
* Non-rechargeable batteries always show a plain discharge curve,
9
* so it is not possible to know how much power is left.
10
*
11
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
12
* http://www.libelium.com
13
*
14
* This program is free software: you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation, either version 3 of the License, or
17
* (at your option) any later version.
18
*
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
23
*
24
* You should have received a copy of the GNU General Public License
25
* along with this program. If not, see .
26
*
27
* Version: 3.0
28
* Design: David Gascón
29
* Implementation: Marcos Yarza
30
*/
31
32
char WARNING_MESSAGE[] =\
33
"\n-------------------------------------------------------------------\n"\
34
"WARNING: This example is suitable for rechargeable batteries only.\n"\
35
"Non-rechargeable batteries always show a plain discharge curve,\n"\
36
"so it is not possible to know how much power is left.\n"
37
"-------------------------------------------------------------------\n";
38
39
40
void setup()
41
{
42
// Open the USB connection
43
USB.ON();
44
USB.println(F("PWR_4 example"));
45
46
// show warning
47
USB.println( WARNING_MESSAGE );
48
}
49
50
51
52
void loop()
53
{
54
// Show the remaining battery level
55
USB.print(F("Battery Level: "));
56
USB.print(PWR.getBatteryLevel(),DEC);
57
USB.print(F(" %"));
58
59
// Show the battery Volts
60
USB.print(F(" | Battery (Volts): "));
61
USB.print(PWR.getBatteryVolts());
62
USB.println(F(" V"));
63
64
delay(5000);
65
}
Copied!

Output

1
H#
2
PWR_4 example
3
-------------------------------------------------------------------
4
WARNING: This example is suitable for rechargeable batteries only.
5
Non-rechargeable batteries always show a plain discharge curve,
6
so it is not possible to know how much power is left.
7
-------------------------------------------------------------------
8
Battery Level: 100 % | Battery (Volts): 4.2290000915 V
9
Battery Level: 100 % | Battery (Volts): 4.2290000915 V
10
Battery Level: 100 % | Battery (Volts): 4.2290000915 V
11
Battery Level: 100 % | Battery (Volts): 4.2290000915 V
Copied!
Last modified 1yr ago