Use the LDR to know the state of the light 🌞

Have you ever wondered how street lights automatically switch on when it becomes dark?


Maybe you think someone is in charge of turning it on and off, or that it works with a timer!


But what if some clouds appear and the road becomes suddenly dark?


In this scenario, using the LDR is the best option. LDR stands for light-dependent resistors, or photoresistors. It is a light-sensitive resistor whose value varies with light intensity.



Overview


The LDR value is inversely proportional to the amount of light. This means in the dark, the LDR's resistance increases, while in the presence of light, the LDR's resistance decreases.


As a result, it may be used to signify the intensity of light, its presence, or its absence.


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


For this project you will need the following components: 

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat Photocell - LDR (4 pieces)
1 QAR
Sale Off
Voltaat 1/4 Watt Resistor (20 Pack)
2 QAR

Make sure you also have a breadboard and jumper wires to connect everything together

Sale Off
Voltaat Full-size Breadboard
20 QAR
Sale Off
Voltaat Jumper Wires - Male to Male (40 Pack)
10 QAR

Wiring it up


To set up the LDR in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the LDR and the Arduino. Once the LDR and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.



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


The purpose of this code is to read the values of the LDR resistance using the Arduino's input pin. The LDR's resistance varies in inverse proportion to the light intensity. Which means, we can use these values in the code to distinguish between the presence of light and darkness.


We will also print these values on your computer.


This code is simple and does not require any libraries. You can use it directly or modify it to suit your own needs.


/*
Voltaat learn (https://www.voltaat.com/pages/voltaat-learn)
Link for full tutorial: https://bit.ly/3EPgaat

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



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.


The serial monitor is a great tool that can facilitate communication between the computer and the Arduino. It can allow us to send and receive different commands as well as view information directly from the Arduino.


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.


You should also make sure you have chosen the right baud rate (9600) as specified in the code.

Resources 


Arduino Code

Fritzing Wiring file

Related Tutorials




Most of us are familiar with the switches used in household devices. A relay is a type of switch that can be connected to an Arduino or any other microcontroller.


A soil moisture sensor is a device that measures the amount of water in the soil. The sensor consists of two metal probes that can be inserted into the soil to measure the moisture levels.


In this tutorial, we will use the PIR sensor to send a message to your computer when someone enters your house.