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:
- How to connect the LDR light sensor module to an Arduino UNF R4.
- How to program the Arduino UNO R4 to detect light using the digital signal from the LDR light sensor module.
- How to program the Arduino UNO R4 to read the light intensity using the analog signal from the LDR light sensor module.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.
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.
This image is created using Fritzing. Click to enlarge image
Arduino UNO R4 Code - Read value from DO pin
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.
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
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.