Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Voltaat Arduino Ultimate Kit
40 min
Share

Use the LDR to know the state of the light

A Light Dependent Resistor (LDR). An LDR, also known as a photoresistor, is a component whose resistance varies with the amount of light falling on it. These devices are made of semiconductor materials that allow them to change their resistance based on the light intensity.

Project Video

Overview

In this tutorial, we will use the Arduino to read the resistance of an LDR and detect darkness. This project may be expanded to control an LED to turn on when the light goes out and off when it comes back, similar to how a street light works!

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
Photocell – LDR (4 pieces)
Get Item
1/4 Watt Resistor (20 Pack)
Get Item
Half-size Breadboard
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the LDR and the Arduino, as shown in the image below.



 Connections from the Arduino to the breadboard:


      • Arduino pin GND → Breadboard ground line


      • Arduino pin 5V → Breadboard 5V line


       • Arduino pin A3 → Common pin between LDR and the 10KΩ resistor


 Connections from the LDR:


     • First pin→ Breadboard 5V line


    • Second pin→ 10KΩ resistor left pin


    • 10KΩ resistor right pin→ Breadboard ground line

Coding

/*
 Voltaat learn (http://learn.voltaat.com/)
 Link for full tutorial:

 Tutorial: Detect darkness using the LDR resistance

 The function of this sketch is to read the values of the LDR resistance - via the input pin of the Arduino

 Connections from the Arduino to the breadboard:
 • Arduino pin GND → Breadboard ground line
 • Arduino pin 5V → Breadboard 5V line
 • Arduino pin A3 → Common pin between LDR and the 10KΩ resistor

 Connections from the LDR:
 • First pin→ Breadboard 5V line
 • Second pin→ 10KΩ resistor left pin
 • 10KΩ resistor right pin→ Breadboard ground line

*/


// Define the variable ldr to Analog Input A3.
const int ldr = A3;

// Define ldrStatus variable
int ldrStatus;

// Commands inside void setup run once.
void setup()
{

 // ldr is defined as an input
 pinMode(ldr, INPUT);

 // Start the serial monitor at 9600 baud rate (9600 bits per second)
 Serial.begin(9600);


}

// Commands inside void loop run forever
void loop()
{
 // Read the value of analog input from LDR and assign it in the variable ldrStatus
 ldrStatus = analogRead(ldr);

 // This value can change depending on the intensity of the surrounding light and the sensitivity of the LDR
 if (ldrStatus <= 450)
 {

   // print "It is dark here " to the serial monitor at the same line
   Serial.print("It is dark here ");

   // print The ldrStatus value to the serial monitor at a new line
   Serial.println(ldrStatus);


 }
 else
 {

   // print "The light has returned " to the serial monitor at the same line
   Serial.print("The light has returned ");

   // print The ldrStatus value to the serial monitor at a new line
   Serial.println(ldrStatus);

 }

}

Testing it Out

You should also make sure you have chosen the right baud rate (9600) as specified in the code.
know when your plant needs watering

After uploading the code, you can now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.

know when your plant needs watering

Now, as we see in the following image, the serial monitor displays the status of the light presence and the analog read value from the analog input A0.

know when your plant needs watering

Resources

No items found.