Light Intensity Comparator using Arduino and Photoresistor

Light Intensity Comparator using Arduino and Photoresistor

The objective of this project is to create a light intensity comparator using Arduino to monitor the light levels from a photoresistor (LDR). When the light intensity crosses a threshold, an LED will turn on or off. This project covers topics like comparison operators (>, <, >=, <=), analog input, and if control structures in Arduino.

Fundamental Programming Concepts

  • Comparison Operators (>, <, >=, <=): Used to compare light intensity values and determine whether to turn the LED on or off.
  • Analog Input: Reads values from the photoresistor and converts them into usable data for comparison.
  • Control Structures (if): Executes specific actions (e.g., turning on/off an LED) based on the comparison of light intensity with a threshold value.

Requirement Components

For this Arduino light intensity comparator project, you will need:

  • Arduino Uno Board
  • Photoresistor (LDR)
  • LED
  • 10kΩ Resistor (for the photoresistor)
  • 220Ω Resistor (for the LED)
  • Breadboard
  • Jumper Wires
  • USB Cable (for connecting the Arduino to your computer)

Circuit Diagram

Circuit Connection for Light Intensity Comparator 

Component Arduino Pin
Photoresistor (LDR) A0 (Analog Input)
Photoresistor (GND) GND
10kΩ Resistor Between A0 and GND
LED (Anode) Pin 9
LED (Cathode) GND
220Ω Resistor Between LED anode and Pin 9

How to Connect the Circuit

  1. Photoresistor (LDR): Connect one leg of the photoresistor to A0 (analog input) on the Arduino and the other leg to 5V. Place a 10kΩ resistor between A0 and GND to form a voltage divider.
  2. LED: Connect the anode (longer leg) of the LED to Pin 9 on the Arduino and the cathode (shorter leg) to GND using a 220Ω resistor.
  3. Double-check the connections to ensure proper wiring.

Explanation of Circuit

  • The photoresistor (LDR) changes its resistance based on the light intensity. The Arduino reads the voltage drop across the LDR and converts it into a digital value between 0 and 1023 using the analogRead() function.
  • The Arduino compares this value with a threshold. If the light intensity is lower than the threshold, the LED turns on; otherwise, it turns off. This showcases how to build a light intensity comparator using Arduino.

Programming Section

Arduino Syntax

Topic Name Syntax Explanation
Analog Read analogRead(pin) Reads the analog value (0-1023) from the specified pin.
Digital Write digitalWrite(pin, value) Sets the pin to HIGH or LOW to turn an LED on or off.
Comparison Operators >, <, >=, <= Used to compare light intensity to a threshold.
If Condition if (condition) {} Executes code when a condition, such as light level, is true.

Arduino Code for Light Intensity Comparator 

Here’s the Arduino code for the light intensity comparator:

// Global variables
const int sensorPin = A0;   // Pin for the photoresistor (LDR)
const int ledPin = 9;       // Pin for the LED
int sensorValue = 0;        // Variable to store light intensity
int threshold = 500;        // Threshold for light intensity
void setup() {
  pinMode(ledPin, OUTPUT);  // Set LED pin as output
  Serial.begin(9600);       // Initialize serial communication for debugging
}
void loop() {
  sensorValue = analogRead(sensorPin);  // Read light intensity from the LDR
  Serial.print("Light Intensity: ");
  Serial.println(sensorValue);          // Print the light intensity to the Serial Monitor
  // Compare light intensity with the threshold value
  if (sensorValue < threshold) {
    digitalWrite(ledPin, HIGH);  // Turn on the LED if light intensity is below the threshold
  } else {
    digitalWrite(ledPin, LOW);   // Turn off the LED if light intensity is above the threshold
  }
  delay(1000);  // Delay for stability
}

Steps to Upload Code:

  1. Connect the Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and select the correct Board and Port.
  3. Copy and paste the code into a new sketch.
  4. Click the Upload button to upload the code to the Arduino.
  5. Open the Serial Monitor to view the light intensity values and check if the LED is turning on or off based on the threshold.

Check Output

Once the code is uploaded, place your hand over the photoresistor to simulate changes in light levels. The Serial Monitor will display the light intensity, and the LED will turn on or off depending on whether the light intensity is above or below the threshold.

Troubleshooting Tips

  • LED not turning on? Double-check the connection of the LED, ensuring the anode is connected to Pin 9 and the cathode is connected to GND.
  • Light readings not changing? Ensure the photoresistor (LDR) is correctly connected, and that the 10kΩ resistor is properly placed.
  • No output on Serial Monitor? Make sure the baud rate in the Serial Monitor is set to 9600 and that you have selected the correct COM port.

Further Exploration

  • Adjust the Threshold: Change the threshold value in the code to control the sensitivity of the light comparator.
  • Multiple LEDs: Add more LEDs to create different light intensity thresholds, turning them on or off based on varying light levels.
  • LCD Display: Display the light intensity values and LED status on an LCD instead of the Serial Monitor.

Note

This Arduino light intensity comparator project is an excellent way to learn about analog inputs, comparison operators, and if statements. These fundamental concepts are vital for building more advanced Arduino sensor projects.

FAQ

Q1: What is a photoresistor (LDR)?
A photoresistor, or Light Dependent Resistor (LDR), is a resistor that changes its resistance based on the light intensity. As light increases, resistance decreases, and as light decreases, resistance increases.

Q2: How does analogRead() work in Arduino?
The analogRead() function reads the voltage on an analog pin and converts it into a value between 0 and 1023, which corresponds to the light intensity detected by the LDR.

Q3: What is the threshold in this project?
The threshold is a pre-set value of light intensity. When the light intensity drops below this value, the LED turns on to indicate low light conditions.

Q4: Why do I need a pull-down resistor with the photoresistor?
The pull-down resistor ensures that the voltage across the A0 pin remains stable when the LDR is not exposed to light, allowing for accurate light intensity readings.

Q5: Can I use another type of sensor for this project?
Yes, you can use other sensors, such as temperature sensors, with slight modifications to the code to compare different types of analog inputs.