Arduino - LDR Module

The LDR light sensor module is capable of detecting and measuring light in the surrounding environment. The module provides two outputs: a digital output (LOW/HIGH) and an analog output.

In this tutorial, we will learn how to use an Arduino and an LDR light sensor module to detect and measure the light level. Specifically, we will cover the following:

LDR Light Sensor Module
image source: diyables.io

Afterward, you can modify the code to activate an LED or a light bulb (via a relay) when it detects light.

If you prefer a light sensor in its raw form, I suggest exploring the tutorial on the Arduino - Light Sensor.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×LDR Light Sensor Module
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

About LDR Light Sensor Module

The LDR light sensor module can be utilized to detect the presence of light or measure the light level in the surrounding environment. It provides two options via a digital output pin and analog output pin.

Pinout

The LDR light sensor module includes four pins:

  • VCC pin: It needs to be connected to VCC (3.3V to 5V).
  • GND pin: It needs to be connected to GND (0V).
  • DO pin: It is a digital output pin. It is HIGH if it is dark and LOW if it is light. The threshold value between darkness and lightness can be adjusted using a built-in potentiometer.
  • AO pin: It is an analog output pin. The output value decreases as the light gets brighter, and it increases as the light gets darker.
LDR Light Sensor Module Pinout
image source: diyables.io

Furthermore, it has two LED indicators:

  • One PWR-LED indicator for power.
  • One DO-LED indicator for the light state on the DO pin: it is on when light is present and off when it is dark.

How It Works

For the DO pin:

  • The module has a built-in potentiometer for setting the light threshold (sensitivity).
  • When the light intensity in the surrounding environment is above the threshold value (light), the output pin of the sensor is LOW, and the DO-LED is on.
  • When the light intensity in the surrounding environment is below the threshold value (dark), the output pin of the sensor is HIGH, and the DO-LED is off.

For the AO pin:

  • The higher the light intensity in the surrounding environment (light), the lower the value read from the AO pin.
  • The lower the light intensity in the surrounding environment (dark), the higher the value read from the AO pin.

Note that the potentiometer does not affect the value on the AO pin.

Wiring Diagram

Since the light sensor module has two outputs, you can choose to use one or both of them, depending on what you need.

  • The wiring diagram between Arduino and the LDR light sensor module when using DO only.
Arduino LDR Light Sensor Module wiring diagram

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino and the LDR light sensor module when using AO only.
Arduino LDR Module wiring diagram

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino and the LDR light sensor module when using both AO an DO.
Arduino Light Sensor Module wiring diagram

This image is created using Fritzing. Click to enlarge image

The real wiring:

Arduino LDR Light Sensor Module connection

Arduino Code - Read value from DO pin

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ldr-module */ #define DO_PIN 2 // Arduino's pin connected to DO pin of the ldr module void setup() { // initialize serial communication Serial.begin(9600); // initialize the Arduino's pin as an input pinMode(DO_PIN, INPUT); } void loop() { int lightState = digitalRead(DO_PIN); if (lightState == HIGH) Serial.println("The light is NOT present"); else Serial.println("The light is present"); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Cover and uncover the LDR light sensor module by your hand or something
  • See the result on Serial Monitor.
COM6
Send
The light is present The light is present The light is NOT present The light is NOT present The light is NOT present The light is present The light is present The light is present
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Please keep in mind that if you notice the LED status remaining on constantly or off even when there is light, you can adjust the potentiometer to fine-tune the light sensitivity of the sensor.

Now we can customize the code to activate an LED or a light when the light is detected, or even make a servo motor rotate. You can find more information and step-by-step instructions in the tutorials provided at the end of this tutorial.

Arduino Code - Read value from AO pin

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ldr-module */ #define AO_PIN A0 // Arduino's pin connected to AO pin of the ldr module void setup() { // initialize serial communication Serial.begin(9600); } void loop() { int lightValue = analogRead(AO_PIN); Serial.println(lightValue); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Cover and uncover the LDR light sensor module by your hand or something
  • See the result on Serial Monitor.
COM6
Send
145 146 146 572 678 945 956 1001 1002 1012 1013 645 546 346 172
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES