Learning Goals 5 min
Combine sleep modes (L04-37) and battery selection (L04-38) into the classic IoT field-device pattern: wake, sample, transmit, sleep. The complete blueprint for any battery-powered sensor that needs to last months or years. By the end of this lesson you will:
- Implement the wake / sample / transmit / sleep cycle on a Pro Mini + sensor + radio.
- Power down all peripherals between cycles (sensor + radio + chip) for true µA average draw.
- Estimate runtime from measured current samples and pick the right wake interval for your use case.
Warm-Up 10 min
Hardware: Pro Mini 3.3 V (or any low-power AVR), DHT22 (or similar), nRF24 / RFM69 / LoRa radio, battery + monitor.
The pattern
while forever: wake from sleep power on the sensor (MOSFET switch) read sensor (~20 ms) power off sensor power on the radio publish (~50 ms) power off radio sleep for the configured interval
Each cycle takes ~100 ms of active time. Sleep takes 15 min – 1 hour depending on application. Active time is 0.005–0.01% of the cycle. Average current drops from ~30 mA to ~50 µA.
New Concept · The wake/sample/transmit cycle 25 min
Switching peripherals via MOSFET
Many sensors draw 1–5 mA even when idle. Cutting their power in sleep saves a lot. Use a P-channel MOSFET on the high side: GPIO HIGH = peripheral off; LOW = on.
Wiring:
- Battery + → P-MOSFET source.
- MOSFET drain → sensor VCC.
- MOSFET gate → 10 kΩ pull-up to battery + + Arduino GPIO.
The structured loop
#include <LowPower.h>
const int SENSOR_POWER = 5; // MOSFET gate (LOW = on)
const int RADIO_POWER = 6;
const int WAKES_PER_SAMPLE = 7; // 7 × 8 s ≈ 56 s; or 450 × 8 s ≈ 1 hour
int batteryThreshold = 3300; // mV
void sleepInterval() {
for (int i = 0; i < WAKES_PER_SAMPLE; i++) {
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
}
void wakeAndDoWork() {
// 1. Sensor on
digitalWrite(SENSOR_POWER, LOW);
delay(50); // sensor wake-up time
// 2. Sample
float temp = readTemperatureSensor();
// 3. Sensor off
digitalWrite(SENSOR_POWER, HIGH);
// 4. Battery check
int vbat = readBatteryVoltage_mV();
if (vbat < batteryThreshold) {
// refuse to transmit if battery is low
return;
}
// 5. Radio on
digitalWrite(RADIO_POWER, LOW);
delay(20); // radio warm-up
radio.begin();
radio.send(makePacket(temp, vbat));
radio.end();
digitalWrite(RADIO_POWER, HIGH);
}
void setup() {
pinMode(SENSOR_POWER, OUTPUT);
pinMode(RADIO_POWER, OUTPUT);
digitalWrite(SENSOR_POWER, HIGH);
digitalWrite(RADIO_POWER, HIGH);
}
void loop() {
wakeAndDoWork();
sleepInterval();
}Pre-wake checks
Sometimes you want to do nothing if conditions aren't right. Common pre-wake gates:
- VBAT too low → skip transmit, only beep low-batt warning.
- Sensor reading hasn't changed significantly → skip transmit (saves more battery, fewer packets to the gateway).
- Time of day (e.g. 9 PM – 7 AM "sleep mode" — only one reading per hour).
Reducing transmit cost
The radio is usually the biggest energy sink. Strategies:
- Higher SF on LoRa = longer per-packet time but longer range. Pick based on coverage.
- Compress payloads. Send a 4-byte struct, not a 30-byte JSON.
- Send only on change. Cache last sent value; transmit when delta exceeds threshold.
- Aggregate. Sample 12 times an hour, send hourly with 12 values.
Worked Example · Build a 2-year temperature logger 25 min
Bill of materials
- Pro Mini 3.3 V (~3 mA active, ~3 µA deep sleep).
- DHT22 (~1 mA active, draws ~0 µA off).
- RFM69CW LoRa-like radio (~30 mA tx peak, ~0.1 µA off).
- P-MOSFET (SI2333DDS or similar).
- 3 × AA NiMH (3.6 V, 2500 mAh).
Math
- Active per cycle: 100 ms × ~30 mA ≈ 0.8 µAh.
- Sleep per cycle: ~3 µA × 15 minutes = 0.75 µAh.
- Per cycle total: 1.5 µAh.
- Cycles per year: 365 × 24 × 4 = 35040.
- Per year: 35040 × 1.5 µAh = 53 mAh.
2500 mAh / 53 mAh per year = 47 years. Battery shelf life will limit you to ~10 years long before capacity is consumed.
Real-world results
Reality is messier: leak currents, parasitic loads, charge inefficiency on the boost regulator. Field deployments of this pattern typically last 2–5 years on AA cells. That's still phenomenal — battery becomes the device's effective lifetime.
Verify in practice
- Measure µA in sleep.
- Measure mA during active cycle + duration.
- Compute weighted average.
- Divide battery mAh by average → years of life.
Basic 5 min
Goal: Build a wake-every-30-seconds blinker that flashes for 50 ms then sleeps. Measure average µA.
Here is the pattern to aim for:
1. Think first (on paper, no typing yet):
- The watchdog can sleep at most 8 s at a time. How close to 30 s can you get?
- The average current is a weighted average of the awake current and the asleep current. Write the sum and predict the answer.
- Which parts of your board keep drawing current while the chip sleeps?
2. Code it. Write the flash-then-sleep loop yourself, using today's LowPower calls.
3. Upload and observe. Measure the current while awake and while asleep, then work out the average.
It works if:
- The LED flashes about every 32 s.
- Your measured awake and asleep currents are written down with units.
- Your average-current calculation uses them correctly.
Challenge 1 5 min
Goal: Wake-and-publish over LoRa every hour. Build with Pro Mini + RFM95. Measure runtime on 2 × AA over a week. Extrapolate to years.
1. Think first (on paper, no typing yet):
- How many 8 s sleeps make an hour?
- What must be switched off during sleep, and what must be started again after waking?
- How will you test this without waiting an hour for each packet?
2. Code it. Start from the structured loop. Use your LoRa code from ARD-L04-18 for the radio.
3. Upload and observe. Test with a 1-minute interval first, then switch to an hour. Log the battery voltage each day.
It works if:
- The receiver gets one packet per interval, including the battery voltage.
- The sleep current is in the µA range.
- After a week you have a voltage trend and an estimate in years, and you can explain why the estimate is rough.
Challenge 2 5 min
Goal: Send-only-on-change. Cache the last published temperature. Send a new packet only when the difference exceeds 0.5 °C. Reduces packets by ~10× during stable weather.
1. Think first (on paper, no typing yet):
- What must the sketch remember between wakes? Does a global variable keep its value through powerDown?
- What test decides whether to send?
- Should the node still send now and then, even if nothing has changed? Why?
2. Code it. Start from your Challenge 1 sketch. Print a line on each wake saying whether it sent or skipped.
3. Upload and observe. Leave it steady, then hold the sensor in your hand to warm it.
It works if:
- While the temperature is steady, it skips and sends nothing.
- When it warms by more than 0.5 °C, it sends, and that value becomes the new comparison.
- You can compare the number of packets in an hour with and without the rule.
Challenge 3 · Estimate one of your project's runtime 10 min
- Pick a battery project.
- Measure active mA + duration per cycle.
- Measure sleep mA + duration per cycle.
- Compute weighted average.
- Divide battery mAh by average → years.
- Compare to the naive "always running" estimate.
Recap 5 min
Low-power sensor node = wake-sample-transmit-sleep with peripherals MOSFET-switched. Average current in µA, runtime in years. The pattern of every commercial battery-powered IoT device. Tomorrow we add solar and the device runs forever.
- Wake/sample/transmit/sleep cycle
- The four phases of a low-power node. Active 0.01–1% of the time; sleep the rest.
- Peripheral power gating
- Using a MOSFET to cut power to sensors / radios when not in use.
- P-channel MOSFET (high-side switch)
- Switches VCC to a peripheral. Gate held HIGH = off; LOW = on. Cheap, low loss.
- Sensor wake-up time
- How long after power-on before a sensor gives valid readings. DHT22 ~50 ms; BMP280 ~10 ms.
- Send-on-change
- Transmit only when the value differs significantly from the last sent value. Reduces radio time.
- Field-device pattern
- The industry-standard low-power node template. Sleep most of the time; do work briefly; sleep again.
Extra Mission 5 min
Part 1 — Design a sensor node for a place nobody visits
Some sensors live where nobody checks them for months. On paper, design a node for one such place. It might be a school garden bed, a bird box or a shed.
Your design must include:
- What it measures, and how often.
- How the data leaves the node, and how often it sends.
- Which parts are switched off between readings, and how.
- The battery, and your estimate of how many months or years it lasts.
Part 2 — Make it
Build the core wake–measure–sleep cycle. Power your sensor through a MOSFET so it is fully off while the chip sleeps. Wake, switch the sensor on, read it, print the value, switch it off, sleep. Measure the current in both states and work out the battery life.
Bring back next class: your uploaded sketch, a photo of the circuit and your current readings with the battery-life calculation.