Learn & build · Electronics · Chapter 11
Light and sensors
A sensor turns something in the world (light, heat, force, a magnet) into something electrical: a current, a voltage, a change of resistance. Then a circuit turns that into a voltage a chip can read, and an ADC turns the voltage into a number. This chapter follows that chain for a few classic sensors, and shows the math that turns the number back into degrees or grams.
1. Light into current: the photodiode
A diode (chapter 4) with a window: each photon that lands in its junction can knock loose one electron, so light makes a tiny current flow backwards through it, proportional to the light. Tiny means microamps. An op-amp turns it into a usable voltage: the photodiode's current has to go through the feedback resistor (golden rule 2), so \(V_{out} = I \cdot R_f\). It's called a transimpedance amplifier, and every optical mouse, fiber receiver and light meter has one.
🎯 Challenge: pick Rf so ordinary room light (about 300 lux) reads between 1 and 3 V.
A photodiode gives 20 µA; Rf = 100 kΩ. What's the output voltage?
2. Optocouplers: a signal across a gap
Put an LED and a phototransistor (chapter 4's bipolar transistor, with light making its base current) face to face in one package, with nothing but plastic between them. Current in the LED makes light, the light makes current on the other side, but no wire connects the two. That's isolation: a microcontroller can switch a mains-powered circuit, or read a signal from a noisy motor, without the two sharing a ground. The ratio of output to input current is the current transfer ratio (CTR), typically 50–200%, and it falls as the LED ages.
Why use an optocoupler between a microcontroller and a circuit connected to mains voltage?
- To amplify the signal
- To make the signal faster
- So no conductive path exists between the two: a fault on the mains side can't reach the low-voltage side (or you)
3. Temperature: a thermistor, a divider and an ADC
An NTC thermistor is a resistor whose resistance falls as it warms, steeply and predictably. Put it in a voltage divider (chapter 1) with a fixed 10 kΩ, read the middle with a 12-bit ADC on 3.3 V, and you have a thermometer. Move the temperature and follow the chain.
The math, step by step
The Beta equation describes an NTC with temperatures in kelvin (\(T_0 = 298.15\) K = 25 °C, where it measures \(R_0\), and a constant \(B\) from the datasheet, here 3950):
\[ R(T) = R_0\, e^{B\left(\frac{1}{T} - \frac{1}{T_0}\right)} \]
With the thermistor on the bottom of the divider: \(V = 3.3 \cdot \frac{R}{R + 10\text{k}}\), and the ADC reports \(n = \text{round}(4095 \cdot V / 3.3)\). Going back, which is what the chip's firmware does with each reading: \(R = 10\text{k} \cdot \frac{n}{4095 - n}\), then solve the Beta equation for \(T\):
\[ \frac{1}{T} = \frac{1}{T_0} + \frac{1}{B} \ln\frac{R}{R_0} \]
Near 25 °C one count is worth about 0.02 °C (about 45 counts per degree), so rounding to whole counts costs only about 0.02 °C.
At 25 °C the thermistor is exactly 10 kΩ, equal to the fixed resistor. What does the 12-bit ADC read?
A sensor reading is a chain: the world changes a resistance or a current, a circuit turns that into a voltage, the ADC turns the voltage into a number, and code undoes each step to get back to the world.
4. How fine can an ADC see?
An ADC with \(N\) bits splits its range into \(2^N\) steps, so every voltage gets rounded to the nearest step, exactly as the radio course's 8-bit dongle does with radio samples. Slide the bits and watch the staircase approach the real curve.
A 12-bit ADC on a 3.3 V range. How big is one step (LSB), in millivolts?
5. Tiny changes: the Wheatstone bridge
A strain gauge (the heart of every kitchen scale) is a zig-zag of foil whose resistance changes by about 0.1% at full load: 350 Ω becomes 350.35 Ω. Measured on its own that's hopeless, buried under the 2.5 V across it. The trick is a bridge: two dividers side by side, made to balance at zero load, so their difference starts at exactly 0 V and only the change shows.
The math
With one gauge \(R(1 + x)\) in a bridge of equal resistors on supply \(V\):
\[ V_{out} = V\left(\frac{R(1+x)}{R + R(1+x)} - \frac{1}{2}\right) \approx \frac{V\,x}{4} \]
A 0.1% change (\(x = 0.001\)) on 5 V gives about 1.25 mV: small, but starting from zero, so an amplifier with a gain of 1000 (the HX711 chip in cheap scales has 128 plus a 24-bit ADC) turns it into a clean reading.
A bridge on 5 V; one gauge changes by 0.1%. About what's the output?
Every one of these is a circuit you've already met: a diode, a divider, an op-amp, a bridge of dividers. Next: logic, where voltages stop being measurements and become ones and zeros.
Previous: Power supplies · All chapters: Learn & build.
Last updated