Programmable light switch with remote control

Published on July 01, 2012

Programmable light switch with remote control

    image
    Continuation of previously published articles, first , second .
    This article will focus on a programmable light switch with remote control. Like previous projects for debugging and prototyping, I use Carduino Nano V.7


    Required Functionality


    The ability to control the switch with any household IR remote control.
    To program the switch on any button of the household IR remote control.
    Turn on / off the light, both from the switch key and from the remote control, regardless of each other.

    Scheme and accessories


    image
    I used the following components to build the prototype:
    Carduino Nano V.7
    Relay Module controller
    TSOP IR receiver Development
    board
    Sound emitter

    Component photo
    image

    Description of the circuit breaker


    First, we add the button code of the control panel to the memory of the switch. To do this, we take the most suitable remote control (the remote control from the air conditioner will not work) and select on it a free button that you never use (usually these are colored teletext buttons). We enter the programming mode of the switch, for this you need to press the on / off key and hold for 5 seconds, a long beep will sound, after that it remains to press the previously selected button on the remote control and your switch is ready for operation. Now the light switch will understand the added command of your IR remote control. The button code is stored in the non-volatile memory of the controller and will be stored in the memory even after turning off the power to the circuit.

    The operation of the prototype switch, see the video


    Code for Arduino Controller


    #include <IRremote.h>   
    #include <avr/delay.h> 
    #include <EEPROM.h> 
    #define button 7 
    #define speaker 11 
    #define lamp 6 
    IRrecv irrecv(2);  //вход для ик приемника 
    decode_results results;   
    unsigned long ir_command; 
    uint8_t swt, count, state = 0;  //статус лампочки 
    void setup()   
    {   
      pinMode(speaker, OUTPUT);//громкоговоритель 
      pinMode(button, INPUT);//вход для кнопки 
      digitalWrite(button,HIGH);//включить подтяжку 
      pinMode(lamp, OUTPUT);//лампочка 
      irrecv.enableIRIn(); // включить приемник   
    }   
    void loop()  
    {   
     swt=digitalRead(button); 
     count=0;  
     while(swt==0) 
       { 
        if(count>30) 
         {  
           beep(100,400); 
           for(unsigned long i=0; i<2000000; i++) 
           { 
             if (irrecv.decode(&results))   
              {   
                SaveEEPROM(results.value);   
                irrecv.resume();            
                break;             
              } 
            }  
           beep(30,900);   
           break;  
          } 
        count++;   
        swt=digitalRead(button);   
        _delay_ms(100);  
       } 
     ir_command=LoadEEPROM(); 
        if (irrecv.decode(&results)) 
        {     
          if(ir_command==results.value)  
            { 
              state=~state;  
              digitalWrite(lamp, state); 
              beep(10,300);  
              _delay_ms(700);  
            }             
         irrecv.resume();          
        } else  if(count>1)       
                 {  
                   state=~state; 
                   digitalWrite(lamp, state);  
                   beep(10,500); 
                   _delay_ms(100); 
                 }         
    } 
    void SaveEEPROM(unsigned long ir_code) 
    { 
      EEPROM.write(0, ir_code & 0xFF); 
      EEPROM.write(1, (ir_code & 0xFF00) >> 8); 
      EEPROM.write(2, (ir_code & 0xFF0000) >> 16); 
      EEPROM.write(3, (ir_code & 0xFF000000) >> 24); 
    } 
    unsigned long LoadEEPROM()  
    { 
      byte val = EEPROM.read(3);  
      unsigned long ir_code=val; 
          val = EEPROM.read(2); 
                    ir_code= (ir_code << 8) | val; 
          val = EEPROM.read(1); 
                    ir_code= (ir_code << 8) | val; 
          val = EEPROM.read(0); 
                    ir_code= (ir_code << 8) | val; 
      return ir_code; 
    } 
    void beep(byte dur, word frq) 
    { 
      dur=(1000/frq)*dur; 
      for(byte i=0; i<dur; i++) 
      { 
       digitalWrite(speaker, HIGH);  
       _delay_us(frq);  
       digitalWrite(speaker, LOW); 
       _delay_us(frq); 
      }  
    } 
    


    To compile the code, you need to add the IRemote library . The library is different from the original one, as I added several more protocols to it, including the new Samsung LED TV protocol

    Circuit diagram of the circuit breaker


    After debugging the device on arduino, I drew the final diagram of the future switch. Since I wrote earlier that Arduino is a convenient tool for debugging and writing code, but it will not work for the final circuit.
    image

    Hex file for Atmega168 controller firmware
    The power supply can be used from a charger for a mobile phone.
    Schemes of power supplies.

    If you have questions, please answer them with pleasure.