Light sensors are widely used in electronic applications. The most common one used is a photoresistor or light-dependent resistor. Despite a long response time, this sensor is inexpensive and useful in several applications.
A photoresistor consists of a zigzag track of photo-sensitive semiconductors. It offers pure resistance although this is dependent on the ambient light. If in the dark, the sensor has a high resistance — think several thousand ohms or mega ohms. If in the light, the sensor’s resistance drops to a few hundred ohms.
Practically, light-dependent resistors (LDRs) are poor at measuring light intensity. An LDR’s response to illumination has a sharp fall in resistance. Additionally, its spectral response is tuned to a maximum of 540 nm wavelength of light.
However, this tuning to pure green light makes LDR particularly sensitive to the spectrum of light visible to human eyes. An LDR can typically gauge the light or dark because of its specially tuned spectral response.
A light or dark sensor can be designed by using an LDR that’s connected to a voltage-divider circuit.
Note:
- There will be a high voltage drop in the LDR because of its high resistance to the dark. As a result, a low voltage will be obtained at the output of the voltage divider.
- There will be much less of a voltage drop in the LDR in the light because of its drop resistance in the light. As a result, a higher voltage will be obtained at the output of the voltage divider.
The output from the voltage divider network is analog. The voltage divider can be paired with an operational amplifier to design the ideal light or dark sensor. First, connect the output of the LDR-resistor voltage divider to the non-inverting input of an OPAMP. Apply the reference voltage to the inverting input.
- If it’s dark, the output voltage from the LDR network will be lower than the reference voltage, and the output of OPAMP will be LOW.
- If it’s light, the output voltage from the LDR network will be greater than the reference voltage and the output of the OPAMP will be HIGH.
Next, try connecting the output of the LDR-resistor voltage divider to the inverting input of an OPAMP. Apply the reference voltage to the non-inverting input.
- If it’s dark, the output voltage from the LDR network will be lower than the reference voltage and the output of OPAMP will be HIGH.
- If it’s light, the output voltage from the LDR network will be greater than the reference voltage and the output of the OPAMP will be LOW.
The reference voltage can be adjusted using a pot or variable resistance. Regardless of which way the voltage divider network is paired with the OP-AMP, the output logic can be used to make light or the dark sensor.
Here’s a circuit diagram that can be switched as the light and dark sensor…
This example uses an LM358 dual-operational amplifier IC. As per the above circuit, the output of the LDR-resistor network is connected to the non-inverting input of one of the op-amps and inverts the input of another LM358 op-amp.
The reference voltage is adjustable via a variable resistor that’s connected to the inverting input of the first op-amp and the non-inverting input of the other. The output of the two op-amps will work in opposing manners in light or dark situations. The output of one of the op-amps is drawn via jumper headers. The user can choose whether to use the circuit as the light or dark sensor.
The same output can be used to drive an LED, indicating the detection of light or dark.
The circuit assembled in the PCB is shown here:
Interfacing an LDR with Arduino
A photoresistor can be paired with Arduino using either digital or analog input. If the LDR in a voltage divider is directly interfaced with Arduino (or any microcontroller), the output of the voltage divider network must be connected to an Arduino analog input.
In this case, Arduino can be programmed to read the analog voltage from the LDR-resistor network and compare it to a reference value for certain decision-making — such as switching ON the electronic lights automatically when the dark is detected or switching OFF the electronic lights automatically when light is detected.
A second way to approach this is to pair the LDR-resistor network with an operational amplifier (op-amp) and take the output from the op-amp to Arduino as the digital input.
In this method, the output of the op-amp is physically calibrated via a potentiometer or a variable resistor. This means the user doesn’t have to reprogram Arduino to recalibrate the sensor when or if necessary. He or she can simply adjust the pot and the recalibration is done.
Here, Arduino reads the digital logic from the LDR op-amp circuit and performs the decision-making accordingly. This method of interfacing an LDR with Arduino is simple, user-friendly, and typically preferred.
Light and dark detection using an LDR
For this project, let’s stick to the LDR sensor module so we can select the light or dark detection by placing jumpers. We’ll be using this module to detect light or dark and monitor the output of LDR op-amp sensor module using Arduino Serial Monitor.
Components required
1. Arduino UNO x1
2. LDR x1
3. Resistors 330Ω x2
4. Resistor 10K x1
5. Pot 10K x1
6. LM358 x1
7. LED x1
8. Breadboard x1
9. Connecting wires or jumper wires
Circuit connections
Begin by connecting an LDR with a 10K resistor in a voltage-divider network. Join the LDR side of the network to the 5V DC and the resistor side of the network to ground (because the change in voltage drop has to occur across the LDR).
Next, connect the output of the LDR-resistor network to LM358 IC’s pins 3 and 6. Take a pot and join its fixed terminals to the 5V DC and the ground. Attach the variable terminal of the pot to LM358 IC’s pins 2 and 5. LM358 IC’s pins 8 and 4 should connect to the VCC and the ground.
To work the circuit as a light sensor, connect LM358’s pin 1 to one of Arduino’s digital I/O pins. The circuit will output as HIGH when light is detected and LOW when it’s dark.
To use a dark sensor instead, connect LM358 ‘s pin 7 of to one of Arduino’s digital I/O pins. As a dark sensor, the circuit will output as HIGH when it’s dark and a LOW when there’s light.
The output from the LDR op-amp circuit can be used to drive an LED directly or via Arduino.
In this tutorial, the circuit has been taken on a PCB, in which the operation as a light or dark sensor can be selected by using a jumper connector or a 2-pin shunt. An LED is already provided on the PCB to indicate the light or dark detection.
Therefore, the output of the LDR op-amp circuit is connected to the A0 analog input pin to monitor the change in output voltage from the op-amp, depending on whether there’s light or dark.
Arduino sketch
const int ldrPin = A0;
void setup() {
Serial.begin(9600);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
Serial.println(ldrStatus);
delay(1000);
}
How the circuit works
The output of the LDR-resistor network is connected to the non-inverting input of one of the op-amps and inverts the input of another LM358 op-amp. The same reference voltage is applied to the opposite inputs of both op-amps. This reference voltage is calibrated by the user.
If it’s dark, the LDR offers high resistance in the circuit. Due to high resistance, there’s a high voltage drop across it. As a result, a voltage lower than the reference voltage is output from the voltage divider network. This also means that the output at LM358’s pin 1 is LOW while pin 7 is HIGH.
When there’s light, the resistance of the LDR drops to a few hundred ohms. Due to drop in resistance, there’s a low voltage drop across the LDR. As a result, a voltage greater than the reference voltage is output from the voltage divider network. So, the output at LM358’s pin 1 is HIGH while pin 7 is LOW.
The result