Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Automatic lighting systems are an essential part of modern home automation. They enhance convenience, improve energy efficiency, and provide security by controlling lights based on environmental conditions or user presence. In this article, we will create an automatic lighting system using an Arduino microcontroller. This project will focus on using a PIR (Passive Infrared) sensor to detect motion and a relay module to control the lighting. The Arduino environment is ideal for this project due to its simplicity, versatility, and extensive community support.
Project: The objective of this project is to create a system that automatically turns on a light when motion is detected and turns it off after a set period of inactivity. This system can be used in various applications such as home corridors, bathrooms, or outdoor lighting. The functionalities include:
Components List:
Examples:
Here is the Arduino code for the automatic lighting system:
// Define the pins
const int pirPin = 2; // PIR motion sensor connected to digital pin 2
const int relayPin = 8; // Relay module connected to digital pin 8
// Variables to store the PIR state
int pirState = LOW; // Start with no motion detected
int val = 0; // Variable to read the PIR sensor value
void setup() {
pinMode(pirPin, INPUT); // Set the PIR pin as input
pinMode(relayPin, OUTPUT); // Set the relay pin as output
digitalWrite(relayPin, LOW); // Ensure the relay is off initially
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
val = digitalRead(pirPin); // Read the state of the PIR sensor
if (val == HIGH) { // If motion is detected
digitalWrite(relayPin, HIGH); // Turn on the relay (and the light)
if (pirState == LOW) {
Serial.println("Motion detected! Light ON."); // Print to serial monitor
pirState = HIGH; // Update the PIR state
}
} else {
digitalWrite(relayPin, LOW); // Turn off the relay (and the light)
if (pirState == HIGH) {
Serial.println("No motion. Light OFF."); // Print to serial monitor
pirState = LOW; // Update the PIR state
}
}
delay(1000); // Wait for 1 second before checking again
}
Explanation of the Code:
Pin Definitions:
pirPin
is set to digital pin 2 where the PIR sensor is connected.relayPin
is set to digital pin 8 where the relay module is connected.Setup Function:
pinMode(pirPin, INPUT);
configures the PIR pin as an input.pinMode(relayPin, OUTPUT);
configures the relay pin as an output.digitalWrite(relayPin, LOW);
ensures the relay is off initially.Serial.begin(9600);
initializes serial communication for debugging purposes.Loop Function:
val = digitalRead(pirPin);
reads the state of the PIR sensor.val == HIGH
), the relay is turned on (digitalWrite(relayPin, HIGH);
) and a message is printed to the serial monitor.val == LOW
), the relay is turned off (digitalWrite(relayPin, LOW);
) and a message is printed to the serial monitor.delay(1000);
introduces a 1-second delay before the next reading to avoid rapid toggling.This project demonstrates a simple yet effective way to automate lighting using Arduino, enhancing both convenience and energy efficiency.