How long will the battery last?
Average the current over a duty cycle and turn it into days of battery life. An easy embedded C problem (Low-power design techniques), run and graded in the…
easy · Low-power design techniques · low-power · battery · calculation
A sensor node repeats a cycle of phases (sleep, measure, transmit), each with a current and a duration. Battery life depends on the average current:
average = Σ (current × time) / Σ time
Write life_days(phases, n, capacity_mah, derate_percent): the usable capacity is capacity_mah reduced by derate_percent (temperature, ageing, self-discharge). Return the whole number of days the battery lasts (rounded down). Use 64-bit arithmetic: µA × ms adds up quickly.
Starter code
uint32_t life_days(const phase_t *phases, int n, uint32_t capacity_mah, uint32_t derate_percent) {
uint32_t total = 0;
for (int i = 0; i < n; i++) total += phases[i].microamps;
uint32_t avg_ua = total / n; // average of the currents
(void)derate_percent;
return capacity_mah * 1000u / avg_ua / 24u;
}Example tests
A sensor node
phase_t p[] = { { 3, 59000 }, { 2000, 900 }, { 45000, 100 } };
show(p, 3, 2000, 20);Expected output:
2000 mAh, 20% derating: 617 daysLearn it first
- Low-power design techniques · Battery budgets, duty cycling, sleep modes and wake sources.
More Low-power design techniques problems
- Water meter: will the battery last? · Work out a battery's life in days from the meter's sleep, wake-up and radio currents.