A microcontroller only understands numbers. The world speaks in degrees, centimeters, grams, and gauss. A sensor is the translator — a transducer that turns a physical property into a voltage, and a clever little circuit that turns that voltage into a number your code can act on.
You have built a small robot. It rolls forward on two motors, and it is supposed to do two simple things: stop before it slams into a wall, and shut down if its motor driver chip overheats. Both feel trivial — until you sit down to write the code.
Inside the robot is a microcontroller. Its world is brutally narrow. It cannot perceive a wall. It cannot feel heat. The only thing it can do is read its analog-to-digital converter (ADC) — a peripheral that measures a voltage on a pin and hands your program back an integer. On a 12-bit-ish hobby ADC that integer runs from 0 to 1023: 0 means "the pin is at 0 V," 1023 means "the pin is at the reference voltage (say 5 V)," and everything in between is linearly spaced.
So the robot's entire sensory universe is: "give me a number between 0 and 1023." The wall is at 1.7 meters. The chip is at 85 °C. Neither of those is a number between 0 and 1023. The whole job of this chapter is to build the bridge:
Read that chain in both directions. Going down, physics produces a number. Going up, your code reverses the physics to recover a real measurement. Every sensor in this chapter — thermistor, RTD, thermocouple, TMP36, ultrasonic ranger, strain gauge, Hall-effect chip — is just a different way of filling in the first two boxes. The bottom three boxes barely change.
A thermistor's resistance falls as it heats. Drop it in a 5 V divider and the node voltage rises with temperature; the ADC turns that voltage into a count. Slide the temperature and watch the whole chain move at once.
A transducer is any device that converts energy from one form to another. A loudspeaker is a transducer (electrical → sound). A sensor is a transducer pointed the other way: it converts a non-electrical quantity into an electrical one we can measure. The art is in the second half — signal conditioning — which takes the often tiny, noisy, nonlinear, or awkwardly-scaled raw output and shapes it into a voltage the ADC can use.
Sensors fall into two big families. Resistive sensors change their resistance with the stimulus: thermistors (temperature), photoresistors (light), strain gauges (force), potentiometers (position). They produce no voltage on their own — you must excite them with a known current or voltage. Self-generating sensors produce a voltage directly: thermocouples (Seebeck voltage from a temperature gradient), photodiodes (photocurrent), piezoelectric elements (charge from pressure). These need no excitation but often need amplification.
For a resistive sensor with resistance R, the standard conditioning circuit is a divider with a fixed resistor R₁ (often chosen equal to the sensor's nominal value R₀). If R₁ sits on top (connected to Vin) and the sensor R sits on the bottom (to ground), the node between them reads:
If instead the sensor is on top and the fixed resistor on the bottom, swap which resistance appears in the numerator. The choice flips the sign of the response — whether Vout rises or falls with the stimulus — which matters a lot when you wire it up. Either way, a changing R produces a changing Vout between 0 and Vin.
Take Vin = 5 V, fixed R₁ = 4.7 kΩ on top, sensor R on the bottom. If the sensor happens to equal 4.7 kΩ:
Now suppose the stimulus pushes the sensor up to 15.95 kΩ (a real number we will derive in the thermistor tab):
Wait — that is the sensor-on-bottom wiring, so a bigger sensor resistance gives a bigger Vout. With the sensor on top instead, the same 15.95 kΩ gives Vout = 5·4700/20650 = 1.14 V. Same components, opposite slope. You must know which resistor is on top before you can convert a count back to a measurement.
Sweep the sensor resistance R from 100 Ω to 100 kΩ. The curve shows Vout for the sensor-on-bottom divider; the dot marks your current R. Notice the slope is steepest — the divider most sensitive — where R is near the fixed resistor R₁.
Before trusting any sensor reading, you must separate four words that everyday speech jumbles together. They are not synonyms, and confusing them produces robots that act with false confidence.
Accuracy — how close a reading is to the true value. A scale that reports 85.7 kg for a 92.1 kg person is inaccurate; it has a systematic offset of −6.4 kg.
Precision (repeatability) — how close repeated readings are to each other. If that same scale reads 85.7 kg every single time, it is highly precise — just precisely wrong.
Resolution — the smallest change the system can distinguish. A 10-bit ADC over 5 V has 1024 steps, so one step (one LSB) is 5 V/1024 ≈ 4.9 mV. Finer voltage changes than that are invisible.
Calibration — the act of correcting accuracy by comparing against a known reference and storing the correction. Precision you cannot fix in software; accuracy you usually can.
A precise-but-inaccurate sensor is actually the good case, because a fixed offset is calibratable: measure the error once against a trusted reference and subtract it forever after. An imprecise sensor — one that scatters randomly — cannot be rescued so easily; you can only average many readings to beat down the noise.
Resolution at the ADC is voltage, but what you care about is resolution in degrees or centimeters. Suppose a temperature sensor produces 10 mV/°C and feeds a 10-bit, 5 V ADC. One ADC count is 4.9 mV, which corresponds to:
So even a perfect sensor cannot resolve finer than about half a degree on this ADC — not because the sensor is bad, but because the digitizer quantizes. Want finer? Amplify the signal before the ADC, or use more ADC bits.
Many sensors are nonlinear, so converting count → value with a formula is expensive on a small microcontroller. The classic trick is a lookup table: precompute the real value for a handful of counts, store them in flash, and linearly interpolate between entries. If count 300 means 20 °C and count 360 means 30 °C, then a reading of 330 maps to:
Cheap, fast, and accurate enough as long as the table is dense where the curve bends sharply.
A dartboard view. Bias shifts the whole cluster away from the bullseye (accuracy); spread scatters the darts (precision). Try high-bias/low-spread to see a "precisely wrong" sensor — the one you can calibrate.
A thermistor is a resistor engineered so its resistance changes strongly with temperature. The common kind is NTC (negative temperature coefficient): resistance falls as it heats. (PTC types rise, and are used more as resettable fuses than as thermometers.) Thermistors are pennies cheap and extremely sensitive — a 4.7 kΩ NTC can swing from ~16 kΩ at the freezing point of water down to a few hundred ohms when hot. That sensitivity is their gift; their nonlinearity is the price.
The resistance does not change linearly with temperature — it changes exponentially. A workable two-point model is the Beta (β) equation, where all temperatures are in kelvin (K = °C + 273.15):
Here R₀ is the resistance at a reference temperature T₀ (usually 4.7 kΩ at 25 °C = 298 K), and β is a material constant (a typical value is 3977 K). For lab-grade accuracy the three-term Steinhart–Hart equation 1/T = A + B·ln(R) + C·(ln R)³ fits the curve better, but Beta captures the essential exponential shape with a single number.
Vin = 5 V, fixed R₁ = 4.7 kΩ on top, NTC thermistor (R₀ = 4.7 kΩ, T₀ = 298 K, β = 3977) on the bottom. We want Vout at the freezing point, T = 0 °C = 273 K.
First the exponent:
Now the resistance:
Finally the divider (sensor on bottom):
And the sanity check at 25 °C: the exponent is β·0 = 0, so R = R₀ = 4.7 kΩ exactly, and Vout = 5·0.5 = 2.50 V. Freezing reads 1.14 V, room temperature reads 2.50 V — and the curve between them bends, which is exactly what the showcase below lets you feel.
The full chain, live. Left pane: the R(T) curve from the Beta equation, with your operating point marked. Right pane: the resulting Vout and the ADC count (10-bit, 0–1023). Slide temperature from −40 to +125 °C; change β and the fixed resistor to see how the designer trades sensitivity for range.
The thermistor was cheap and sensitive but bent like a pretzel. When you need accuracy and linearity — in a lab, an oven controller, an industrial process — you reach for two other temperature sensors that trade money for honesty.
A resistance temperature detector (RTD) exploits the fact that the resistance of a pure metal rises almost linearly with temperature. The gold standard is platinum: a Pt100 has 100 Ω at 0 °C. To first order:
with R₀ = 100 Ω and temperature coefficient α ≈ 0.003925 /°C for platinum. The resistance change is tiny — that is the cost of linearity — but it is beautifully predictable.
Compare the two technologies over the same 0–100 °C span. The platinum RTD crept from 100 Ω to 139.25 Ω — a clean, straight 39% rise. The thermistor from the last tab swung from roughly 16 kΩ down to a few hundred ohms over a similar range — a factor of fifty, but along a savagely curved path. That single comparison is the engineering tradeoff in numbers: the RTD is linear and accurate but barely moves (you need a sensitive front end), while the thermistor moves enormously but nonlinearly.
A thermocouple is two wires of dissimilar metals joined at one end. The junction generates a small voltage that depends on the temperature difference between the joined (hot) end and the open (cold) ends — the Seebeck effect:
where S is the Seebeck coefficient of the metal pair. A Type-K (chromel–alumel) couple has S ≈ 41 µV/°C and works from −200 °C all the way to about +1350 °C. That enormous range is why thermocouples own furnaces and kilns where thermistors and RTDs would simply melt.
A Type-K couple with its hot junction in a 1000 °C furnace and its terminals at 25 °C sees ΔT = 975 °C:
But notice the catch baked into V = S·ΔT: the voltage measures a difference, not the hot temperature itself. If you assumed the cold end was at 0 °C but it was really at 25 °C, your reading is off by 25 °C. So every thermocouple system needs cold-junction compensation: a second, ordinary sensor (often a TMP36 or thermistor) measures the terminal-block temperature, and firmware adds it back. The thermocouple gives you the difference; the cold-junction sensor supplies the reference.
Output versus temperature for three sensors on one axis. Toggle each on or off. Watch the thermistor curve bend while the RTD and TMP36 stay arrow-straight — linearity made visible. (Each curve is normalized so all three fit one frame.)
Thermistors, RTDs, and thermocouples all dump the curve-fitting, excitation, and compensation onto you. An integrated-circuit (IC) temperature sensor bakes all of that into silicon. You pay a few extra cents, and in return you get an output that is already linear, already in convenient units, and sometimes already digital. This is the "plug-and-play" corner of the cost/effort tradeoff.
The TMP36 outputs a voltage that rises a flat 10 mV for every degree Celsius, with a 500 mV offset so it can report below-zero temperatures on a single supply:
No Beta, no kelvin, no exponentials. Just read the pin and do one multiply and one subtract. It works from −40 to +125 °C with about ±2 °C accuracy — not lab grade, but more than enough to know your motor driver is overheating.
Your ADC reports a pin voltage of 0.750 V. Convert to temperature:
The reverse check: at 0 °C the TMP36 outputs 0.010·0 + 0.500 = 0.500 V. And a reading of 0.900 V means T = 100·0.9 − 50 = 40 °C. Compare the cognitive load to the thermistor: there we needed logs, an exponent, a divider, and a lookup table; here we needed a line.
The DS18B20 goes one step further and outputs a digital number over the 1-Wire bus — a single data line that can carry many sensors, each with a unique 64-bit address. It does its own analog-to-digital conversion internally, so your microcontroller never touches an analog voltage at all. Range is −55 to +125 °C with ±0.5 °C accuracy and up to 12-bit resolution, and the conversion-and-units problem disappears: you ask for the temperature and you receive degrees.
Drag temperature and read the output voltage off the straight line; the inverse formula T = 100·Vout − 50 is shown live. Compare the dead-straight TMP36 line to the thermistor's curve back in tab 3.
Back to the robot's first job: stop before the wall. Temperature was a resistance problem; distance is usually a timing problem. The dominant trick is time-of-flight: emit a pulse, measure how long the echo takes to come back, and multiply by the wave's speed.
An ultrasonic ranger (the ubiquitous HC-SR04) chirps a ~40 kHz burst of sound, then times the echo. Sound travels at roughly 340 m/s in room-temperature air. The pulse goes out to the wall and back, so the measured time covers twice the distance:
The echo returns 10 ms (0.010 s) after the chirp. With v = 340 m/s:
There it is — the "wall is 1.7 m away" from the opening problem, recovered from a 10 ms time interval. One subtlety: the speed of sound depends on temperature, rising from ~331 m/s at 0 °C to ~346 m/s at 25 °C. A precise ranger therefore reads a thermometer too and corrects v — sensors leaning on sensors.
IR optical (reflectance): an infrared LED shines forward and a photodiode or position-sensitive detector reads the reflection. Sharp's GP2Y0A modules triangulate the reflected spot's angle, giving a (nonlinear) voltage versus distance. Cheap and fast, but fooled by dark, glossy, or angled surfaces.
Capacitive RC sensing: a conductive plate forms a capacitor with its surroundings; a nearby hand or object changes that capacitance, which changes the time constant τ = RC of a charge/discharge circuit. Measure the timing shift and you have proximity with no moving parts and no contact — this is how touchscreens and many touch buttons work.
Slide the wall closer or farther and watch the echo pulse race out and back. The timeline shows the measured round-trip time; the readout divides by two to give the true distance. Change air temperature to nudge the speed of sound.
The last family of sensors measures mechanical and magnetic quantities — force, acceleration, magnetic field. Their conditioning circuits introduce one new idea beyond the simple divider: the Wheatstone bridge, the trick for reading a tiny resistance change with high precision.
A strain gauge is a thin foil resistor bonded to a surface. When the surface stretches (tension) the foil's wire lengthens and thins, raising its resistance; under compression it shortens and its resistance falls. The change is minuscule — fractions of a percent — far too small to read accurately from a plain divider, where it would be lost in the offset voltage.
The fix is the Wheatstone bridge: two voltage dividers fed from the same supply, with the output taken as the difference between their midpoints:
When the bridge is balanced — R₁/R₂ = R₄/R₃ — the two midpoints sit at the same voltage and Vout = 0. Now the genius: you set the bridge to balance with the gauge unstrained, so the big constant offset cancels itself out, and the meter reads only the change. A 0.1% resistance shift that was invisible in a divider becomes a clean, amplifiable signal around zero.
Vin = 5 V, all four arms start at 350 Ω (a common gauge value), so the bridge is balanced and Vout = 0. Tension raises the gauge arm R₃ by 0.5 Ω to 350.5 Ω:
A 0.14% resistance change produced 1.8 mV out of a 5 V supply — small, but sitting on zero rather than buried under 2.5 V, so an amplifier can blow it up cleanly. That is the entire reason load cells (bathroom scales, kitchen scales, industrial weighing) use bridges.
A MEMS accelerometer (like the MMA8452Q) is a microscopic spring-mass system etched into silicon. Acceleration deflects a tiny proof mass; the deflection changes the capacitance between interleaved comb fingers; on-chip electronics read that capacitance and report acceleration, usually over I²C. It is the same spring-mass-capacitor idea as before, just shrunk to micrometers.
The Hall effect: pass a current I through a thin conductive slab sitting in a magnetic field B perpendicular to it, and the field pushes the moving charges sideways, building a measurable voltage across the slab:
where n is charge-carrier density, e the electron charge, and d the slab thickness. The output is proportional to B, so a Hall sensor reads magnetic field strength directly — no contact, no moving parts. Switch-mode Hall chips snap their output high once B crosses a threshold (perfect for counting gear teeth or sensing a door magnet); linear-mode chips output a voltage proportional to B (for current sensing and position).
Drag the strain slider through tension and compression. The needle swings off zero as the gauge arm leaves its rest value; at balance it parks dead center.
Move the bar magnet toward the current-carrying slab. The Hall voltage VH grows with field strength. Flip between linear (proportional) and switch (threshold) modes.
Every sensor in this chapter solved the same opening problem — turn a physical quantity into a number a microcontroller can act on — and almost all of them did it with one of two circuits: a voltage divider (variable element, absolute reading) or a Wheatstone bridge (matched divider pair, differential reading). The physics in the front box changed; the bridge to the ADC barely did.
| Concept | Equation | Worked anchor |
|---|---|---|
| Voltage divider | Vout = Vin·R/(R₁+R) | R=R₁ → Vout = Vin/2 |
| ADC mapping | count = 1023·Vout/Vref | 2.5 V on 5 V ref → 512 |
| Thermistor (Beta) | R = R₀·eβ(1/T−1/T₀) | 0 °C → 16 kΩ → Vout 1.14 V |
| Steinhart–Hart | 1/T = A + B·lnR + C·(lnR)³ | lab-grade thermistor fit |
| Platinum RTD | R = R₀(1+αT) | 100 °C → 139.25 Ω |
| Thermocouple (Seebeck) | V = S·ΔT | Type-K, 41 µV/°C |
| TMP36 | T = 100·Vout − 50 | 0.75 V → 25 °C |
| Ultrasonic ToF | d = v·t/2 | 340·0.010/2 = 1.7 m |
| Hall effect | VH = −I·B/(n·e·d) | VH ∝ B |
| Wheatstone bridge | Vout=Vin[R₃/(R₃+R₄)−R₂/(R₁+R₂)] | balanced when R₁/R₂=R₄/R₃ |
| You want… | Pick | Because |
|---|---|---|
| Cheap, sensitive temp, hobby | NTC thermistor | pennies, huge swing — pay with nonlinearity |
| Linear, accurate temp, lab | Pt100 RTD | straight line, but tiny ΔR needs care |
| Very high temperature | Type-K thermocouple | −200 to +1350 °C; needs cold-junction comp |
| Easy, plug-and-play temp | TMP36 / DS18B20 | linear/digital out, almost no code |
| Distance to a wall | Ultrasonic / IR | time-of-flight or reflectance, no contact |
| Tiny force / weight | Strain gauge + bridge | differential reading on zero, amplifiable |
| Magnetic field / position | Hall-effect IC | contactless, switch or linear |
You can now trace any measurement from physics to ADC count and back to real units. The robot can finally see the wall and feel the heat.