---
title: "Solar sensor node: the energy budget"
heading: Sizing a solar panel and battery for a node that must never go dark, the daily harvest from a clear-sky profile, the load's daily energy, the balance on clear and cloudy days, the trade between panel and battery, and an hour-by-hour simulation that finds the smallest panel for a worst-case week
eyebrow: D13 · Lesson 2 · Solar sensor node
xp: 40
---

::: hook
A solar sensor node has to keep working through the night and through a run of gray days, on energy it gathered earlier. Too small a panel and the battery drains a little every day until the node dies in winter; too small a battery and it dies on the third cloudy day however big the panel is. Adding up energy per day gives a first answer in a minute. An hour-by-hour simulation of a worst-case week gives the real one, because the battery fills up and wastes sunshine, and it empties at night.
:::

::: play
- The battery's energy over a week for a panel of peak power P, a steady load L and weather factor k (1 for clear, 0.1 for heavy cloud), starting full. This sketch doesn't stop the battery at full or empty; the dashed lines show where it really would.
- Each day the panel adds a half-sine of energy around noon and the load takes a straight-line slope. Over a day the panel delivers $0.9 \times kP \times 24/\pi$ Wh.
- If the line drifts down day by day, the panel is too small for that weather. Find the panel that holds it level on clear days, then lower k.
:::

```sim plot
view: graph
x: t = 0..168
y: E = -10..30
slider: P = 0.1..5 step 0.05 = 0.6
slider: L = 5..300 step 5 = 120
slider: k = 0.1..1 step 0.05 = 1
slider: B = 1..25 step 1 = 10
plot: B + 0.9*k*P*(floor(t/24)*24/pi + step((t - 24*floor(t/24)) - 6)*step(18 - (t - 24*floor(t/24)))*(12/pi)*(1 - cos(pi*((t - 24*floor(t/24)) - 6)/12)) + step((t - 24*floor(t/24)) - 18)*24/pi) - L*t/1000 | samples: 1200 | label: stored energy, Wh (t in hours)
plot: B + 0*t | dashed | color: ch3 | label: full
plot: 0*t | dashed | color: ch4 | label: empty
readout: daily balance, Wh = 0.9*k*P*24/pi - 24*L/1000 | digits: 3
readout: hours a full battery lasts in the dark = 1000*B/L | digits: 3
```

::: predict
## A node draws an average of 120 mW. With a 5 Wh battery, roughly how long can it run in complete darkness?

- [ ] About 4 hours
- [x] About 42 hours
- [ ] About a week
- [ ] About a month

::: reveal
120 mW is 2.88 Wh a day, so 5 Wh lasts 5/0.12 = 41.7 h, under two days. Three cloudy days in a row would empty it even with some harvest, which is why the battery has to be sized for the worst stretch of weather, and the panel for recharging afterward.
:::
:::

::: nameit
## Daily energy

A clear day's sunshine on a fixed panel is roughly a half-sine from 6:00 to 18:00, so a panel of peak power P (watts at full sun) collects

$$E_\text{day} = \eta kP \times \frac{24}{\pi}\ \text{Wh}$$

about 7.6 peak-hours, with η the charger and battery efficiency (0.9 here) and k the weather factor. Real figures depend on latitude, season and tilt, and are tabulated as **peak sun hours**. The load takes $24\bar P$ Wh a day.

## Two sizes

- **Panel:** on the days you design for, $\eta kP \times 24/\pi \ge 24\bar P$, so $P \ge \pi\bar P/(\eta k)$.
- **Battery:** enough for the longest dark stretch, $N \times 24\bar P$ for N days, divided by the usable depth of discharge.

The two trade against each other: a bigger battery rides out a bad week with less panel, but the panel must still break even over the season, or the node drains slowly however big the battery is.

## Simulating

The daily sums miss two things: a full battery wastes sunshine, and an empty battery stops the node even if tomorrow is sunny. Stepping hour by hour, adding harvest, subtracting load and clipping to the battery's range, catches both. Run it for the worst week you design for, starting from the charge the battery would have then, not from full.
:::

::: example
## What panel keeps a 120 mW node level on clear days, and on days with k = 0.1?

::: solution
1. **Clear days.**
   $$\begin{aligned} P &= \frac{\pi\bar P}{\eta k} \\ &= \frac{\pi \times 0.12}{0.9}\ \text{W} \\ &= 0.42\ \text{W} \end{aligned}$$
2. **Days with k = 0.1.**
   $$\begin{aligned} P &= \frac{\pi \times 0.12}{0.9 \times 0.1}\ \text{W} \\ &= 4.2\ \text{W} \end{aligned}$$
