Using an LDR with Arduino and a control switch to light an Arduino

Using an LDR with Arduino and a control switch to light an Arduino

In this tutorial, we will explore how to use a Light Dependent Resistor (LDR) with an Arduino to control a light based on ambient light levels. The LDR changes its resistance depending on the light intensity; thus, it can be used to detect whether it is light or dark in an environment. When the light is below a certain threshold, we will turn on an output pin to activate a light.

LDR Illustration and symbol

The primary components you will need include an Arduino board, a 10k ohm resistor, the LDR, and some jumper wires. The LDR will be connected in a voltage divider configuration with the resistor to read the analog voltage, which will change based on the light intensity. We will use this voltage to control an LED or other output devices.

For a visual demonstration of the setup and code, be sure to check the associated video (in video at 0:10).

Hardware Explained

The Light Dependent Resistor (LDR) is a type of resistor whose resistance decreases with increasing incident light intensity. It is commonly used in applications where light levels need to be monitored. In our setup, the LDR will be used to detect ambient light levels and will send a corresponding analog voltage to the Arduino.

The Arduino will read this voltage via one of its analog pins and decide whether to turn on or off the connected light based on a defined threshold. The 10k ohm resistor is used in the voltage divider configuration with the LDR to create a stable voltage output that the Arduino can read.

Datasheet Details

Manufacturer Generic
Part number LDR
Resistance (light) 100 - 500 Ω
Resistance (dark) 10 kΩ - 1 MΩ
Working voltage 3.3 V - 5 V
Response time 20 ms (typ.)
Package Through-hole

 

  • Ensure proper voltage levels; do not exceed 5V.
  • Use a pull-down resistor to stabilize the readings.
  • Opt for a suitable resistor value (10kΩ) for the voltage divider.
  • Consider using a capacitor for noise filtering if needed.
  • Keep the LDR away from direct light sources when testing.

Wiring Instructions

Wiring LDR with Arduino
Wiring LDR with Arduino

To set up the circuit, begin by connecting one terminal of the LDR to the 3.3V power supply on the Arduino. The other terminal of the LDR will connect to one end of the 10k ohm resistor. The other end of the resistor should be connected to the ground.

Next, create a connection between the junction of the LDR and the resistor to an analog input pin, such as A0. This connection will allow the Arduino to read the voltage. Make sure to connect the ground of the Arduino to the common ground of your circuit to ensure proper functionality.

Code Examples & Walkthrough

The following code snippet initializes the serial communication and reads the analog value from the LDR. The value is then converted into a voltage and printed to the serial monitor.

int LDRvalue = analogRead(A0); // Read the analog value from LDR
float voltage = LDRvalue * (5.0 / 1023.0); // Convert to voltage
Serial.print("Voltage ="); // Print label
Serial.print(voltage); // Print actual voltage

In this code, the variable LDRvalue stores the raw analog reading from the LDR. The voltage is calculated based on the maximum analog value (1023) and the voltage reference (5V).

Next, we check if this voltage is below a threshold (3V in this case) to control an output pin.

if(voltage < 3 ){ 
  digitalWrite(10, HIGH); // Turn on the light
}else{
  digitalWrite(10, LOW); // Turn off the light
}

Here, if the measured voltage is below 3 volts, we set the output pin (10) to HIGH, turning on the light; otherwise, it's set to LOW, turning it off. The constant checking of the light condition allows for real-time control based on the ambient light levels.

Demonstration / What to Expect

Upon completing the wiring and uploading your code, you should see the voltage readings on the serial monitor change as you vary the light intensity on the LDR. If you cover the LDR, the voltage should increase, and when exposed to light, it should decrease. This behavior will directly affect the output pin, which controls the light.

Be cautious of common pitfalls, such as floating inputs or incorrect voltage levels, which can lead to erroneous readings (in video at 12:30).

Video Timestamps

  • 00:00 Introduction to LDR
  • 01:30 Hardware setup
  • 03:45 Code explanation
  • 06:00 Running the code
  • 09:15 Troubleshooting tips

Images

Wiring LDR with Arduino
Wiring LDR with Arduino
An LDR
An LDR
LDR Illustration and symbol
LDR Illustration and symbol
14-Using an LDR with an Arduino and a control switch for a light
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

No files available.