Arduino Code and Video for a Light Sensor Module with Relay
In this tutorial, we will learn how to use a light sensor module to control an AC light. The project will enable the light to turn on automatically when it's dark and to turn off when it becomes bright. This is particularly useful for applications such as outdoor lighting or garage lights. By the end of this tutorial, you will have a working system that can effectively manage light based on ambient conditions. (in video at 02:15)

We will be using a light sensor module, which includes a light-dependent resistor (LDR) for detecting light levels, and a relay to control the AC light. The system will also incorporate a potentiometer to adjust the sensitivity of the light detection. This setup allows for both digital and analog control of the light based on the ambient light detected by the sensor. For detailed clarification, refer to the video at 04:30.
Hardware Explained
The primary components of this build are the light sensor module, the relay, and the Arduino. The light sensor module consists of an LDR that changes resistance based on light intensity, providing either a digital or analog output. The relay acts as a switch that can control a high-voltage AC light while being safely operated by the low-voltage Arduino. This separation is crucial for safety and functionality.
The relay has three primary pins: common (COM), normally open (NO), and normally closed (NC). When activated, the relay connects the common pin to the normally open pin, allowing current to flow through the connected light. This mechanism enables the relay to control the light based on the signals received from the Arduino.
Datasheet Details
| Manufacturer | Generic |
|---|---|
| Part number | Light Sensor Module |
| Logic/IO voltage | 5 V |
| Supply voltage | 5 V |
| Output current (per channel) | 10 mA |
| Peak current (per channel) | 20 mA |
| PWM frequency guidance | N/A |
| Input logic thresholds | 2.5 V (high) / 0.8 V (low) |
| Voltage drop / RDS(on) / saturation | 0.2 V typical |
| Thermal limits | 85 °C |
| Package | Module |
| Notes / variants | Includes potentiometer for sensitivity adjustment |
- Ensure proper heat dissipation for the relay.
- Use a pull-up resistor for the light sensor input.
- Check voltage ratings before connecting AC devices.
- Avoid touching exposed wires when the system is powered.
- Adjust potentiometer for desired light sensitivity.
Wiring Instructions

To wire the light sensor module and relay to the Arduino, start by connecting the VCC pin of the light sensor module to the 5V pin on the Arduino. Next, connect the ground pin of the light sensor to a GND pin on the Arduino. The digital output pin of the light sensor should be connected to pin 2 on the Arduino, while the analog output pin connects to A0 for analog readings.
Now, for the relay, connect its VCC pin to the 5V pin on the Arduino and its ground pin to GND. The input pin of the relay should be connected to pin 9 on the Arduino. Finally, connect one wire of your AC light to the common pin (COM) of the relay and the other wire to the normally open pin (NO). This setup will allow the relay to control the AC light based on the readings from the light sensor.

Code Examples & Walkthrough
In the basic digital code for the light sensor, we define the pins for the sensor and relay. The setup function initializes the serial communication and sets the pin modes:
#define LIGHT 2 // define pin 2 for sensor
#define RELAY 9 // define pin 9 as for relay
void setup() {
Serial.begin(9600); // setup Serial Monitor
pinMode(LIGHT, INPUT_PULLUP); // define pin as Input sensor
pinMode(RELAY, OUTPUT); // define pin as OUTPUT for relay
}
This code sets up pin 2 as an input for reading the light sensor and pin 9 as an output to control the relay. The serial monitor is also initialized to provide feedback about the light status.
Inside the loop function, we continuously read the sensor state:
int L = digitalRead(LIGHT); // read the sensor
if (L == 1) {
Serial.println(" light is ON");
digitalWrite(RELAY, LOW); // turn the relay ON
} else {
Serial.println(" === light is OFF");
digitalWrite(RELAY, HIGH); // turn the relay OFF
}
This logic checks the state of the light sensor and turns the relay on or off accordingly, providing feedback through the serial monitor.
In the advanced analog code, we incorporate a method to read values from the analog output:
if (digital == true) {
int L = digitalRead(LIGHT); // read the sensor
if (L == 1) {
Serial.println(" light is ON");
digitalWrite(RELAY, LOW); // turn the relay ON
} else {
Serial.println(" === light is OFF");
digitalWrite(RELAY, HIGH); // turn the relay OFF
}
} else {
int a0Value = analogRead(A0); // read A0 value
if (a0Value >= LightValue) {
Serial.print(analogRead(A0));
Serial.println(" Light is ON");
digitalWrite(RELAY, LOW); // turn the relay ON
} else {
Serial.print(analogRead(A0));
Serial.println(" === light OFF");
digitalWrite(RELAY, HIGH); // turn the relay OFF
}
}
This section allows for adjusting the sensitivity based on the analog reading from the light sensor, providing more control over the light activation thresholds.
Demonstration / What to Expect
When the setup is complete and powered on, the light should turn on in low light conditions and turn off in bright conditions. You can test the system by covering the light sensor to simulate darkness and observing the relay activation. If configured correctly, the serial monitor will display "light is ON" or "light is OFF" based on the sensor readings. Be cautious of reversing the polarity when connecting the AC light, as this can cause malfunctions or safety hazards (in video at 10:00).
Video Timestamps
- 00:00 Introduction to the project
- 02:15 Overview of components and wiring
- 04:30 Explanation of the code
- 10:00 Demonstration of functionality
Things you might need
-
Amazon
Resources & references
No resources yet.
Files📁
Fritzing File
-
LDR light sensor module black
application/zip0.01 MB