3. **Let the battery cover bad days.** Designing for permanent heavy cloud would be extravagant, which is where the battery comes in: it carries the node through a few bad days, and the panel only has to refill it afterward.

**Answer:** 0.42 W on clear days, and 4.2 W at k = 0.1.
:::
:::

::: example
## The lab's simulation finds the smallest panel for a week of two clear days, three at k = 0.1 and two clear, starting full. What does it give for 5, 10 and 20 Wh batteries?

::: solution
1. **Run the simulation.** The smallest panels it finds are
   $$\begin{aligned} 5\ \text{Wh:}\ &2.46\ \text{W} \\ 10\ \text{Wh:}\ &0.38\ \text{W} \\ 20\ \text{Wh:}\ &0.006\ \text{W} \end{aligned}$$
2. **The 10 Wh result.** 0.38 W is below the 0.42 W a clear day needs to break even, so that node would drain over a sunny month.
3. **The 20 Wh result.** 20 Wh simply lasts the whole week on its own.
4. **What to simulate instead.** A one-week run that starts full flatters a big battery; simulate a whole season, or start the bad week at the charge a long run leaves.

**Answer:** 2.46 W, 0.38 W and 0.006 W, though the last two are misleading because a one-week run that starts full flatters a big battery.
:::
:::

::: check
kind: python

## Simulate the battery hour by hour and find the smallest panel for a bad week.

```python starter
import math

def clear_sky(h):
    # The fraction of the panel's peak power during hour h of the day (0 to 23), taken at the
    # hour's middle: sin(pi*(h + 0.5 - 6)/12) from 6:00 to 18:00, and 0 at night.
    return 0.0

def simulate(panel_w, battery_wh, load_w, weather, eta=0.9, start=1.0):
    # weather has one factor per day (1 for clear, 0.1 for heavy cloud). The battery starts at
    # start*battery_wh. Each hour: add eta*panel_w*weather[day]*clear_sky(h), subtract load_w
    # (both in Wh for the hour), then clip to the range 0 to battery_wh. Return the charge at the
    # end of every hour, as a list.
    return []

def smallest_panel(battery_wh, load_w, weather):
    # The smallest panel, to within 0.001 W, for which the charge never falls to 0 during the
    # simulation. Search between 0 and 50 W by bisection.
    return 0.0
```

```python solution
import math

def clear_sky(h):
    return max(0.0, math.sin(math.pi*(h + 0.5 - 6)/12)) if 6 <= h < 18 else 0.0

def simulate(panel_w, battery_wh, load_w, weather, eta=0.9, start=1.0):
    soc, out = start*battery_wh, []
    for k in weather:
        for h in range(24):
            soc += eta*panel_w*k*clear_sky(h) - load_w
            soc = min(battery_wh, max(0.0, soc))
            out.append(soc)
    return out

def smallest_panel(battery_wh, load_w, weather):
    lo, hi = 0.0, 50.0
    while hi - lo > 0.001:
        mid = (lo + hi)/2
        if min(simulate(mid, battery_wh, load_w, weather)) > 0:
            hi = mid
        else:
            lo = mid
    return hi
```

```python tests
assert clear_sky(5) == 0 and clear_sky(18) == 0 and abs(clear_sky(6) - math.sin(math.pi/24)) < 1e-12, "Night hours give 0, and 6:00 to 7:00 is taken at 6:30."
assert abs(sum(clear_sky(h) for h in range(24)) - 1/math.sin(math.pi/24)) < 1e-9, "A clear day's twelve hours add up to 7.661 peak-hours."
_s = simulate(0.0, 10.0, 0.5, [1])
assert len(_s) == 24 and abs(_s[0] - 9.5) < 1e-12 and _s[-1] == 0.0, "With no panel the battery loses the load every hour and stops at empty."
_t = simulate(1.0, 100.0, 0.0, [1], start=0.0)
assert abs(_t[-1] - 0.9/math.sin(math.pi/24)) < 1e-9, "A 1 W panel on a clear day stores 0.9 × 7.661 = 6.895 Wh."
assert max(simulate(10.0, 5.0, 0.0, [1, 1])) == 5.0, "The battery can't hold more than its capacity."
_week = [1, 1, 0.1, 0.1, 0.1, 1, 1]
_p = smallest_panel(5.0, 0.12, _week)
assert min(simulate(_p, 5.0, 0.12, _week)) > 0 and min(simulate(_p - 0.002, 5.0, 0.12, _week)) == 0, "Just below the answer the battery empties, and at the answer it doesn't."
assert abs(_p - 2.456) < 0.003, f"For 120 mW through this week with a 5 Wh battery the panel must be about 2.456 W; got {_p:.4f}."
```

