Categories
Projects

Arduino Remote Light Switch

Beds are hard to get out of it when you’re settled in, but lights don’t turn themselves off.

In the past, I could switch the lights off in my bed using poles to turn off the lights. But not after moving to my new apartment.

There are commercial solutions to this issue. You can change the switch out to respond to claps or install Bluetooth-enabled light bulbs. Both are expensive.

The IR remote that passes a signal to an IR receiver on the Arduino which then causes the servo switch off the light.

This $30 solution was much easier. A servo would switch off the lights upon receiving an IR signal. The code was programmed in C.

Another example of how simple programming and simple engineering can help you in your daily life.

What you need

Here’s the item list:

  • Arduino (I used the Elegoo Uno R3)
  • Infrared remote
  • Servo (I used the SG90)
  • IR Receive Module
  • Power source
  • Woodworking materials (glue, blocks of wood)

For a power source, what I did was to go to a thrift store to buy a 9V power supply and I plugged it in to the V-in and GND.

On the IR receive module, you will see pins G, R and Y. Connect G to GND, R to 3.3v and Y to pin 11.

Connect the servo’s live wire (usually red or close-to-red colour) to 5V, ground wire (usually black or close to) to GND and signal wire to pin 9.

To attach the servo, I used double sided tape and then used blocks of wood to raise the servo to the appropriate height.

Coding the program for Arduino

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


Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int receiver = 11;
int pos = 0;    // variable to store the servo position
int boardPin = 13;
int powerButton = 0;

IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void translateIR() // takes action based on IR code received

// describing Remote IR codes 

{

  switch(results.value)

  {
  case 0xFFA25D: Serial.println("POWER"); powerButton = 1; break;
  case 0xFFE21D: Serial.println("FUNC/STOP"); break;
  case 0xFF629D: Serial.println("VOL+"); break;
  case 0xFF22DD: Serial.println("FAST BACK");    break;
  case 0xFF02FD: Serial.println("PAUSE");    break;
  case 0xFFC23D: Serial.println("FAST FORWARD");   break;
  case 0xFFE01F: Serial.println("DOWN");    break;
  case 0xFFA857: Serial.println("VOL-");    break;
  case 0xFF906F: Serial.println("UP"); break;
  case 0xFF9867: Serial.println("EQ");    break;
  case 0xFFB04F: Serial.println("ST/REPT");  break;
  case 0xFF6897: Serial.println("0");  break;
  case 0xFF30CF: Serial.println("1");  break;
  case 0xFF18E7: Serial.println("2");  break;
  case 0xFF7A85: Serial.println("3");  break;
  case 0xFF10EF: Serial.println("4");  break;
  case 0xFF38C7: Serial.println("5");  break;
  case 0xFF5AA5: Serial.println("6");  break;
  case 0xFF42BD: Serial.println("7");  break;
  case 0xFF4AB5: Serial.println("8");  break;
  case 0xFF52AD: Serial.println("9");  break;
  case 0xFFFFFFFF: Serial.println(" REPEAT");break;  

  default: 
    Serial.println(" other button : ");
    Serial.println(results.value);

  }// End Case

  delay(500); // Do not get immediate repeat


} //END translateIR

void setup() {
  irrecv.enableIRIn(); // Start the receiver
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(0);
  
  pinMode (boardPin,OUTPUT);
}

void loop() {



 if (irrecv.decode(&results)) // have we received an IR signal
  {
    translateIR();
    if (powerButton) {
      turnOffSwitch();
    } 
    turnOffSwitch(); // remove this if you only want power button to activate the servo.
    irrecv.resume();
  }  
}


void turnOffSwitch () {
  myservo.write(120);
      delay(1000);
      myservo.write(0);
      powerButton = 0; //resets powerButton to reset servo
     // turnOffNoise = 1;
}

Basically, the program is a combination of the example IR remote code and the example servo code.

The code triggers the servo to swing when it receives any IR signal whether it’s from the remote or from your cell phone.

You’d have to tweak the arm swing degree to your situation. Change the value of myservo.write in the function turnOffSwitch(). I had to do some trial and error to get these values, but give it a try and see if it works for you.