Remote Control Light Switch (ARDUINO)

133K37231

Intro: 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.

29 Comments

I gave the code a lookover and found you must complete the #include statements which should be "

#include <IRremote.h>
#include <Servo.h>"

then when that is done you get a compile error for the nano of

"

/opt/arduino-1.5.8/libraries/RobotIRremote/src/IRremoteTools.cpp:5:16: error: 'TKD2' was not declared in this scope
int RECV_PIN = TKD2; // the pin the IR receiver is connected to
^
Error compiling.

The code given is trying to allocate the pin twice it seems and it does not recognise "TKD2"

Can you tell me what you changed from the original library?

Note Arduino 1.5.8 the library is already there under RobotIRremote although I don't know if it is correct or complete.

Hey thanks for catching that,

So it sounds like you don't have the library installed correctly,

I'll add some pictures to show what it should look like.

I had to delete the RobotIRremote for it to work correctly, let me know if that works out for you.

Looking further here

http://forum.arduino.cc/index.php/topic,188236.0.h...

I found the following

"Removing
the files - libraries\RobotIRremote\IRremoteTools.cpp and
libraries\RobotIRremote\IRremoteTools.h solved the problem. They are
example files which somehow are getting included. You can move those two
files to some other location as backup."

I went to my libraries
file and did this and can confirm that the example given above then
compiles fine without downloading a new library. Its an Arduino
libraries problem .

Either way will work

Excuse me for dwelling on this but I wanted to find the real cause and suggest a fix to Arduino.cc.

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.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

#include <Servo.h>
#include <IRremote.h> //need to get this library https://github.com/shirriff/Arduino-IRremote
int REC_PIN = 11;

IRrecv irrec(REC_PIN);

decode_results result;

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

irrec.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 (irrec.decode(&result)) { // if a signal is detected, store the value

CurrentValue = (result.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);

irrec.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;

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

ye sremoving work self tested

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...

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.

There's a couple of easy ways to get arround that. Well... At least easier than using a servo. :)

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?

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.

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.


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.

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

https://www.sparkfun.com/products/10636

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.

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

Overall I tried it but it didn't work. Lacks details. :(

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

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.

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
More Comments