Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
π Ready to explore and create with us? Click [here] and join today!
Light control switchο
This project is a light control switch system. The main idea is to use a photoresistance sensor module to detect the ambient light level and, based on this detection, control a relay module. If the ambient light is below a certain threshold, the relay is switched on. Conversely, if the ambient light is above the threshold, the relay is switched off.
Warning
As a demonstration, we are using a relay to control an RGB LED module. However, in real-life scenarios, this may not be the most practical approach.
While you can connect the relay to other appliances in actual applications, extreme caution is required when dealing with HIGH AC voltage. Improper or incorrect use can lead to severe injury or even death. Therefore, it is intended for people who are familiar with and knowledgeable about HIGH AC voltage. Always prioritize safety.
1. Build the Circuitο

2. Codeο
Open the
03-fun_Light_control_switch.ino
file under the path ofultimate-sensor-kit\fun_project\03-fun_Light_control_switch
, or copy this code into Arduino IDE.
3. Code explanationο
The primary principle behind this project is the use of a photoresistance sensor to detect ambient light levels. Photoresistors change their resistance based on the light falling on them. This property is utilized in the sensor module to give a digital output. When the light is below the set threshold, the sensor sends a HIGH signal to the Arduino. This signal is then used to activate a relay, which can control other devices.
Note
The photoresistance sensor has a potentiometer (a small adjustable knob) that sets the threshold for when it outputs HIGH vs. LOW. This threshold might need to be adjusted based on the desired light levels for switching.
Setting up constants and defining pins
Here, we are defining the pins we will use for the relay and the sensor. We use the
const
keyword because these pin numbers wonβt change throughout the program.const int RelayPin = 6; const int sensorPin = 7;
Initialization in the setup() function
The
setup()
function is executed once when the program starts. Here, we declare theRelayPin
as an output since we will be sending signals to control the relay. We also start Serial communication at 9600 baud for debugging purposes.void setup() { // Set RelayPin as an output pin pinMode(RelayPin, OUTPUT); // Start the Serial communication for debugging Serial.begin(9600); }
Reading Sensor and Controlling Relay
The
loop()
function is where the main logic resides. It repeatedly checks the value from the photoresistance sensor. If the sensor reads a value of 1 (indicative of light level below the threshold), the relay is turned on by settingRelayPin
to HIGH. Otherwise, the relay is turned off by settingRelayPin
to LOW.void loop() { // Read the value from the photoresistance sensor module const int sensorValue = digitalRead(sensorPin); // If the light level is lower than the threshold (sensor value equals 1), // switch the relay module ON. if (sensorValue == 1) { digitalWrite(RelayPin, HIGH); } else // If the light level is higher than the threshold (sensor value equal 0), // switch the relay module OFF. { digitalWrite(RelayPin, LOW); } }