Dark and Light Sensor with LM358 Op-Amp Comparator and Hysteresis
Transistor-based light switches work, but they switch gradually and tend to flicker when the light level hovers near the threshold. An op-amp comparator fixes the first problem because its very high gain makes the output snap from low to high. Adding a little positive feedback, called hysteresis, fixes the second problem by giving the circuit two separate thresholds, one for turning on and one for turning off.
In this project an LDR and an LM358 build a sensor that can be switched between dark-activated and light-activated modes. You will calculate the hysteresis band and see why it makes a real difference at dusk.
Components Required
| Part | Value / Type | Quantity |
|---|---|---|
| Dual op-amp U1 | LM358 (one half used) with 8-pin socket | 1 |
| LDR | GL5528 or similar | 1 |
| R1 (divider) | 10 kilo-ohm | 1 |
| VR1 (threshold) | 10 kilo-ohm potentiometer | 1 |
| R2 (input to IN+) | 10 kilo-ohm | 1 |
| R3 (feedback) | 470 kilo-ohm | 1 |
| R4, LED | 1 kilo-ohm, red LED | 1 each |
| R5, Q1 | 4.7 kilo-ohm, BC547 | 1 each |
| Buzzer or relay | Active buzzer, or 9 V relay with 1N4007 | 1 |
| S1 (mode) | DPDT slide switch | 1 |
| Decoupling | 100 nanofarad ceramic | 1 |
| Power | 9 V battery or adapter | 1 |
Circuit Explained Stage by Stage
Stage 1: Sensor divider with mode switch
The LDR and R1 form a voltage divider whose midpoint, node S, goes to the inverting input (pin 2). The DPDT switch S1 swaps the two parts. In dark mode the LDR is on top (from +9 V to S) and R1 on the bottom, so S rises with light. In light mode R1 is on top and the LDR on the bottom, so S falls with light.
Stage 2: Reference and hysteresis network
VR1 across the supply gives an adjustable reference voltage, VREF, at its wiper. VREF reaches the non-inverting input (pin 3) through R2, and R3 feeds back from the output (pin 1) to pin 3. Because R3 connects the output to the positive input, the feedback is positive: once the output starts to move, it pushes the threshold away from the input and the output snaps fully over.
Thresholds at pin 3 (ignoring the wiper's own resistance): V_th = VREF x R3/(R2+R3) + VOUT x R2/(R2+R3) Output high (about 7.5 V on a 9 V supply for LM358, light load): upper threshold V_H = VREF x 0.979 + 7.5 x 0.0208 Output low (about 0 V): lower threshold V_L = VREF x 0.979 Hysteresis = V_H - V_L = 7.5 x 10k / 480k = about 0.16 V
The input must move by about 0.16 V past the switching point before the output changes back. Small changes from passing clouds, flickering street lamps or electrical noise can no longer make the output chatter. A smaller R3 gives a wider band.
Stage 3: Output driver
Pin 1 drives an LED through R4 and the base of Q1 through R5. Q1 switches an active buzzer or a relay coil with a 1N4007 flyback diode. The LM358 can source only a few tens of milliamps, so the transistor is necessary for anything beyond an LED.
| LM358 pin | Function | Connection |
|---|---|---|
| 1 | Output A | R3 to pin 3, R4 to LED, R5 to Q1 base |
| 2 | Inverting input A | Node S of the sensor divider |
| 3 | Non-inverting input A | R2 to VR1 wiper, R3 to pin 1 |
| 4 | GND | Ground |
| 5 | Non-inverting input B | GND (unused half) |
| 6 | Inverting input B | Pin 7 (unused half as follower) |
| 7 | Output B | Pin 6 |
| 8 | VCC | +9 V with 100 nF to GND |
How It Works
In dark mode, bright light makes node S higher than pin 3, so the output is low and the load is off. As light fades, S falls. When it drops below the lower threshold, the output swings high, the LED lights and Q1 switches the load. The threshold at pin 3 rises slightly at the same moment, so the light must increase noticeably before the output returns low.
In light mode the divider is reversed. Bright light pulls S down below the threshold and turns the output on, which is useful as a door-open alarm in a dark cupboard or a sunrise alarm.
Assembly and Testing
- Wire power to pins 8 and 4 and fit the decoupling capacitor across them.
- Build the divider with S1 and connect node S to pin 2. Measure S under room light and with the LDR covered to see the range.
- Connect VR1, R2 and R3. Set VR1 so the wiper sits midway between the two readings of S.
- Add the LED, then Q1 and the buzzer or relay.
- Slowly cover and uncover the LDR. The LED should switch cleanly, never half-bright.
- Measure pin 3 with the output high and low; the difference should be close to the calculated 0.16 V.
- Flip S1 and confirm the opposite behaviour.
Troubleshooting
- Output stuck high or low: VREF is outside the range that S covers. Re-measure both and adjust VR1.
- Still flickers at the threshold: increase the hysteresis by reducing R3 to 220 kilo-ohm, or add 1 microfarad from node S to ground.
- Relay chatters as it switches: the relay current dips the supply and shifts VREF. Use a separate regulator for the sensor side or a larger supply capacitor.
- Output never reaches 9 V: normal for LM358. Its output high level is typically about 1.5 V below the supply.
- Modes appear reversed: S1 wiring is swapped; check which divider arrangement each switch position selects.
Summary
An LM358 comparator with a resistor from output to non-inverting input becomes a Schmitt trigger with two thresholds. With an LDR divider on the other input, it makes a crisp, flicker-free dark or light sensor. The hysteresis formula lets you choose how much the light must change before the output switches back.