::: right
Right. Your simulation clips at full and empty, and your search finds the 2.456 W panel that just survives the week.
:::
:::

::: check
given: P = 0.5 | 1 | 2 | 5; k = 0.2 | 0.5 | 1
answer: P*k*24/pi
unit: Wh
tolerance: 0.5%

## A {P} W panel sees weather factor {k}, with a half-sine of sun from 6:00 to 18:00. How much energy does it deliver in a day, before charging losses?

::: right
Right. $kP \times 24/\pi$ = {answer} Wh.
:::

::: misses
- [P*k*12] The sun isn't at its peak for all twelve hours: the half-sine averages $2/\pi$ of its peak.
- [any] Not yet. Peak power times about 7.64 peak-hours, times k.
:::
:::

::: check
given: L = 10 | 50 | 200; k = 0.3 | 0.6 | 1
answer: pi*L/1000/(0.9*k)
unit: W
tolerance: 0.5%

## A node draws {L} mW on average, and charging is 90% efficient. What panel breaks even on days with weather factor {k}?

::: right
Right. $\pi\bar P/(\eta k)$ = {answer} W.
:::

::: misses
- [pi*L/1000/k] Include the 90% efficiency.
- [any] Not yet. Daily harvest equals daily load.
:::
:::

::: check
given: N = 2 | 3 | 5; L = 10 | 50 | 200; D = 0.5 | 0.8
answer: N*24*L/1000/D
unit: Wh
tolerance: 0.5%

## A node draws {L} mW and must run {N} days with no sun, using at most {D} of the battery's capacity. What capacity does it need?

::: right
Right. $N \times 24\bar P/\text{DoD}$ = {answer} Wh.
:::

::: misses
- [N*24*L/1000] Only part of the capacity is usable: divide by the depth of discharge.
- [any] Not yet. Energy for N days, over the usable fraction.
:::
:::

::: check
kind: parts
given: A = 25 | 50 | 100; eta = 15 | 20; H = 1 | 3 | 5

## A solar panel of {A} cm² with {eta}% efficiency gets {H} peak-sun-hours a day (1 kW/m² equivalents).

::: part
answer: A*1e-4*eta/100*1000*H
unit: Wh
tolerance: 0.5%

### How much energy does it harvest per day?

::: misses
- [A*eta/100*1000*H] Convert cm² to m².
- [any] Not yet. Area × efficiency × 1000 W/m² × hours.
:::
:::

::: part
answer: A*1e-4*eta/100*1000*H/24*1e3
unit: mW
tolerance: 0.5%

### What average power is that over 24 hours?

::: misses
- [A*1e-4*eta/100*1000*H*1e3] Spread the day's energy over 24 hours.
- [any] Not yet. Daily energy ÷ 24 h.
:::
:::

::: right
Right. The node's average consumption, after charging losses, must stay below that.
:::
:::

::: check
given: C = 500 | 1000 | 2000; Ia = 0.1 | 0.5 | 2
answer: C/Ia/24
tolerance: 0.5%

## A battery of {C} mAh feeds a node averaging {Ia} mA with no sun at all. How many days does it last?

::: right
Right. {answer} days of autonomy.
:::

::: misses
- [C/Ia] That's in hours; divide by 24.
- [any] Not yet. Capacity ÷ current, in days.
:::
:::

::: explain
## Why can't a large enough battery make up for a panel that's too small?

::: model
A battery only stores energy; it doesn't make any. If the panel harvests less than the node uses on an average day, the battery loses a little every day, and however large it is, it eventually runs down; a bigger battery only delays that. The battery's job is to cover the gap between when energy arrives and when it's used, through each night and through a few bad days, while the panel has to supply at least the node's total consumption over the season. Sizing needs both: the panel from the long-run energy balance and the battery from the longest stretch the node must ride out.
:::
:::

::: practice

::: check
given: P = 0.1 | 20; k = 0.05 | 0.8
answer: P*k*24/pi
unit: Wh
tolerance: 0.5%

## Daily harvest: {P} W at k = {k}?

::: misses
- [any] $kP \times 24/\pi$.
:::
:::

::: check
given: N = 1 | 7; L = 2 | 500; D = 0.3 | 0.9
answer: N*24*L/1000/D
unit: Wh
tolerance: 0.5%

## Battery for {N} dark days at {L} mW, depth {D}?

::: misses
- [any] $24N\bar P/\text{DoD}$.
:::
:::
:::
