Introduction: Remote Control Light Switch (ARDUINO)
So I was sick of falling asleep with the light on...
And this is what i came up with! Parts of this instructable were borrowed from other people but a lot of what was borrowed has been modified for this application.
I hope you enjoy building this project because it's pretty sick!
Step 1: Materials
Materials:
Arduino Nano microcontroller
5v Power Adapter For the Arduino
38 kHz Infrared Receiver Module
Red LED
Green LED
Momentary Pushbutton Switch
Two 100 ohm Resistors
10 kohm Resistor
9 gram servo
Perf Board
12 - 14 gauge wire
Remote control (just use an old tv remote)
Tools:
Wire Strippers
Soldering Iron and Solder
Drill and Bit Set
Sharp Knife
Hot Glue Gun
Step 2: Prepare Your Light Switch
Now we need to prepare our light switch and attach the servo so it can be toggled.
I didn't come up with the idea for this step but borrowed it from this website.
http://www.echotwek.com/wp/2014/03/20/arduino-cont...
For this step it may be easier to just look at the photos.
Step 1
Gently pry the lightswitch paddle off.
Careful not to lose this little piece of metal
Step 2
Measure out some wire
And give it a bend
And another bend
Strip the tip of the short side up to the plastic block
Drill a small hole through the plastic block to allow the stripped wire to pass through
Make sure the wire fits snugly inside of the paddle
Dremel out a valley on the opposite side of your drill hole
Notch out a valley on the outer part of the plastic clip big enough for a stripped bit of wire to fit into
Cut the long side of the wire about an inch past the paddle’s length
And strip back until the middle of the paddle
Fit the wire into the valley you notched out
And bend it upwards so that it is flush to the outside.
Hot glue down your handywork
Step 3
Remove the tab on the opposite side of the screw terminals
Drill a 1/4in hole down the middle of the tab you just removed
Snap it together, and make sure it toggles with ease
Step 4
Position the servo so that it is flush to the switch body
Hot glue down your handy work.
Shape the wire so that it can easily be toggled by the servo. Yet does not protude out to far past the lightswitch body
The mechanical portion is complete!
Step 3: Download and Install the IR Remote Library
This project uses an IR remote library that was developed by Ken Shirriff. This library lets you decode the signal coming from your remote. You can check out his original project and setup here: http://www.righto.com/2009/08/multi-protocol-infr...
The first thing that you need to do for this project is download the library zip file. You can find it here: https://github.com/shirriff/Arduino-IRremote
Click "Download ZIP" on the right side of the page and save the zip file. Then unzip it. Rename the folder "IRRemote" (unless that name is already being used).
Then copy the folder into your libraries directory. The libraries directory should contain the folder "IRremote." If for some reason you already have a folder with this name, then you may need to rename it. The IRremote folder should contain the files. A lot of problems experienced when uploaded in the code, are caused be the library not being loaded in the correct location. UPDATE: In version 1.5.8 of the Arduino software, several of the default libraries were changed. This created duplicate IRremote libraries. So if you encounter errors, this is likely the source. You may need to change the file names to make it work correctly.
You will most likely get this error below:
/opt/arduino-1.5.8/libraries/RobotIRremote/src/IRremoteTools.cpp:5:16: error: 'TKD2' was not declared in this scopeint RECV_PIN = TKD2; // the pin the IR receiver is connected to^Error compiling.
You need to remove the files - libraries\RobotIRremote\IRremoteTools.cpp and libraries\RobotIRremote\IRremoteTools.h this should solve the problem. They are example files which somehow are getting included. You can move those two files to some other location as backup.
Step 4: The Arduino Code
So here is the code to actually make this project work, Make sure you have downloaded the IR remote library first or it will not work.
Quick explanation of the code:
When the pushbutton is pressed the red LED will turn on, and the arduino will then wait for any IR signal to come in. Once A signal is detected that signal gets saved and is then used to toggle the servo.
The servo is set to always be at 90 degrees until the signal is received, it then moves to either 180 degrees or to 1 degree which makes the servo arm hit the wire, switching the light on and off.
#include <IRremote.h>
#include <Servo.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long CurrentValue = 0;
unsigned long StoredCode = 0;
const int buttonPin = 6; // the number of the pushbutton pin
const int ledPin = 4; // the number of the LED pin
const int outputPin = 3; // the number of the output LED pin
const int servoPin = 9;
int enableOutput = 0;
int buttonState = 0; // variable for reading the pushbutton status
int RecordState = 0; //is the reciever in record mode
int outputState = 1; //is the output on or off
Servo myservo;
int pos = 0;
void setup()
{
myservo.attach(9);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(outputPin, OUTPUT); // initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
void loop()
{ buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (irrecv.decode(&results)) { // if a signal is detected, store the value
CurrentValue = (results.value);
Serial.println("code received"); //displays stored code for reference
// if the recieved value equals the programed value, then toggle the output state
if(CurrentValue == StoredCode) {
enableOutput = 1;
}
if (RecordState == 1) { // if the record mode is activated store the current value as the programed value StoredCode = CurrentValue;
RecordState = 0;
digitalWrite(ledPin, LOW);
Serial.println(StoredCode); //displays stored code for reference
}
delay(500);
irrecv.resume(); // Receive the next value
}
else //if no signal is detected, then the current value is 0
{ CurrentValue = 0;
} if (buttonState == HIGH) { // check if the record button is pressed.
// if it is, the buttonState is HIGH:
while (buttonState == HIGH) buttonState = digitalRead(buttonPin); //wait for the button to be released digitalWrite(ledPin, HIGH); //turn on the LED to indicate that record mode is on
RecordState = 1;
}
if((outputState == 1)&&(enableOutput==1)) {//set the appropriate output state Serial.println("State=1,Enable=1"); //displays stored code for reference
pos = 1;
myservo.write(pos);
delay(1000);
pos = 90;
myservo.write(pos);
delay(15);
digitalWrite(outputPin, HIGH); outputState = 0;
}
else if ((outputState == 0)&&(enableOutput==1))
{
Serial.println("State=0,Enable=1"); //displays stored code
pos = 180;
myservo.write(pos);
delay(1000);
pos = 90;
myservo.write(pos);
delay(15);
digitalWrite(outputPin, LOW);
outputState = 1;
}
enableOutput = 0;
}
Step 5: The Schematic
This circuit uses a 38 kHz Infrared Receiver Module to detect the signal from the remote. The right pin of this connected to 5V. The middle pin is connected to GND. The Left pin connects to digital pin 11 on the board.
The circuit also has two indicator LEDs. One indicates that the receiver is in programming mode and the other indicates whether the output is on or off. One end of each LED is connected to GND. The other end is connected to a 100 ohm series resistor. The resistors are then connected to digital pins 3 and 4.
A momentary switch is used to set programming mode. One end of the switch is connected to 5V. The other end of the switch is connected to digital pin 6 and a 10 kohm resistor. The other end of the resistor is connected to GND.
The Servo is used to flip the light switch:
the orange wire connects to Pin 9, The Red wire connects to 5V, and finally the brown wire is connected to ground.
I used an arduino nano because of it's small size which would fit into an outlet enclosure.
You may want to refer to the diagram for all the pin connections on the arduino nano,
Step 6: Test the Circuit on a Breadboard
It is always a good idea to test your circuit on a breadboard before soldering it together.
When the Arduino is powered, the output indicator (green) LED should turn on. This indicates that the output at pin 2 is HIGH and can be used to activate another circuit.
To program the receiver, press the button. When the button is released, the programming mode indicator (red) LED will turn on.
Now point your remote at the receiver module and press a button. If the Arduino registered the signal, the programming mode LED will turn off. The receiver is now programmed.
Pressing this button on the remote again will now cause the servo to move which will then toggle the light switch off and on. The output state is indicated by the LED.
If you connect the Arduino to your computer you can use the serial monitor function to observe the value for the signals that you are programming.
Step 7: Now Make It Permanent
Once you get everything working pull your circuit of the breadboard and start soldering it together,
I would test that everything still functions afterwards (sometimes a wire could be soldered out of place)
After it was all soldered I grabbed a blank wall mount and drilled some holes to mount my switch and pushbutton.
When installing your light switch into the wall your going to want to be extremely careful (maybe even turn off the power where you are installing it) Because you will be dealing with high AC voltages.
Step 8: Manually Program IR Values (optional)
If you plug the Arduino into your computer, you can use the Serial Monitor tool to view the value of the IR signals for the various buttons on your remote. This gives you the option of manually inserting these values into the code. By doing this you can program responses to multiple buttons. It also means that you will not have to reprogram the receiver whenever the power is disconnected.