Arduino UNO R4 - Light Sensor Control Relay
In this tutorial, we will learn how to program Arduino Uno R4 to control the relay based on value read from either LDR light sensor or LDR light sensor module.
By connecting a relay to devices like light bulbs, LED strips, motors, or actuators, we can use a light sensor to control these devices.

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 .
The LDR light sensor is very affordable, but it requires a resistor for wiring, which can make the setup more complex. To simplify the wiring, you can use an LDR light sensor module as an alternative.
Overview of Relay and Light Sensor
If you're unfamiliar with relays and light sensors (their configurations, functionalities, and programming methods), you can learn more by checking out these tutorials:
Wiring Diagram
- The wiring diagram between Arduino Uno R4, relay module and raw LDR light sensor (analog)

This image is created using Fritzing. Click to enlarge image
- The wiring diagram between Arduino Uno R4, relay module and LDR light sensor module (digital)

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Uno R4 and other components.
Arduino UNO R4 Code
- The Arduino Uno R4 code for reading the value from the LDR light sensor and controls the relay
- The Arduino Uno R4 code for reading the value from the LDR light sensor module and controls the relay
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.
- Wire the components 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.
- Connect the Arduino UNO R4 to your computer using a USB cable
- Launch the Arduino IDE, then choose the correct board and port
- Paste the provided code into the Arduino IDE
- Click the Upload button in the Arduino IDE to send the code to the Arduino UND R4

- Sends light to the sensor
- Observe the change in the relay's state
Code Explanation
Check the explanations in the source code comments for each line!