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.

Before you start. The light sensor in a divider is chapter 1 §2; comparators and hysteresis are chapter 7 §5; the MOSFET switch and logic-level gates are chapter 4 §3–4; open-drain lines and pull-ups are chapter 13 §5; the build steps are chapter 17.

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?
  1. The sensor voltage is \(S = V\,\dfrac{R}{R_1 + R}\), with \(R\) the LDR.
  2. How much it moves for a small change of \(R\): \(\dfrac{dS}{dR} = V\,\dfrac{R_1}{(R_1 + R)^2}\).
  3. 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}\).
  4. 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\)).
  5. 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?

In the dark the comparator lets go of its output, and with no pull-up the MOSFET's gate just sits at whatever it last was, or wherever stray charge puts it. Pick R2 in the schematic and take it out to see. An open-collector output without a pull-up is one of the most common "my circuit does nothing" bugs.

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?
  1. The comparator flips when the sensor node reaches the reference, \(V_{ref}\) (6 V with the pot centered).
  2. 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}\).
  3. 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.
  4. 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.
  5. 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?

Chapter 5's wire resistance and chapter 15's trace widths, again. Control signals (microamps, milliamps) are happy on a breadboard; an amp belongs on thick wire, screw terminals or a soldered board. Prototype with a short piece of strip, then move the power path to proper wire.

7. The shopping list

RefPartWhy this one
U1LM393 (8-pin DIP)Dual comparator, open collector, 2–36 V: runs from the strip's 12 V.
Q1IRLZ44N (TO-220)Logic-level, 0.025 Ω at 5 V, 55 V, ±16 V gate.
LDR1GL5528 light-dependent resistor10–20 kΩ at 10 lux, 1 MΩ dark.
R115 kΩ, ¼ WMatches the LDR at dusk (section 2).
RV110 kΩ trim potThe threshold: the calibration knob.
R210 kΩ, ¼ WThe pull-up the open collector needs.
R3100 kΩ, ¼ WHysteresis: on below ~8 lux, off above ~12.
C10.1 µF ceramicThe comparator's bypass.
LP112 V LED strip, ~12 WThe load; check its watts per meter.
BAT112 V adapter, 1.5–2 AA margin above the strip's 1 A.

8. Power up and check

9. What's wrong?

Covering the LDR, the gate goes to 12 V, but the strip stays dark.

A good gate voltage means the decision chain works; the fault is after it. Measure the drain: if it stays at 12 V with the gate high, the source isn't grounded; if it's at 12 V with the strip unconnected, the strip's wire is the problem. Divide the chain, measure at the boundary, and you halve the search every time.

The strip turns on at dusk, then the light from the strip itself reaches the LDR, and it turns off, then on, then off…

That's a feedback loop through the air, and it oscillates exactly like chapter 9's. Every sensor that controls what it senses needs thought: a thermostat measures the room, not the heater's grille.

10. Make it yours

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