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


Arduino Nano microcontroller

5v Power Adapter For the Arduino

38 kHz Infrared Receiver Module


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)


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.


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()




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



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;



pos = 90;



digitalWrite(outputPin, HIGH); outputState = 0;


else if ((outputState == 0)&&(enableOutput==1))


Serial.println("State=0,Enable=1"); //displays stored code

pos = 180;



pos = 90;



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.

<p>I gave the code a lookover and found you must complete the #include statements which should be &quot;</p><p>#include &lt;IRremote.h&gt;<br>#include &lt;Servo.h&gt;&quot;</p><p>then when that is done you get a compile error for the nano of </p><p>&quot;</p><p>/opt/arduino-1.5.8/libraries/RobotIRremote/src/IRremoteTools.cpp:5:16: error: 'TKD2' was not declared in this scope<br> int RECV_PIN = TKD2; // the pin the IR receiver is connected to<br> ^<br>Error compiling.</p><p>The code given is trying to allocate the pin twice it seems and it does not recognise &quot;TKD2&quot; </p><p>Can you tell me what you changed from the original library?</p><p>Note Arduino 1.5.8 the library is already there under RobotIRremote although I don't know if it is correct or complete.</p>
<p>Hey thanks for catching that,</p><p>So it sounds like you don't have the library installed correctly,</p><p>I'll add some pictures to show what it should look like.</p><p>I had to delete the RobotIRremote for it to work correctly, let me know if that works out for you.</p>
<p>Looking further here</p><p><a href="http://forum.arduino.cc/index.php/topic,188236.0.html" rel="nofollow">http://forum.arduino.cc/index.php/topic,188236.0.h...</a></p><p>I found the following</p><p>&quot;Removing <br> the files - libraries\RobotIRremote\IRremoteTools.cpp and <br>libraries\RobotIRremote\IRremoteTools.h solved the problem. They are <br>example files which somehow are getting included. You can move those two <br> files to some other location as backup.&quot;</p><p>I went to my libraries <br>file and did this and can confirm that the example given above then <br>compiles fine without downloading a new library. Its an Arduino <br>libraries problem .</p><p>Either way will work</p>
<p>Excuse me for dwelling on this but I wanted to find the real cause and suggest a fix to Arduino.cc.</p><p>What is happening is the RobotIRRemote library included with Arduino 1.5.8 , IRremoteTools.cpp sets RECV_PIN to TKD2 which is the name of the pin on the robot they use . You could change that to pin 11 in the library then restart the Arduino IDE or you could change your program slightly as below to have it compile normally without a download needed whatever. I havenot built the circuit yet so if you can you might confirm this runs in practice.</p><p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p><p>#include &lt;Servo.h&gt;<br>#include &lt;IRremote.h&gt; //need to get this library <a href="https://github.com/shirriff/Arduino-IRremote" rel="nofollow"> https://github.com/shirriff/Arduino-IRremote</a><br>int REC_PIN = 11;<br><br>IRrecv irrec(REC_PIN);<br><br>decode_results result;<br><br>unsigned long CurrentValue = 0;<br><br>unsigned long StoredCode = 0;<br><br>const int buttonPin = 6; // the number of the pushbutton pin<br><br>const int ledPin = 4; // the number of the LED pin<br><br>const int outputPin = 3; // the number of the output LED pin<br><br>const int servoPin = 9;<br><br>int enableOutput = 0;<br><br>int buttonState = 0; // variable for reading the pushbutton status<br><br>int RecordState = 0; //is the reciever in record mode<br><br>int outputState = 1; //is the output on or off<br><br>Servo myservo;<br><br>int pos = 0;<br><br>void setup()<br><br>{<br><br>myservo.attach(9);<br><br>Serial.begin(9600);<br><br>irrec.enableIRIn(); // Start the receiver<br><br>pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:<br><br>pinMode(outputPin, OUTPUT); // initialize the pushbutton pin as an input:<br><br>pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:<br><br>}<br><br>void loop()<br><br>{ buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:<br><br>if (irrec.decode(&amp;result)) { // if a signal is detected, store the value<br><br>CurrentValue = (result.value);<br><br>Serial.println(&quot;code received&quot;); //displays stored code for reference<br><br>// if the recieved value equals the programed value, then toggle the output state<br><br>if(CurrentValue == StoredCode) {<br><br>enableOutput = 1;<br><br>}<br><br>if (RecordState == 1) { // if the record mode is activated store the current value as the programed value StoredCode = CurrentValue;<br><br>RecordState = 0;<br><br>digitalWrite(ledPin, LOW);<br><br>Serial.println(StoredCode); //displays stored code for reference<br><br>}<br><br>delay(500);<br><br>irrec.resume(); // Receive the next value<br><br>}<br><br>else //if no signal is detected, then the current value is 0<br><br>{ CurrentValue = 0;<br><br>} if (buttonState == HIGH) { // check if the record button is pressed.<br><br>// if it is, the buttonState is HIGH:<br><br>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<br><br>RecordState = 1;<br><br>}<br><br>if((outputState == 1)&amp;&amp;(enableOutput==1)) {//set the appropriate output state Serial.println(&quot;State=1,Enable=1&quot;); //displays stored code for reference<br><br>pos = 1;<br><br>myservo.write(pos);<br><br>delay(1000);<br><br>pos = 90;<br><br>myservo.write(pos);<br><br>delay(15);<br><br>digitalWrite(outputPin, HIGH); outputState = 0;<br><br>}<br><br>else if ((outputState == 0)&amp;&amp;(enableOutput==1))<br><br>{<br><br>Serial.println(&quot;State=0,Enable=1&quot;); //displays stored code<br><br>pos = 180;<br><br>myservo.write(pos);<br><br>delay(1000);<br><br>pos = 90;<br><br>myservo.write(pos);<br><br>delay(15);<br><br>digitalWrite(outputPin, LOW);<br><br>outputState = 1;<br><br>}<br><br>enableOutput = 0;<br><br>}</p><p>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</p>
<p>ye sremoving work self tested</p>
<p>Has anyone got it to work? I follow everything beside using Mega, Uno...none of them worked. Cannot even get outputPin led to light up in the first place... </p>
<p>is there a reason you didn't want to just use a relay?</p>
<p>I wanted the lightswitch to be able to function normally so I could just walk over and turn it on and off, if a relay was used I would have had to use the remote everytime I turned it on or off.</p>
<p>There's a couple of easy ways to get arround that. Well... At least easier than using a servo. :)</p>
<p>You could use a 3-way wall switch and a single pole double throw relay wired as you would for two 3-way wall switches? But using the servo is kind of a neat way to provide a good isolation barrier. Also even a good relay can be expensive. And don't forget about the insurance liability for using non-UL components in your house wiring. Besides, if a relay was used as a 3-way switch there would be one off state that would required the relay to be energized so the servo is really a neat idea. I confess, I haven't looked at the code yet so I don't understand how you keep track of on and off states or does the circuit only turn the switch off?</p>
<p>I'm thinking about this problem too some time now... Need some easy way to seamlessly integrate classic wall switches and some kind of home automation light controlling. Using 3-way wall switches and bistable relay-s are a better solution. Bistable relays does not need to be energised all the time for one state, just for a short period of time during the switch over. Another solution I'm thinking about a lot is completely isolate the wall switch from the mains, and just use it as an input to an MCU and for every manual switch (on or off) the MCU should switch the bistable relay too.</p>
<p>Ah, yes... bistables would work well but large ones tend to be expensive and two coils are usually need. I know that the old X10 Appliance Modules have a really slick low cost high current latching relay inside that uses a single bipolarity coil but I have yet to be able to source one. Also, they are only SPST so while suitable to control the AC load from one location, it wouldn't work in the 3-way application. For that, maybe a ratchet-type latching relay with a single coil doing pulse-on pulse-off action to SPDT contacts.</p><br>
<p>You would obviously need to modify the code, but adding a push-to-make momentary switch would enable you to toggle the lights on or off so you could use a relay and still have 'manual' control over the lights.</p>
<p>I have 240 Volts and I tested a little solid state relay today which would save you a lot of time . The switching is done by an IR LED built in and a triac switches the AC . The arduino switches it with less than 20 mA and the ACC current is up to 8 AMPS. Preferably put a heat sink and keep under 5 Amps. $6 US</p><p>https://www.sparkfun.com/products/10636</p>
<p>Does not have solid state relays some level of leakage in closed state too? It would not be usable in every situation. I had played with an SSR using some led driver and led strip connected to it, and it blinked in the closed state of the relay.</p>
<p>I tried with no luck, ended up buying the #1 best selelr for remote control light switch on Amazon http://www.amazon.com/dp/product/B00DQELHBS</p>
<p>Overall I tried it but it didn't work. Lacks details. :(</p>
<p>Thanks for including my link! I have been meaning to update Instructables with some example code for an RF version but sadly I have not had much time :(</p><p>Can I suggest you give these a try? http://lowpowerlab.com/moteino/ they are fantastically easy to work with and have a very long range.</p>
Hi im just a begginner can we se more picture detail by detail in puting the servo to the switch and where do we put the gauze wire
<p>I would have just used a solid state relay. That seems a lot safer than hacking up a light switch. You could always add a push button to manually control it too.</p>
<p>Did you ever heard about &quot;Blynk&quot;?! I across it few days ago, it might save you some time.</p><p>https://www.kickstarter.com/projects/167134865/blynk-build-an-app-for-your-arduino-project-in-5-m?ref=popular</p>
<p>What are you using for a power source for the nano? Once you shove it in the wall, I imagine it's a PITA to change out the power source. I'd rather not have to go around my house every couple weeks replacing batteries behind my light switches.</p>
not that I don't love a good hack but.....<br>http://m.homedepot.com/p/Chamberlain-Remote-Light-Switch-WSLCEV/202894597<br>
<p>how to fix this error</p>
<p>Arduino: 1.5.6-r2 (Windows 7), Board: &quot;Arduino Uno&quot;</p><p>C:\Program Files\Arduino\libraries\RobotIRremote\src\IRremoteTools.cpp:5: error: 'TKD2' was not declared in this scope</p><p> This report would have more information with</p><p> &quot;Show verbose output during compilation&quot;</p><p> enabled in File &gt; Preferences.</p>
<p>Nice project! This would work awesome with a Hover gesture board: www.justhover.com</p>
thats a very expensive light switch. You should look i to programming a cheap microcontroller thats not part of a development board.
<p>Very nice!</p><p>I wonder if it could be used with 12V DC switches. I live full time in a Motor Home and all the lighting is 12V DC. Remote would be super.</p>

About This Instructable




More by blubflubify:Remote Control Light switch (ARDUINO) 
Add instructable to: