Embedded Systems

One problem, taken all the way through: a small board on a coin cell reads a button and a temperature, blinks an LED, logs to flash, talks to a radio chip, and has to last a year. Every lesson follows the same five steps as the controls lab, and the last two carry the ideas to a bike light.

1 · Arrive Read a button reliably, though its contact bounces and an unconnected pin reads nonsense. about 7 min

2 · Reactivate A digital pin only asks one question, is the voltage high or low? Everything here is about making that answer mean what you pressed.

3 · Inform High, low and in between

A pin reads 1 above a threshold and 0 below it. A button only connects the pin to ground, so a pull-up resistor has to bring it back up when released; without one the pin floats and reads whatever couples into it. When the contact closes it bounces for a few ms. Three fixes: a Schmitt trigger input (two thresholds, so a noisy slow edge can't cross back and forth), an RC filter that smooths the bounces, and software debounce, which only accepts a level once it has held for a few ms.

4 · Process

Try it

You can now: read a button with a pull-up, and stop bounce with a filter and a Schmitt input, or with software debounce.

1 · Arrive Turn a voltage into a number with an ADC, and see what its bits, reference and averaging do to the reading. about 8 min

2 · Reactivate A digital input gives one bit. An ADC gives many, by comparing the voltage with a reference. Builds on: Digital input

3 · Inform Steps of one LSB

An N-bit ADC splits 0 to the reference voltage into 2N steps. One step is one LSB; the reading can be off by up to half of one. Anything above the reference reads as the top code, so a bigger signal needs a divider. Noise bigger than an LSB makes readings jump between codes; averaging n readings shrinks the noise by √n, and with a little noise the average can even resolve steps smaller than one LSB.

4 · Process

Try it

Show the math

code = ⌊V / Vref · 2N⌋, LSB = Vref / 2N. The quantisation error is uniform over ±½ LSB, an RMS of LSB / √12. Averaging n readings with independent noise σ leaves σ / √n.

You can now: choose the bits and reference for a signal, and average away noise.

1 · Arrive Measure temperature with a thermistor in a divider, and turn the ADC's code back into °C. about 8 min

2 · Reactivate The ADC turns a voltage into a code. A thermistor turns temperature into a resistance; a divider turns that into a voltage. Builds on: Analog input

3 · Inform A divider and a curve

An NTC thermistor's resistance falls steeply as it warms (10 kΩ at 25 °C, about 33 kΩ at 0 °C). In a divider with a fixed resistor it makes a voltage; with the supply as the ADC's reference the code depends only on the two resistances (ratiometric), not on the battery. The curve isn't straight, so a straight line through two points is only right near them; the Beta formula follows it closely, and a small table of measured points does better still. The divider draws current whenever it's powered.

4 · Process

Try it

Show the math

R(T) = R₀ · exp(B (1/T − 1/T₀)), T in kelvin, R₀ = 10 kΩ, T₀ = 298.15 K, B ≈ 3950 K. V = Vcc · R / (R + Rf). Inverting: R = Rf · code / (2N − code), then 1/T = 1/T₀ + ln(R/R₀)/B.

You can now: read a thermistor through a divider, pick its resistor, and convert codes to °C with a formula or a table.

1 · Arrive Dim an LED by switching it fast (PWM), and make its brightness steps look even. about 7 min

2 · Reactivate A pin can only be on or off. Switch it fast enough and the eye sees the average. Builds on: Digital input

3 · Inform Duty, current and the eye

The resistor sets the LED's current: I = (Vcc − Vf) / R, with Vf ≈ 1.9 V for red. PWM switches it on for a fraction of each period, the duty; the average current, and what it costs the battery, is I × duty. Below about 100 Hz the eye sees it flicker. Perceived brightness is far from linear in light, so a slider mapped straight to duty bunches its steps at the dim end; gamma correction undoes that.

4 · Process

Try it

You can now: dim an LED with PWM, keep it from flickering, and space its steps with gamma.

1 · Arrive Know what goes in flash, in RAM and on the stack, and keep the stack from running into everything else. about 10 min

2 · Reactivate The node now samples, converts and logs. Each of those needs memory, and this chip has 8 KB of RAM. Builds on: Thermistor

3 · Inform Flash, RAM and the stack

Flash holds the program and anything const. RAM holds .data (initialised globals), .bss (zeroed globals, like a log buffer), the heap (malloc) growing up, and the stack growing down from the top. Each function call pushes a frame with its locals, so big local arrays and deep calls use stack, and an interrupt can push its frame at the deepest moment. Nothing stops the stack meeting the globals; it just corrupts them.

4 · Process

Try it

You can now: place data in flash or RAM on purpose, and size the stack for the worst case.

1 · Arrive Keep a log in flash without wearing it out. about 8 min

2 · Reactivate RAM is lost when the battery is changed. Flash keeps the log, but it can't be written like RAM. Builds on: Memory

3 · Inform Erase, then write

Erasing a page sets every bit to 1; writing can only clear bits to 0. So changing a byte twice needs the whole page erased, and pages only take about 10,000 erases. A ring log appends records through several pages and erases each one only when it comes round again, so the wear is shared and each page is erased once per lap.

4 · Process

Try it

You can now: log to flash with erase-before-write in mind, and spread the wear with a ring log.

1 · Arrive Send the reading to the radio chip over UART, I²C or SPI, and see what makes each one fail. about 10 min

2 · Reactivate Inside the chip a byte is eight wires at once. Between chips it goes one bit at a time, and both ends must agree when each bit is. Builds on: Analog input

3 · Inform Three ways to agree on a bit

UART has no clock wire: both ends agree on a speed (the baud rate), and the receiver times each bit from the start bit's edge, so the clocks must match within a few percent. I²C has a clock and a data wire shared by many chips; they only pull the lines low, and pull-up resistors bring them back up by charging the wires' capacitance, so a weak pull-up gives slow edges. SPI sends a clock with the data and drives both ways, so it is fast and simple, at the cost of more wires.

4 · Process

Try it

You can now: pick a bus, and say what limits UART's clocks, I²C's pull-ups and SPI's wire count.

1 · Arrive Make the node last a year on a coin cell by budgeting every microamp. about 12 min

2 · Reactivate Every part so far draws current. The ADC, the LED, a flash write and the radio take milliamps for milliseconds; sleeping takes microamps all the time. Builds on: Thermistor, LED and PWM, Flash storage, Buses

3 · Inform Charge per wake, current all the time

A coin cell holds 225 mAh. The node's average current is its sleep current plus each phase's charge (current × time) divided by the time between wakes. A year is 8760 hours, so the average has to stay under about 25 µA. The cell also has internal resistance that grows as it empties: a pulse of a few mA makes the voltage sag, and late in its life the sag crosses the brown-out level and the chip resets, with charge still left. A capacitor across the cell supplies short pulses itself.

4 · Process

Try it

Show the math

Iavg = Isleep + Σ Ik tk / T. Life = capacity / Iavg. Sag during a pulse of length tp with a capacitor C: I · R · (1 − e−tp/RC).

You can now: budget a battery from sleep current and charge per wake, and keep pulses from browning out an old cell.

1 · Arrive Run a bike light's modes from one button with a state machine, and tell a short press from a long one. about 6 min

2 · Reactivate On the node a button only needed counting. Here one button does two jobs, depending on how long it's held. Builds on: Digital input

3 · Inform A state machine

The light is always in one state (OFF, LOW, MID, HIGH, FLASH), and events move it between them: a short press steps to the next mode, a long press turns it off. The button is debounced as on the node; what's new is timing the press. Writing it as a table of states and events keeps every case visible.

4 · Process

Try it

You can now: write a button's behaviour as a state machine, and time presses to tell them apart.

1 · Arrive Read a Li-ion battery's voltage through a divider to warn and step down before it runs flat. about 6 min

2 · Reactivate The ADC can't read above its reference, and a battery can sag under load. Both matter here. Builds on: Analog input, Battery, Mode button

3 · Inform A divider for the battery

An 18650 Li-ion cell runs from 4.2 V full to 3.0 V empty, mostly near 3.7 V. The MCU's ADC tops out at its 3.3 V reference, so the battery goes through a divider, and the firmware multiplies back. At high current the cell sags, so the light steps down to LOW at 3.4 V for a last stretch of runtime, and cuts off at 3.0 V to protect the cell.

4 · Process

Try it

You can now: scale a battery voltage into the ADC's range, and use it to step down before the cell is empty.