Circuit Creation

LDR Sensor Project using Arduino

The LDR (Light Dependent Resistor) sensor project helps you measure ambient light levels. This tutorial walks you through connecting the sensor, reading values with Arduino, and displaying or acting on the data.

Beginner

Components Required

Circuit Connections

Build a simple voltage divider using the LDR and a fixed resistor. The middle node will connect to an analog input on the Arduino. Make sure power is removed while wiring the circuit.

  1. Place the LDR and 10 kΩ resistor on the breadboard in series.
  2. Connect one end of the LDR to 5 V on the Arduino.
  3. Connect the free end of the resistor to GND on the Arduino.
  4. Connect the junction between LDR and resistor to A0 (analog pin 0).
  5. (Optional) Connect an LED to digital pin 13 with a 220 Ω resistor to indicate darkness.
Arduino LDR sensor circuit connection

Here is the circuit diagram for reference. Analog pin A0 measures the voltage from the divider.

Arduino Code

                
                // LDR Sensor Project
                // Circuit Creation

                const int ldrPin = A0;      // analog input pin
                int ldrValue = 0;

                void setup() {
                  Serial.begin(9600);
                  pinMode(13, OUTPUT);    // optional LED indicator
                }

                void loop() {
                  ldrValue = analogRead(ldrPin);
                  Serial.println(ldrValue);

                  // turn on LED when it is dark (value less than threshold)
                  if (ldrValue < 500) {
                    digitalWrite(13, HIGH);
                  } else {
                    digitalWrite(13, LOW);
                  }

                  delay(500);
                }
                
            

Upload the Code

  1. Connect the Arduino board to your computer using the USB cable.
  2. Open the Arduino IDE and paste the code into a new sketch.
  3. Select the correct board and port from the Tools menu.
  4. Click the Upload button.
  5. Open the Serial Monitor at 9600 baud to observe the light readings.

Code Explanation

Let us understand how the Arduino code works step by step. This will help you modify the project later and build more advanced circuits.

Declaring the LDR Pin

const int ldrPin = A0; sets the analog pin used to read the voltage divider.

setup() Function

The setup() function initializes serial communication and configures pin 13 as an output for the optional LED indicator.

loop() Function

The loop() function continuously reads the analog value from the LDR, prints it over the serial port, and toggles the LED based on a threshold.

analogRead() Function

The analogRead(ldrPin) command returns a value from 0 (dark) to 1023 (bright).

Serial.println()

The Serial.println() sends the sensor reading to the Serial Monitor, allowing you to observe light changes in real time.

Common Mistakes & Troubleshooting

What You Learned