Dark detector
An op-amp comparator, an LDR and a pot light an LED when the room goes dark, no board, on a half-size breadboard. There is no microcontroller in it: the simulator solves the 6 parts as a circuit, so it runs the moment you press Run, and nothing needs compiling.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $4.20. On Mokxi they're free.
What you'll learn
- Voltage dividers with a light sensor
- An op-amp used as a comparator
- Setting a threshold with a potentiometer
- Why an op-amp output does not reach the rail
- Difficulty: Intermediate
- Time: About 20 minutes
- Parts: 9
How it works
A 10k resistor from 5 V and a light sensor to ground form a voltage divider. In the light the sensor has low resistance, so the middle point sits near 0 V. In the dark its resistance rises to tens of kilohms and the middle point climbs toward 5 V.
An op-amp compares that middle point (IN+) with the potentiometer wiper (IN-). There is no feedback, so the output snaps high when the middle is above the wiper and low when it is below.
When the output is high, current flows through the 220 ohm resistor and the red LED. The output reaches only about 4.0 V, because the op-amp has its own output resistance, so the LED glows at about half brightness. Turn the pot to move the switching point.
Parts list
9 parts, plus the jumper wires. They retail for about $4.20; on Mokxi they're free.
- 1 × Half breadboard
- 2 × Power, 5 V
- 1 × Light sensor
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × Op-amp
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 10k Ω pin 2 | 5 V |
| Potentiometer pin 1 | 5 V |
| Light sensor pin 1 | Ground |
| Potentiometer pin 3 | Ground |
| Op-amp pin GND | Ground |
| LED, red pin C | Ground |
| Resistor, 10k Ω pin 1 | Light sensor pin 2 |
| Op-amp pin IN+ | Light sensor pin 2 |
| Op-amp pin IN- | Potentiometer pin 2 |
| Resistor, 220 Ω pin 1 | Op-amp pin OUT |
| Op-amp pin V+ | 5 V |
| LED, red pin A | Resistor, 220 Ω pin 2 |
The notes that come with it
There is no microcontroller in this one, so nothing is compiled: the simulator solves the circuit itself. The file in the editor is the notes that come with it.
// A dark detector: an op-amp comparator, an LDR and a pot. No board.
//
// The 10 k from 5 V and the LDR to ground make a divider. In the light the
// cell is a few hundred ohms and the middle sits near 0 V; covered, the cell
// is tens of kilohms and the middle rises towards 5 V.
//
// The op-amp compares that middle (IN+) with the pot's wiper (IN-). With no
// feedback its gain is the whole 100 000, so the output slams to one rail or
// the other: up when the middle is above the wiper, down when it is below.
//
// It does not quite reach the rail, and that is the op-amp being honest. It
// drives through 100 ohms of its own output resistance, so with 220 ohms and
// the LED hanging on it the loop is 100 + 220 + the LED's own 15, and
// (5 - 1.8) / 335 is 9.6 mA. The output sits at 4.0 V rather than 5, the LED
// lights at about half brightness, and the probe on the resistor says so.
//
// Things to try. Drag the slider on the cell towards dark and find the point
// where the LED comes on; turn the knob and the point moves. Swap IN+ and IN-
// and it becomes a light detector. Add a 1 M from OUT back to IN+ and it
// stops flickering at the threshold: that is hysteresis, and it is what the
// night light sketch does in software.
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED is always on or always off. This project
Turn the pot. Its wiper sets the threshold, and it may sit outside the range your light level covers. - The LED lights in the light and goes out in the dark. This project
IN+ and IN- are swapped. The light sensor divider belongs on IN+ and the pot wiper on IN-. - The LED never lights at all. This project
Check the op-amp has V+ on 5 V and GND on ground, and that the LED cathode goes to ground. - The reading barely changes, or sits stuck near 0 or the top of the range. Light sensor
An LDR is a resistor, so use it in a divider with a fixed resistor, about 10k, and read the middle node. Without the divider the analog pin sees nothing useful. - Readings go the wrong way: higher in the dark. Light sensor
Which side the LDR sits on decides the direction. LDR on the supply side gives a higher reading in the light. LDR on the ground side gives a lower reading in the light. Swap them if needed. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.