Learn & build · Electronics · Part II · Chapter 20
Sensor to switch: a dusk light
So far the builds did something on their own: blink, count. Most useful circuits react to the world: a fan that starts when it's hot, a pump when the soil is dry, a light when it gets dark. Each is the same chain, a sensor, a decision, and a switch for something that needs real power. This build makes that chain with no microcontroller and no code: a 12 V LED strip that turns itself on at dusk and off at dawn, and doesn't flicker in between.
1. The goal, the blocks, and the whole circuit
The goal: a 12 V LED strip (about 1 A) turns on when the light falls below dusk, about 10 lux, and off again in the morning, without flickering as the light hovers near the edge. The blocks: a light sensor that turns lux into volts; a comparator that decides "dark or not" against an adjustable threshold; a MOSFET that switches the strip. Here's the circuit. Drag the light down to dusk and back up, slowly, and watch where the strip turns on and where it turns off.
🎯 Challenge: find the light level where the strip turns on, then the one where it turns off. Are they the same?
2. The sensor: choosing the divider resistor
The LDR (a light-dependent resistor, chapter 1) is the bottom of a divider with R1 on top, so the sensor voltage rises as the room darkens. R1 is a choice, and a bad one makes the sensor nearly blind at dusk. The goal is that around 10 lux the voltage moves as much as possible for a small change of light, so the comparator gets a clear signal. Pick R1 and watch the curve's slope at dusk.
🎯 Challenge: pick the R1 that makes the voltage change the most between 8 and 12 lux.
The steepest choice is R1 equal to the LDR's own resistance at the light you care about: about 15 kΩ at 10 lux for a GL5528 (its datasheet says 10–20 kΩ, which is also why the threshold must be adjustable). Too small an R1 and the voltage sits near 12 V at every dusk-like light; too big and it sits near 0.
Why is matching the resistances the steepest choice?
- The sensor voltage is \(S = V\,\dfrac{R}{R_1 + R}\), with \(R\) the LDR.
- How much it moves for a small change of \(R\): \(\dfrac{dS}{dR} = V\,\dfrac{R_1}{(R_1 + R)^2}\).
- Light changes the LDR by a fraction (its resistance goes as lux−0.7), so what matters is the change for a given fractional change, \(R\,\dfrac{dS}{dR} = V\,\dfrac{R_1 R}{(R_1 + R)^2}\).
- For a fixed \(R\), that's largest when \(R_1 = R\) (try \(R_1 = R/2\) and \(2R\): both give \(\tfrac{2}{9}V\), less than the \(\tfrac{1}{4}V\) at \(R_1 = R\)).
- And at \(R_1 = R\) the voltage is exactly \(V/2\), the middle of the range: the same answer as "put the decision where the signal has room on both sides."
3. The decision: an LM393 and its open-collector output
Chapter 7 built a comparator from an op-amp. The LM393 is a chip made only for comparing, two of them in an 8-pin package, and its datasheet holds two surprises that catch everyone once.
Surprise one: its output can't go high. It's an open collector: a transistor from the output pin to ground, nothing to the supply. When IN− is above IN+ it pulls the pin low; otherwise it simply lets go, and the pin floats, unless a pull-up resistor (R2 here) lifts it. That's chapter 13's I²C line again, and the reason is the same: you choose the high voltage, and several outputs can share one line.
Predict: take out R2, the pull-up. It gets dark. What does the strip do?
- Turns on as usual
- Stays off: nothing pulls the gate high
- The LM393 burns out
Surprise two: its inputs don't reach the top rail. The datasheet promises correct comparisons only for inputs from 0 V to VCC − 1.5 V, 10.5 V on 12 V. In deep darkness the LDR reaches 1 MΩ and the sensor voltage climbs to about 11.8 V, past that limit. It still works, because the same datasheet adds that only one input needs to be inside the range: the reference on IN− stays at 6 V. Reading that footnote is the difference between a design and a guess. (A design that never needs it would power the divider from a lower voltage.)
4. Hysteresis: one threshold for on, another for off
At dusk the light doesn't fall smoothly: clouds, a passing car, a tree moving in the wind. A comparator with one exact threshold turns the strip on and off with every wobble. Chapter 7's cure is positive feedback, and here it's R3: once the strip is on, the comparator's high output feeds a little current through R3 back into the sensor node, nudging it up, so it takes noticeably more light to turn the strip off again. While the strip is off, R3 pulls the other way. Start with no R3 at all, then choose one and see the two thresholds move apart. (Changing these also changes the circuit in section 1.)
🎯 Challenge: set the thresholds so the strip turns on below about 8 lux and off above about 12 lux.
A sensor circuit is a chain: a divider turns the world into a voltage, a comparator with hysteresis turns the voltage into a clean yes or no, and a transistor turns the yes into power. Make the divider steepest at the decision, make the threshold adjustable, and give the decision two edges.
Where do the two thresholds come from?
- The comparator flips when the sensor node reaches the reference, \(V_{ref}\) (6 V with the pot centered).
- At that moment, the currents into the node balance (Kirchhoff, chapter 6): from +12 V through R1, from the comparator's output side through R3, and out through the LDR: \(\frac{12 - V_{ref}}{R_1} + \frac{V_G - V_{ref}}{R_3 + R_G} = \frac{V_{ref}}{R}\).
- While the strip is on, the output side is +12 V through the pull-up: \(V_G = 12\), \(R_G = R_2\) = 10 kΩ. Current flows in through R3, so the LDR must fall lower (brighter) to bring the node back down: the turn-off threshold.
- While it's off, the output pulls to about 0 V: \(V_G \approx 0\). R3 drains current out, so the LDR must rise higher (darker): the turn-on threshold.
- Solve each for \(R\) and turn \(R\) into lux with the LDR's curve, \(R = 15\text{ k}\Omega \times (\text{lux}/10)^{-0.7}\). With R3 = 100 kΩ: about 7.9 and 12.0 lux; the simulator, sweeping the light down and back up, finds 7.9 and 12.1.
5. The switch: a MOSFET on the ground side
The comparator can sink a few milliamps; the strip wants an amp. Chapter 4's logic-level MOSFET does the lifting, here on the strip's ground side ("low side"): the strip's + wire goes to +12 V, its − wire to the drain, and the source to ground. The gate gets the full 12 V through the pull-up, so the IRLZ44N is far past fully on (its datasheet: 0.025 Ω at only 5 V) and burns about \(1^2 \times 0.025 = 0.025\) W: no heatsink. And 12 V is inside its ±16 V gate limit, which matters: a 24 V strip with the same circuit would put 24 V on the gate and destroy it.
6. The breadboard
One chip in the middle, the sensor and its resistors on the bottom half, the trim pot on the top, and the MOSFET at the right, beside the strip's flying leads. Two details: the LM393's unused second comparator has its inputs tied (pin 5 to ground, pin 6 to +12 V), so it sits still instead of chattering; and the strip's current flows only through the MOSFET and its own wires, never through the thin jumpers of the control side.
The strip draws 1 A. Breadboard contacts are usually rated for about an amp at most, and jumper wires are thin. Where should the strip's current flow?
- Along the breadboard's rails, like everything else
- Through jumper wires to the MOSFET
- Through thick wires straight to the adapter and the MOSFET's legs; on a breadboard, only for testing
7. The shopping list
| Ref | Part | Why this one |
|---|---|---|
| U1 | LM393 (8-pin DIP) | Dual comparator, open collector, 2–36 V: runs from the strip's 12 V. |
| Q1 | IRLZ44N (TO-220) | Logic-level, 0.025 Ω at 5 V, 55 V, ±16 V gate. |
| LDR1 | GL5528 light-dependent resistor | 10–20 kΩ at 10 lux, 1 MΩ dark. |
| R1 | 15 kΩ, ¼ W | Matches the LDR at dusk (section 2). |
| RV1 | 10 kΩ trim pot | The threshold: the calibration knob. |
| R2 | 10 kΩ, ¼ W | The pull-up the open collector needs. |
| R3 | 100 kΩ, ¼ W | Hysteresis: on below ~8 lux, off above ~12. |
| C1 | 0.1 µF ceramic | The comparator's bypass. |
| LP1 | 12 V LED strip, ~12 W | The load; check its watts per meter. |
| BAT1 | 12 V adapter, 1.5–2 A | A margin above the strip's 1 A. |
8. Power up and check
- 1. Without the strip first: VCC (pin 8) 12 V; the pot's wiper (pin 2) adjustable around 6 V.
- 2. The sensor: pin 3 below 6 V in a lit room, rising when you cover the LDR with your hand: past about 6 V the output should flip.
- 3. The output: the MOSFET's gate near 0 V in light and near 12 V when covered (in the simulator, 11.7 V: the adapter's own resistance sags a little under the strip's amp).
- 4. Then the strip: covered, it lights; uncovered, it goes out. Feel the MOSFET: cool.
- 5. Calibrate: at the dusk you want, turn RV1 slowly until the strip just turns on.
9. What's wrong?
Covering the LDR, the gate goes to 12 V, but the strip stays dark.
- The power path: the strip's − wire isn't on the drain, or the source isn't on ground
- The comparator is broken
- R3 is the wrong value
The strip turns on at dusk, then the light from the strip itself reaches the LDR, and it turns off, then on, then off…
- R3 is too big
- The sensor sees the light it controls: shield it, or point it away from the strip
- The comparator is too fast
10. Make it yours
- A thermostat: swap the LDR for an NTC thermistor (chapter 11, 10 kΩ at 25 °C) and R1 for 10 kΩ: now it's "too hot?", and the strip can be a 12 V fan. A fan is a motor, a coil (chapter 14), so it needs a flyback diode across it, cathode to +12 V, which the LED strip didn't. Which input should the sensor go to, so the fan runs when it's hot?
- An inverted light: swap the LDR and R1. What changes, and what must change with them to keep "on when dark"?
- Both comparators: use comparator B on the same sensor with a second threshold, for a two-stage light.
Next: a speaker amplifier, the first build that handles a signal instead of a level: audio riding on half the supply, and the capacitors that keep it off the speaker.
Previous: Two chips talking · All chapters: Learn & build.
Last updated