D13 · Lesson 3 · Solar sensor node
Writing the node's firmware so it spends nearly all its time in deep sleep, waking on a timer interrupt, reading a TMP102 temperature sensor over I²C, reporting, and going back to sleep, with the average current measured on the board
The energy budget in lesson 2 assumed the node sleeps almost all the time. The firmware has to make that true. A loop that waits by spinning on a clock keeps the processor running at full current between readings; a node that sleeps deeply and lets a timer wake it uses a few percent of that. The structure is the one from C12: a timer interrupt sets a flag, the main loop does the work when the flag is set, and then the processor sleeps until the next interrupt.
- The node's firmware on the C12 board: every 100 ms (a minute, in a real node, scaled down so the run is short) the timer wakes the processor, which reads the TMP102 over I²C and prints the temperature in hundredths of a degree, then sleeps.
- The power panel shows the average current and the life of a 2000 mAh battery. Change
MODEto 0 (busy waiting) or 1 (light sleep) and run again to compare. - Move the sensor's temperature slider: the readings follow it, in steps of 1/16 °C, the sensor's resolution.
Predict
Awake, the node draws 2.05 mA; in deep sleep, 0.052 mA. If it's awake 0.5% of the time, how much longer does a battery last than if it never slept?
The average is 0.005 × 2.05 + 0.995 × 0.052 = 0.062 mA against 2.05 mA, a factor of 33. It isn't 200 because the sleep current itself, mostly the wake-up timer, now dominates: 0.052 mA is 84% of the average. That's the next thing to attack (C12).
Name it
The loop
- Set up a timer to interrupt at the reporting interval and enable deep sleep.
- In the interrupt handler, clear the timer's flag and set a
volatileflag; nothing slow belongs there (C12). - In the main loop, when the flag is set, read the sensor and report; then execute
wfito sleep until the next interrupt.
Reading the TMP102
The temperature register holds a 12-bit two's-complement value in its top 12 bits, in units of 1/16 °C. A register read on I²C is a write of the register number, a repeated start, and a read of two bytes: five bytes of 9 clocks each, 45 clocks, 450 µs at 100 kHz.
Measuring, not guessing
The budget's numbers come from the datasheet; the board's power panel measures what the firmware actually does. A forgotten busy loop, a peripheral left on or a sleep that never happens shows up at once as an average current far above the budget.
Worked example
The TMP102 reads 0x1780. What temperature is that?
Show the solution
- Shift right by 4. 0x1780 becomes
- Convert to degrees.
- A negative reading. −5.25 °C is −84 sixteenths, stored as so the register reads 0xFAC0.
- Decoding the sign. The code subtracts 4096 when bit 11 is set.
Answer 23.5 °C.
Worked example
How long is the processor awake per reading if an I²C register read at 100 kHz dominates, and what does that do to the average?
Show the solution
- Awake time. About 45 clocks at 10 µs, plus a little code:
- A reading every 100 ms. That's 0.45% awake:
- A reading a minute. The awake share falls to under 0.001%, and the average is the sleep current, 0.052 mA.
Answer about 0.45 ms awake per reading, which makes the average 0.061 mA at a reading every 100 ms and leaves it at the 0.052 mA sleep current at one a minute.
Check
This node reads the TMP102 every 100 ms but waits busily in between. Make it read from a 100 ms timer interrupt and deep-sleep in between, printing the same hundredths of a degree (−5.25 °C here).
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
The TMP102 counts in sixteenths of a degree. What signed 12-bit value does it report at −5.25 °C?
Check
A TMP102 register read takes 45 I²C clocks. How long is that at 400 kHz?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
A node is awake 0.5% of the time at 2.05 mA and sleeps the rest at 0.052 mA. What's its average current?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
Check
Firmware wakes every 600 s for 5 ms at 5 mA and sleeps at 5 µA otherwise.
(a)
What fraction of the time is it awake, in percent?
(b)
What is the average current?
Write units the way you normally would: 1.5 µA, 1500 nA and 0.0015 mA are the same answer.
Check
A node wakes for 10 ms at 3 mA and sleeps at 3 µA. At what wake interval do the awake and sleep charge per cycle become equal (so the sleep current makes up half the budget)?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Explain why
Why does the interrupt handler only set a flag instead of reading the sensor itself?
An interrupt handler runs with other interrupts held off, so anything slow inside it delays everything else that might need attention, such as a byte arriving on the UART or the next timer tick. An I²C read takes about half a millisecond of waiting on the bus, and printing or radio work takes longer. Setting a flag takes a few instructions; the main loop then does the slow work with interrupts enabled and goes back to sleep when it's done. This keeps the handler's timing predictable and the structure the same however much work each reading grows into.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.