Light Sensor (LDR) Interface

About The Project

In this project, we are learning how to interface a light sensor (LDR) with an Arduino Uno board.

LDR

A Light Dependent Resistor (LDR), also known as a photoresistor or photoconductor, is a type of resistor whose resistance varies significantly with the amount of light falling on its surface.

Potential Dividing Circuit

A voltage divider circuit is used in light measurement with an LDR (Light Dependent Resistor) to convert the varying resistance of the LDR into a measurable voltage. The Arduino can read voltage levels through its analog input pins but cannot directly measure resistance.

Where  R2= 1 K Ohms

R2 is the resistance of the LDR, that depends on the light intensity hit to its surface.

The Arduino reads this voltage (Vx) as an analog input, converting it to a digital value using its Analog to Digital Converter (ADC). The ADC outputs a value between 0 and 1023, corresponding to a voltage range of 0 to 5V.

Program Code

C
// www.matthewtechub.com
// Light Intensity Measurement
int sensorPin=A0;
 int sensorValue;
void setup() {
  Serial.begin(9600);
}

void loop() {
  // Read the analog value from the LDR
  sensorValue = analogRead(sensorPin);

  // Print the analog value to the Serial Monitor
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);

  // Wait for 1 second before taking another reading
  delay(1000);
}

Code Explanation

C
int sensorPin=A0;
 int sensorValue;
  • int sensorPin = A0;: This line declares a variable sensorPin and sets it to A0, which is the analog pin on the Arduino where the LDR (Light Dependent Resistor) is connected.
  • int sensorValue;: This declares a variable sensorValue to store the value read from the LDR.

.

C
void setup() {
  // Initialize serial communication at 9600 baud rate
  Serial.begin(9600);
}
  • void setup() {: This is a setup function that runs once when the Arduino is powered on or reset.
  • Serial.begin(9600);: Initialises serial communication at a baud rate of 9600. This allows the Arduino to send data to the Serial Monitor on your computer.
C
void loop() {
  // Read the analog value from the LDR
  sensorValue = analogRead(sensorPin);

  // Print the analog value to the Serial Monitor
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);

  // Wait for 1 second before taking another reading
  delay(1000);
}
  • void loop() {: This is the main loop function that runs continuously after the setup() function has completed.
  • sensorValue = analogRead(sensorPin);: Reads the analog value from the pin connected to the LDR and stores it in sensorValue. The analogRead function returns a value between 0 and 1023, which represents the voltage level on the analog pin (0 to 5V).
  • Serial.print(“Sensor Value: “);: Prints the string “Sensor Value: ” to the Serial Monitor.
  • Serial.println(sensorValue);: Prints the value stored in sensorValue to the Serial Monitor and moves to the next line. This allows you to see the light intensity as a number between 0 and 1023.
  • delay(1000);: Pauses the execution of the program for 1000 milliseconds (1 second) before repeating the loop. This delay helps in taking readings at 1-second intervals.

Modify Yourself

  1. Modify the program to measure the voltage across the LDR.
  2. Why is ‘pinMode(sensorPin, INPUT);’ not used in this program code?

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!