HOME AUTOMATION (DEVICE CONTROLLING ) USING MSP430G2553 , IR REMOTE AND RELAY

Photo of author

By Jackson Taylor

Home » Electronics Topics » Msp430 » HOME AUTOMATION (DEVICE CONTROLLING ) USING MSP430G2553 , IR REMOTE AND RELAY

As an Amazon Associate we earn from qualifying purchases.

This project turns an ordinary IR remote into a wireless switch panel for your room. An MSP430G2553 microcontroller reads the button presses, and relays switch fans, bulbs and a tube light on and off. It is a good first build if you want to learn how infrared decoding, GPIO outputs and relay switching fit together.

How the system works

The remote sends a burst of modulated infrared light for every button press. An IR receiver module next to the microcontroller demodulates that signal and passes it on to one input pin. The MSP430G2553 decodes the burst into a number, compares it with the codes stored in the program, and toggles the output pin linked to that button. Each output pin drives a relay, and each relay switches one appliance.

The same idea is used in the infra red receiver based builds on this site, and it also works with a spare TV remote once you know its codes.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

What you need

CIRCUIT

Circuit diagram of MSP430G2553 LaunchPad with IR receiver and relay outputs for office automation

Pin assignment

The program uses Energia-style pin names. The connections are:

  • P1_3: IR receiver signal output
  • P1_4: relay for fan 1
  • P1_5: relay for bulb 1
  • P2_0: relay for fan 2
  • P2_1: relay for the tube light
  • P2_2: relay for bulb 2

Connect the grounds of the LaunchPad, the IR receiver and the relay module together. Check the supply voltage of your relay module as well. The microcontroller pins work at logic level, and many relay boards need a 5 V supply and some need a driver stage. If your relay board is active-low, a HIGH output will switch the relay off, so swap HIGH and LOW in the code.

See also
RELAY & RELAY MODULES

Button mapping

The sketch reacts to six buttons. The numbers are the decimal values that the library returned for the remote used in the original build.

  • Key 1 toggles fan 1
  • Key 2 toggles fan 2
  • Key 3 toggles bulb 1
  • Key 4 toggles bulb 2
  • Key 5 toggles the tube light
  • The red off key switches every device on if the master flag is off, or off if it is on

Each device has its own state variable (f1, f2, b1, b2, t), so pressing a key once turns the device on and pressing it again turns it off.

PROGRAM


