Automatic Lighting With PIR Sensor
by Reza Rachmanto in Circuits > Arduino
1533 Views, 1 Favorites, 0 Comments
Automatic Lighting With PIR Sensor
In our everyday life, we often forget to turn off the lights when we are leaving the house or office. In accordance to that problem, now we are going to create a system where we don't need to worry about having to turn on or turn off the lights when we are in our office or home.
In this project we are going to use:
1. PIR Sensor
2. Arduino Uno
3. LED
PIR Sensor which stands for Passive InfraRed Sensor is a sensor that detects or measures infrared light that is being radiated by objects nearby. The white dome-shaped cap that surrounds it is made to expand the detector's field of vision. The PIR sensor is set on low by default, if it detects movement from the infrared lights nearby, it will trigger a high signal for some time. Some PIR motion sensors has two adjustable potentiometers for changing the sensitivity and the duration of the activation signal. The PIR motion sensor in Tinkercad Circuits does not simulate these adjustments.
In this project we are using LED as the source of light to replace lamps that is usually found in houses and offices.
Creating the Circuit
To start creating the circuit, we will go to Tinkercad and get our components. Search for Breadboard, Arduino Uno, LED, PIR Sensor, and a Resistor.
First we're going to wire the LED to the breadboard and arduino, So we will plug the LED to two different breadboard rows so that the cathode (negative, shorter leg) connects to the resistor leg (we're setting the resistor to 220 Ohm). Connect other resistor leg with the ground. Then we can plug the LED anode (positive, longer leg) to pin 13 of the Arduino.
Now we will set up the PIR Sensor to the Arduino. We will connect the right leg of the sensor to Ground, the middle leg of the sensor to Power (5V) and the left leg of the sensor to Digital Pin 2.
Now the Circuit is finished and should look like the image we provide.
The Code.
Now we will code the Arduino so the LED will light up if the sensor detects movement.
int buttonState = 0;
In this part of the code, we are setting the variable to store the current state of the sensor.
void setup() { pinMode(2, INPUT); pinMode(13, OUTPUT); }
Here, we are setting the pins. We set Pin 2 as an input to see if there are any movement in the area from the sensor and Pin 13 as an Output to light up the LED.
void loop() { buttonState = digitalRead(2); if (buttonState == HIGH) { digitalWrite(13, HIGH); delay(2000); } else { digitalWrite(13, LOW); } delay(10); }
Now we are finally on the main part of the code. Here we have buttonState = digitalRead(2); to check the state of Pin 2 and stores that state in the buttonState variable we created earlier. Then there is an if statement where if the condition is met, the LED light up. If not, the code inside the else part will be executed instead, where the LED is turned off. In the code, we use delay(2000); as a way to stop the LED from turning off immediately. Currently the code stops that for 2 seconds, but we can adjust it to 5 seconds, 10 seconds, or even hours.
We Are Done!!
You can see our finished project here.