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.
What you need
- MSP430 LaunchPad with the MSP430G2553
- IR receiver module (three pins: signal, power, ground)
- Any IR remote with at least six buttons
- Relay module with five channels, or five relays with transistor drivers and flyback diodes
- Appliances to control: two fans, two bulbs and a tube light in this example
- Jumper wires, a breadboard and a suitable enclosure
CIRCUIT
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.
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
- 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.
- Waiting: the loop() function waits until the library decodes a signal, then prints the value to the serial monitor.
- Matching: check_ir_value() compares the value with each key code and flips the matching state variable and output pin.
- Next signal: irrecv.resume() prepares the receiver for the next button press.
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
- Add LEDs to show the state of each relay.
- Control the same outputs from a phone using Android mobile and Bluetooth.
- Move to a wireless mesh with ZigBee technology for larger buildings.
- Add smart sensors for home automation, such as motion or light sensors, so devices switch automatically.
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.