Having smart home devices can make life more convenient and efficient. One simple way to get started with home automation is by building your own smart light switch using an Arduino microcontroller board. With just a few common electronic components, you can make a voice-activated or app-controlled light switch in about 10 minutes.
What You Will Need
To build this simple DIY smart light switch, you will need:
-
Arduino Uno - The brains of the operation. This microcontroller board allows you to program and control electronic components.
-
Relay module - This component acts like a switch, allowing the Arduino to turn the light on and off by energizing the coil in the relay.
-
Jumper wires - To make connections between components.
-
Breadboard - For easily connecting components together without soldering.
-
Light bulb and lamp - For testing the smart switch. An LED bulb is recommended.
-
USB cable - For loading programs onto the Arduino.
-
WiFi module (optional) - Allows wireless control via smartphone app.
Step-by-Step Instructions
Follow these steps to build your own smart light switch using an Arduino:
1. Connect the relay module
-
Use jumper wires to connect the relay module to the Arduino board.
-
Connect the relay VCC pin to Arduino 5V pin. This provides power to the relay coil.
-
Connect the relay GND pin to Arduino GND. Grounds the circuit.
-
Connect the IN pin on the relay to Arduino pin 12. This will be the control pin.
2. Connect the light bulb
-
Connect your light bulb or other device to the COM and NO pins on the relay module.
-
When the relay coil is energized, these pins will be connected together turning on the light.
3. Load the Arduino program
-
Download or copy the following sample Arduino sketch provided below. This simple program turns the relay, and connected light, on and off every 2 seconds.
-
Connect the Arduino board to your computer via USB and load the program using the Arduino IDE software.
```cpp
// Smart Light Switch Demo
int relayPin = 12; //control pin for relay module
void setup() {
pinMode(relayPin, OUTPUT);
}
void loop() {
digitalWrite(relayPin, HIGH); //turn light ON
delay(2000);
digitalWrite(relayPin, LOW); //turn light OFF
delay(2000);
}
```
- Once loaded, the program will run automatically switching the relay on and off.
4. Control wirelessly via WiFi (optional)
-
To control your smart switch wirelessly, add a WiFi module to your circuit and install the Arduino sketch provided below.
-
This allows control of the relay pin via a smartphone app or web page button.
-
Simply enter your WiFi network SSID and password in the sketch before loading.
```cpp
/*
WiFi Smart Light Switch
Turns relay on and off wirelessly
using a webpage button or smartphone app
*/
include
const char* ssid = "yourNetworkName"; // SSID of local network
const char* password = "yourWifiPassword"; // WiFi password
WiFiServer server(80); // Set web server port number to 80
const int relayPin = 12; // Set relay control pin
int relayState = LOW; // State of relay
void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, relayState);
// Connect to WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin(); // Start the web server
}
void loop(){
WiFiClient client = server.available(); // Check for client connections
if (client) { // If a client connects
String request = client.readStringUntil('\r'); // Read request
Serial.println(request);
client.flush(); // Clear input buffer
if (request.indexOf("/RELAYON") != -1) {
digitalWrite(relayPin, HIGH); // Turn relay ON
relayState = HIGH;
}
if (request.indexOf("/RELAYOFF") != -1) {
digitalWrite(relayPin, LOW); // Turn relay OFF
relayState = LOW;
}
// Return response to client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Relay is now: ");
if(relayState==HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("<a href=\"/RELAYON\"\"><button>Turn On </button></a>");
client.println("<a href=\"/RELAYOFF\"\"><button>Turn Off </button></a><br />");
client.println("</html>");
delay(1); // Delay 1 msec to allow web page to load
client.stop(); // Close connection
}
}
```
That's it! In just 10 minutes you now have a fully functioning DIY WiFi-enabled smart light switch using Arduino. You can expand on this project by adding home automation software, voice control, multiple relays to control more devices, and more. The possibilities are endless!
Going Further with Your Smart Light Switch
Looking to take your new smart switch to the next level? Here are some ideas:
-
Add voice control with home assistants like Amazon Alexa, Google Assistant, or Apple Siri using IFTTT applets. Just link your app to your home assistant to enable voice-activated lighting.
-
Connect your DIY switch to home automation software like OpenHAB or Home Assistant for centralized control, customization, automation rules, and integration with other smart devices.
-
Build a nice-looking enclosure for your circuitry using a small project box, 3D printed case, or other housing.
-
Add a touch/button interface on the enclosure to control the light directly without needing your phone.
-
Hook up multiple relays to control groups of lights or other devices from a single switch.
-
Implement scheduling so the light comes on at certain times of day automatically using Arduino code.
-
Add light sensors to enable automatic on/off switching based on ambient light levels.
By combining an Arduino board, a few electronic components, and your own creativity, you can build an inexpensive yet powerful smart light switch to bring automation and convenience to your home lighting.