Arduino UNO R4 - LDR Module

This tutorial instructs you how to use an Arduino UNO R4 and an LDR light sensor to check and measure light levels. We will learn the following in detail:

Arduino UNO R4 LDR Light Sensor Module

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×LDR Light Sensor Module
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LDR Light Sensor Module

The LDR light sensor module can be used to detect light or measure light levels around it. It offers two choices with a digital output and an analog output.

Pinout

The LDR light sensor module has four pins:

  • VCC pin: Connect this to VCC (3.3V to 5V).
  • GND pin: Connect this to GND (0V).
  • DO pin: This is a digital output pin. It shows HIGH when dark and LOW when light. You can change the darkness and lightness level by adjusting the potentiometer.
  • AO pin: This is an analog output pin. The output value goes down when it is brighter and goes up when it is darker.
LDR Light Sensor Module Pinout
image source: diyables.io

It also has two LED lights:

  • One PWR-LED indicator shows when the power is on.
  • One DO-LED indicator shows light status on the DO pin: it lights up in the presence of light and turns off in the dark.

How It Works

For the DO pin:

  • The module has a potentiometer to set the light threshold.
  • If the light level around is higher than the threshold set with the potentiometer, the sensor's output pin (DO) is LOW, and the DO-LED (a small light on the module) is off.
  • If the light level around is lower than the threshold, the sensor's output pin (DO) is HIGH, and the DO-LED is on.

For the AO pin:

  • The AO pin gives a reading that changes with the light level.
  • When there is a lot of light, the reading from the AO pin is lower.
  • When it is darker, the reading from the AO pin is higher.
  • The potentiometer does not affect the value on the AO pin. It only changes the threshold for the DO pin.

This way, you can adjust the sensitivity with the potentiometer for the DO pin while still getting precise light level readings from the AO pin.

Wiring Diagram

The light sensor module has two outputs. You can use one or both, based on your needs.

The wiring diagram between Arduino UNO R4 LDR Light Sensor Module

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code - Read value from DO pin

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-ldr-module */ #define DO_PIN 2 // The Arduino UNO R4 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 light_state = digitalRead(DO_PIN); if (light_state == HIGH) Serial.println("The light is NOT present"); else Serial.println("The light is present"); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the light sensor module to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the above code and open it in Arduino IDE
  • Click the Upload button in Arduino IDE to send the code to Arduino UNO R4
  • Block and unblock the light on the LDR sensor module using your hand or an object
  • Check the results on the 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  

If you see that the LED stays on all the time or is off even when it's light, you can turn the potentiometer to adjust the sensor's light sensitivity.

You can now adjust the code to turn on an LED or a light when it detects light, or to make a servo motor spin. For more details and step-by-step guides, look at the tutorials at the end of this document.

Arduino UNO R4 Code - Read value from AO pin

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-ldr-module */ #define AO_PIN A0 // The Arduino UNO R4 pin connected to AO pin of the ldr module void setup() { // initialize serial communication Serial.begin(9600); } void loop() { int light_value = analogRead(AO_PIN); Serial.println(light_value); }

Detailed Instructions

Follow these instructions step by step:

  • Copy the code you see above and open it using the Arduino IDE.
  • Press the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4.
  • Block and then expose the LDR light sensor module using your hand or another object.
  • Check the Serial Monitor to see the outcome.
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

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!