In our earlier articles, we have explained What is a PIR Sensor, How it Works, and Arduino Light Sensor with LDR. In this project, we will set up an automatic light control system using Arduino. You can use Arduino UNO or any Arduino-compatible microcontroller board, such as ATtiny85.
If we use only a PIR Sensor to control the light then it will become ON even during the daytime. So we need a mechanism to keep it off during the daytime. For this reason, we need to add an LDR. This project is easy from the point of required programming skills.
Required Hardware to Build Automatic Room Light Controller
- Arduino Uno ( any other Arduino board will be just fine as long as it provides an Analogical pin ).
- PIR Motion Sensor
- LDR (Photoresistor)
- 10 KOhms resistor
- Relay module
- AC Lamp/Bulb etc
- Breadboard and jumper wires (optional)
Circuit Diagram For Automatic Room Light Controller
Connect one of the LDR legs to the VCC (5v of the Arduino). Connect the other LDR leg to the A0 pin Of Arduino and also to the resistor. Connect the (empty) resistor to the GND of the Arduino.
---
The PIR sensor has three pins :
- VCC
- GND
- OUT
We have powered the PIR sensor using the 5V Rail of the Arduino. The output pin of the PIR Sensor is connected to the 2nd digital pin. You have to wire the ”GND” to the Arduino’s ”GND”.
In this project, we have used a relay for controlling AC light because the Arduino cannot control AC appliances directly, but a relay can do this job. There are two ways to assembly the relay connection :
NC = Normally Closed Connection (we have used this).
NO = Normally Open Connection.
You have to wire the relay’s ”OUT” to the 8th digital pin of the Arduino. Then the relay’s ”GND” to the Arduino’s ”GND” and ”VCC” to the ”VCC”.

In the illustration, I have used an external DC power to complete the circuit. You have to use AC power and the bulb you want to control. You can run the simulator to check how it works:
This project is modified from this person’s project.
Required Sketch
Here is the sketch for the above project. You have to adjust the LightSensorVal < 600 for your project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | int LightSensorVal = 0; int PIRSensorVal = 0; int RelayOutputVal = 0; void setup() { pinMode(A0, INPUT); pinMode(2, INPUT); pinMode(8, OUTPUT); Serial.begin(9600); } void loop() { LightSensorVal = analogRead(A0); PIRSensorVal = digitalRead(2); RelayOutputVal = 8; if (LightSensorVal < 600) { if (PIRSensorVal == HIGH) { digitalWrite(8, HIGH); delay(2000); } else { digitalWrite(8, LOW); delay(1000); } } else { digitalWrite(8, LOW); Serial.println(LightSensorVal); delay(300); } } |