Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Smart Lighting with Arduino
Introduction: Smart Lighting is a rapidly growing trend in home automation, allowing users to control their lighting systems remotely and automate them based on various triggers. In this article, we will explore the importance and usefulness of Smart Lighting and provide a detailed example project using Arduino. We will also include a list of the components required for the project and provide example codes with detailed explanations.
Project: The project aims to create a Smart Lighting system using Arduino that can be controlled remotely and automated based on different conditions. The objectives of the project are:
List of Components: To create the Smart Lighting system, the following components are required:
Arduino Uno - 1x Link: [insert link]
Relay Module - 1x Link: [insert link]
Light Sensor Module - 1x Link: [insert link]
PIR Motion Sensor - 1x Link: [insert link]
LED Bulbs - 3x Link: [insert link]
Jumper Wires - Assorted Link: [insert link]
Examples: Example 1: Remote Control This code allows users to control the lighting system remotely using a smartphone or computer. It establishes a connection between the Arduino and a Wi-Fi module, allowing commands to be sent via a web interface.
// Include necessary libraries
#include <WiFi.h>
// Define network credentials
const char* ssid = "YourNetworkSSID";
const char* password = "YourNetworkPassword";
// Initialize server on port 80
WiFiServer server(80);
void setup() {
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
// Start the server
server.begin();
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait for client request
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
client.flush();
// Process the request
if (request.indexOf("/on") != -1) {
// Turn on the lights
// Add code to control the relay module here
} else if (request.indexOf("/off") != -1) {
// Turn off the lights
// Add code to control the relay module here
}
// Send HTTP response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<body>");
// Display the current status of the lights
client.print("Lights are ");
// Add code to check the status of the relay module here
client.println("</body>");
client.println("</html>");
// Disconnect the client
delay(1);
client.stop();
}
Example 2: Automation based on Motion Sensor This code automates the lighting system based on motion detection using a PIR motion sensor. When motion is detected, the lights turn on, and after a specified delay, they turn off.
// Include necessary libraries
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Define pin connections for PIR motion sensor
#define PIR_PIN 2
// Initialize PIR motion sensor
int pirState = LOW;
int val = 0;
void setup() {
// Set PIR pin as input
pinMode(PIR_PIN, INPUT);
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Read PIR sensor value
val = digitalRead(PIR_PIN);
// Check if motion is detected
if (val == HIGH) {
// Motion detected, turn on lights
// Add code to control the relay module here
pirState = HIGH;
Serial.println("Motion detected!");
delay(5000); // Delay for 5 seconds
}
// Check if motion has stopped
if (val == LOW && pirState == HIGH) {
// Motion stopped, turn off lights
// Add code to control the relay module here
pirState = LOW;
Serial.println("Motion stopped!");
}
}