Projects / Op-amp comparator light switch

Op-amp comparator light switch

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. Power the chip

    Pin 8 to 5 V, pin 4 to ground.

  2. 370 lux

    Build the sensor divider

    LDR to 5 V, 10 kilohm to ground, joint to pin 3.

  3. Set the reference

    Pot wiper to pin 2.

  4. 11.8 mA

    Add the output parts

    10 kilohm pull-up and the LED with 220 ohm.

  5. Tie the spare inputs

    Pin 5 to ground, pin 6 to 5 V.

  6. Test without feedback

    Note any flicker at the edge.

  7. Add the 100 kilohm feedback

    Output pin 1 to pin 3.

  8. Measure the thresholds

    Pin 3 voltage at switch-on and switch-off.

In short

How do you build an op-amp comparator light switch?

An LM393 comparator turns an LED on when a light sensor falls below a set point, with hysteresis added so it does not flicker. You need LM393 Comparator, LDR Photoresistor, Potentiometer 10 kΩ, Resistor 10 kΩ × 2, Resistor 100 kΩ, LED 5 mm red, Resistor 220 Ω, Digital multimeter, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
10

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
5 V supply positiveLM393 pin 8 (VCC)Power, any clean 5 V source
5 V supply negativeLM393 pin 4 (GND)Common ground
5 V positiveLDR leg 1Top of the light divider
LDR leg 2 and 10 kilohm resistor leg 1LM393 pin 3 (IN+A)Sensor voltage, falls when it gets dark
Other 10 kilohm resistor leg5 V negativeBottom of the divider
Pot outer legs5 V positive and negativeReference voltage divider
Pot wiperLM393 pin 2 (IN-A)Set point reference
LM393 pin 1 (OUT A)10 kilohm pull-up to 5 VOpen-collector output needs a pull-up
LM393 pin 1 (OUT A)100 kilohm resistor to pin 3Positive feedback for hysteresis
5 V positive220 ohm, red LED anodeLED branch
Red LED cathodeLM393 pin 1 (OUT A)Output sinks current to light the LED
LM393 pin 55 V negativeUnused comparator input tied low
LM393 pin 65 V positiveUnused comparator input tied high

Code

A comparator compares two voltages and gives a clean two-state output. The LM393 has an open-collector output, which means it can only pull its output low, so it needs a pull-up resistor to produce the high state. Here the LDR and a 10 kilohm resistor make a light-dependent voltage, and the pot makes an adjustable set point. When the room goes dark the sensor voltage falls below the set point, the output sinks, and the LED, wired from 5 volts to the output, lights up. A plain comparator will chatter when the light level sits near the set point, because noise pushes the sensor voltage back and forth across it. Positive feedback fixes this: a 100 kilohm resistor from the output to the plus input nudges the sensor voltage in the direction of the current output, so the switch-on and switch-off points are slightly different. That gap is called hysteresis, and it is the same idea behind a Schmitt trigger. You will measure the two thresholds with a multimeter at pin 3 and see the gap directly. Unused comparator inputs should never float, so the spare inputs are tied to the rails.

Build notes (no code needed for this build)

Pinout (8-pin DIP, notch at the top, pin 1 at the top left): 1 OUT A, 2 IN- A, 3 IN+ A, 4 GND, 5 IN+ B, 6 IN- B, 7 OUT B, 8 VCC. Leave pin 7 unconnected.

How it works
  The LM393 output is an open-collector transistor. When the IN+ input is BELOW the IN- input, the output sinks to ground. When IN+ is above IN-, the output is released and the pull-up resistor raises it to 5 V.
  The LDR divider goes to IN+, the pot reference goes to IN-.
  Dark: LDR resistance is large, the sensor voltage is low, IN+ is below IN-, the output sinks, and the LED lights.
  Bright: sensor voltage is high, IN+ is above IN-, the output is released, and the LED is off.
  The LED goes from 5 V through 220 ohm to the output, so it lights when the output sinks: about (5 - 2 - 0.2) / 220 = 13 mA, inside the LM393 sink limit of about 16 mA.

Hysteresis
  Without feedback, a slowly changing light makes the output chatter around the set point. The 100 kilohm resistor from the output back to IN+ pulls the sensor voltage slightly towards the output state, so the switch-on and switch-off points differ.
  The two thresholds differ by about (5 - 0.2) x R / (100 kilohm + R), where R is the divider resistance seen at pin 3, roughly 5 to 10 kilohm. That gives a gap of about 0.2 to 0.4 V.

Method
 1. Build with the feedback resistor left out. Set the pot so the LED switches in your room, and note that it can flicker at the edge.
 2. Add the 100 kilohm feedback resistor.
 3. Measure the voltage at pin 3 with the meter as you slowly shade the LDR. Note the voltage at which the LED turns ON.
 4. Slowly uncover the LDR and note the (higher) voltage at which the LED turns OFF.
 5. The difference between the two voltages is the hysteresis. Compare with the calculation above.

Power: use a clean 5 V source such as a USB adapter through a breakout lead, or the 5 V pin of a board on USB.

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
LDR uncovered, pot mid-wayLED off
LDR coveredLED on, about 13 mA
Slow shading without feedbackLED may flicker at the edge
Slow shading with feedbackClean switching
Measured gap between thresholdsAbout 0.2 to 0.4 V

If it does not work

LED never lights

Check pin 2 and pin 3 are not swapped and the LED direction.

LED always on

Reference is too high, turn the pot.

Output stays low

Check the 10 kilohm pull-up to 5 V is fitted.

No hysteresis seen

Check the 100 kilohm goes from pin 1 to pin 3.

Chip warm

Check the supply polarity.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the op-amp comparator light switch?

LM393 Comparator, LDR Photoresistor, Potentiometer 10 kΩ, Resistor 10 kΩ × 2, Resistor 100 kΩ, LED 5 mm red, Resistor 220 Ω, Digital multimeter, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the op-amp comparator light switch take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the op-amp comparator light switch works?

LDR uncovered, pot mid-way: LED off. LDR covered: LED on, about 13 mA. Slow shading without feedback: LED may flicker at the edge.

LED never lights: what should I check?

Check pin 2 and pin 3 are not swapped and the LED direction.

LED always on: what should I check?

Reference is too high, turn the pot.

Output stays low: what should I check?

Check the 10 kilohm pull-up to 5 V is fitted.

No hysteresis seen: what should I check?

Check the 100 kilohm goes from pin 1 to pin 3.