Arduino Mega - LDR Module

This guide shows you how to use an Arduino Mega board and an LDR light sensor to check and measure light levels. We will learn the following topics in detail:

Arduino Mega LDR Light Sensor Module

Hardware Preparation

1×Arduino Mega
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

Or you can buy the following 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 tell if there is light or how bright it is around it. It has two outputs: digital and analog.

Pinout

The LDR light sensor module has four pins.

  • VCC pin: Connect to power supply (3.3V to 5V).
  • GND pin: Connect to ground (0V).
  • DO pin: This is a digital output. It goes HIGH when it’s dark and LOW when it’s light. You can adjust how dark or light it is by turning the knob (potentiometer).
  • AO pin: This is an analog output. The value goes down when it’s brighter and goes up when it’s darker.
LDR Light Sensor Module Pinout
image source: diyables.io

It also has two small LED lights.

  • One Power LED tells you when the power is on.
  • One DO LED shows the light on the DO pin: it lights up in light and turns off in the dark.

How It Works

About the DO pin:

  • The module has a small knob to set the light level (the limit).
  • If the light around it is brighter than this limit, the DO output is LOW, and the DO-LED (the small light on the module) is off.
  • If the light around it is darker than this limit, the DO output is HIGH, and the DO-LED is on.

About the analog output pin:

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

This way, you can adjust the sensitivity using the small pot for the DO pin, while still getting accurate light readings from the AO pin.

Wiring Diagram

The light sensor module has two output pins. You can use one or both, depending on what you need.

The wiring diagram between Arduino Mega LDR Light Sensor Module

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code - Read value from DO pin

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-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 steps, one by one.

  • Connect the light sensor module to the Arduino Mega following the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the Arduino Mega board and the right COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click Upload in the Arduino IDE to send the code to the Arduino Mega.
  • Cover and uncover the light on the LDR sensor with your hand or an object.
  • Check the results in 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 the LED stays on all the time or goes off even when there is light, you can turn the potentiometer to adjust how sensitive the sensor is to light.

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

Arduino Mega Code - Read value from AO pin

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-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 steps one by one.

  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Cover the LDR light sensor with your hand or another object, then uncover it.
  • Open the Serial Monitor to see the result.
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!