//off -red --- 33441975
//1 -- 33444015
//2 -- 33478695
//3 -- 33486855
#define  KEY_off_red   33441975
#define  KEY_1   33444015
#define  KEY_2   33478695
#define  KEY_3   33486855
#define  KEY_4   33435855
#define  KEY_5   33468495
int  f1,f2,b1,b2,t,a;
#define  fan1  P1_4
#define bulb1  P1_5
#define  fan2  P2_0
#define  tube  P2_1
#define bulb2  P2_2
#include <IRremote.h>
int RECV_PIN = P1_3;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  init_all();
  off_all();
  Serial.begin(9600);delay(1000);
  Serial.println("HAI");
  irrecv.enableIRIn(); // Start the receiver
}
void loop() {
  while (!irrecv.decode(&results));
  {
    Serial.println(results.value);
    check_ir_value();
    irrecv.resume(); // Receive the next value
  }
}
void check_ir_value(void)
{
  if(results.value == KEY_1 )
  {
    Serial.print("key1-");
    if(f1==1)  {f1=0;   Serial.println("oFF");      digitalWrite(fan1 ,LOW); }
    else       {f1=1;   Serial.println("oN");       digitalWrite(fan1 ,HIGH); }
  }
  else if(results.value == KEY_2 )
  {
    Serial.print("key2-");
    if(f2==1)  {f2=0;   Serial.println("oFF");      digitalWrite(fan2 ,LOW); }
    else       {f2=1;   Serial.println("oN");       digitalWrite(fan2 ,HIGH); }
  }
  else if(results.value == KEY_3 )
  {
    Serial.print("key3-");
    if(b1==1)  {b1=0;   Serial.println("oFF");      digitalWrite(bulb1 ,LOW); }
    else       {b1=1;   Serial.println("oN");       digitalWrite(bulb1 ,HIGH); }
  }
  else if(results.value == KEY_4 )
  {
    Serial.print("key4-");
    if(b2==1)  {b2=0;   Serial.println("oFF");      digitalWrite(bulb2 ,LOW); }
    else       {b2=1;   Serial.println("oN");       digitalWrite(bulb2 ,HIGH); }
  }
  else if(results.value == KEY_5 )
  {
    Serial.print("key5-");
    if(t==1)  {t=0;   Serial.println("oFF");      digitalWrite(tube ,LOW); }
    else      {t=1;   Serial.println("oN");       digitalWrite(tube ,HIGH); }
  }
  else if(results.value == KEY_off_red )
  {
    Serial.print("key-red-");
    if(a==1)  {a=0;   Serial.println("oFF");     off_all(); }
    else      {a=1;   Serial.println("oN");      on_all(); }
  }
}
void init_all(void)
{
  pinMode(fan1,OUTPUT); pinMode(fan2,OUTPUT); pinMode(tube,OUTPUT);
  pinMode(bulb1,OUTPUT); pinMode(bulb2,OUTPUT);
}
void off_all(void)
{
  digitalWrite(fan1 ,LOW);digitalWrite(fan2 ,LOW);digitalWrite( tube,LOW);
  digitalWrite( bulb1,LOW);digitalWrite(bulb2 ,LOW);
  f1=0;f2=0;b1=0;b2=0;t=0;
}
void on_all(void)
{
  f1=1;f2=1;b1=1;b2=1;t=1;
  digitalWrite(fan1 ,HIGH);  digitalWrite(fan2, HIGH);  digitalWrite( tube,HIGH);
  digitalWrite( bulb1,HIGH);  digitalWrite(bulb2 ,HIGH);
}

How the code works

  1. Setup: init_all() sets the five relay pins as outputs, and off_all() makes sure every device starts off. The serial port runs at 9600 baud, and the IR receiver is enabled.
  2. Waiting: the loop() function waits until the library decodes a signal, then prints the value to the serial monitor.
  3. Matching: check_ir_value() compares the value with each key code and flips the matching state variable and output pin.
  4. Next signal: irrecv.resume() prepares the receiver for the next button press.
See also
BOOT STRAP CAPACITOR DESIGN

Using your own remote

The key codes above belong to one specific remote. Yours will almost certainly send different numbers. Open the serial monitor at 9600 baud, press each button and note the value printed. Then replace the numbers in the #define lines. Some remotes also send a repeat code when a button is held down, so a quick tap gives the most reliable result.

Safety when switching mains loads

Relays isolate the microcontroller from the appliance, but the appliance side may carry dangerous mains voltage. Test the whole system first with low-voltage loads such as LEDs or a small DC fan. When you move to mains, use relays rated above the load current, keep the wiring inside an insulated enclosure, and have someone qualified check the work. Never touch the relay terminals while the circuit is powered.

Ideas for improving the project

FAQ

Why does the relay seem to work the wrong way round?

Many relay modules are active-low, meaning they switch on when the input pin is LOW. If your devices turn on when they should turn off, swap HIGH and LOW in the code, or adjust the initial state in off_all().

Nothing happens when I press a button. What should I check?

Open the serial monitor and see whether a number appears when you press a key. If nothing prints, check the receiver wiring and power. If a number appears but the device does not switch, your remote’s code does not match the one in the program, so update the #define values.

Can I add more devices?

Yes. Define another output pin and a state variable, add a new key code, and add one more else if block that follows the same pattern. Remember to include the new pin in init_all(), off_all() and on_all().

Can the microcontroller drive the relay directly?

Not always. A relay coil can draw more current than a microcontroller pin should supply. Use a relay module with its own driver circuit, or add a transistor and a flyback diode for each bare relay.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
See also
PWM BASED LIGHT WAVES USING MSP430G2553 FOR CHRISTMAS CRIB

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works