A LOW COST INFRA RED RECEIVER REMOTE BASED HOME AUTOMATION

Photo of author

By Jackson Taylor

Home » DIY Tech Projects » A LOW COST INFRA RED RECEIVER REMOTE BASED HOME AUTOMATION

As an Amazon Associate we earn from qualifying purchases.

An infrared (IR) remote is one of the cheapest ways to switch things on and off from your sofa. With an Arduino, a small IR receiver module and a couple of relays, you can turn a lamp or a fan on and off using the TV-style remote you may already have in a drawer. This article walks through a low-cost IR remote home automation project: how it works, what you need, the full sketch and what to watch out for.

The video below shows the project working.

How IR remote home automation works

A remote control has an infrared LED that flashes a pattern of pulses when you press a button. Each button sends a different code. An IR receiver module sits on the Arduino side, filters out ambient light, and turns the pulses into a clean digital signal. The Arduino decodes that signal into a number and compares it with the codes you have chosen for your appliances.

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

When the code matches, the Arduino sets an output pin HIGH or LOW. That pin drives a relay module (or a transistor stage), and the relay switches the appliance. The whole chain is: remote, IR receiver, Arduino, relay, appliance.

What you need

  • An Arduino board (an Uno or any ATmega328-based board works with this sketch)
  • An IR receiver module with three pins: signal, ground and supply
  • Any IR remote that sends standard codes, such as an old TV or set-top box remote
  • A two-channel relay module, or two transistors with relays and flyback diodes
  • Jumper wires and a breadboard
  • The IRremote library, installed from the Arduino Library Manager

Wiring

  1. Connect the receiver module’s supply pin to 5V and its ground pin to GND on the Arduino.
  2. Connect the receiver’s signal (output) pin to Arduino digital pin 2. This is the RECV_PIN in the sketch.
  3. Connect digital pin 10 to the input of the first relay channel and digital pin 9 to the second.
  4. Power the relay module from the 5V and GND pins, and check the module’s markings, since pin order differs between receiver modules.
See also
HOME AUTOMATION

Safety note: relays switch mains voltage in a real installation. Test with a low-voltage load such as an LED or a small DC lamp first. If you do switch mains appliances, use an insulated enclosure, keep the wiring away from the low-voltage side, and have an experienced person check it.

The program

The sketch below is the original code from this project. Pin 2 reads the IR receiver, and pins 9 and 10 drive the outputs.

PROGRAM

/* An IR detector/demodulator must be connected to the input RECV_PIN. */ #include <IRremote.h> int b; int RECV_PIN = 2; IRrecv irrecv(RECV_PIN); decode_results results;

void setup() { pinMode(9,OUTPUT); pinMode(10,OUTPUT); irrecv.enableIRIn(); // Start the receiver }

void loop() { while(!irrecv.decode(&results)) ; { irrecv.resume(); // Receive the next value } if(results.value==33441975){ Serial.println(“off”); } if(results.value==0X1FE50AF) //1 { digitalWrite(10,HIGH); } else if(results.value==0X1FED827) //2 { digitalWrite(9,HIGH); } else if(results.value==0X1FE30CF) //4 { digitalWrite(10,LOW); } else if(results.value==0X1FEB04F) //5 { digitalWrite(9,LOW); } }

What each part does

  • IRrecv irrecv(RECV_PIN) creates the receiver object on pin 2, and irrecv.enableIRIn() starts it.
  • irrecv.decode(&results) checks whether a new code has arrived and stores it in results.
  • irrecv.resume() tells the library to get ready for the next button press.
  • The if / else if chain compares results.value with the codes for buttons 1, 2, 4 and 5.

In this sketch, button 1 turns the output on pin 10 on, and button 4 turns it off. Button 2 turns the output on pin 9 on, and button 5 turns it off. That gives you two independent on/off channels.

Using your own remote

The button codes in the sketch belong to the remote used in the original build. Your remote will almost certainly send different values, so replacing them is the first thing to do. Upload the IRremote library’s dump example, open the Serial Monitor, press each button and note the hexadecimal value that appears. Then put those values in the sketch in place of the ones shown.

Problems in the original code and how to fix them

  • No Serial.begin(). The sketch calls Serial.println(“off”) but never starts the serial port, so nothing will print. Add Serial.begin(9600); to setup() if you want that message.
  • Blocking loop. The while(!irrecv.decode(&results)) line waits until a code arrives, so the Arduino can do nothing else in the meantime. An if statement avoids this.
  • Old library version. This code uses the older IRremote API (IRrecv and decode_results). Newer releases of the library changed these names, so install an older version or adapt the code to the new examples.
  • Unused variable. The variable b is declared but never used and can be removed.
See also
40 Fun DIY Electronics Projects for Beginners & Makers

A cleaner version of the main loop looks like this:

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

void loop() { if (irrecv.decode(&results)) { if (results.value == 0x1FE50AF) digitalWrite(10, HIGH); else if (results.value == 0x1FED827) digitalWrite(9, HIGH); else if (results.value == 0x1FE30CF) digitalWrite(10, LOW); else if (results.value == 0x1FEB04F) digitalWrite(9, LOW); irrecv.resume(); } }

Troubleshooting

  • Nothing happens: check the receiver’s pin order and make sure the signal pin goes to pin 2.
  • Wrong or random codes: keep the receiver away from bright light and make sure the remote’s battery is fresh.
  • Relay works backwards: many relay modules are active-low. If so, swap HIGH and LOW in the sketch.
  • Relay clicks but the Arduino resets: power the relay module separately, or add a proper driver stage, so the relay coil does not pull down the Arduino’s supply.

Where to go next

IR works only in line of sight and over a short range. If you need more, there are several ways to extend the idea. You can read the MSP430 version of this project that uses the same remote-and-relay approach on a different microcontroller. You can control it from a phone over Bluetooth instead of a remote. For control through walls, an RF transmitter and receiver pair does not need a clear line of sight. A larger build could use a ZigBee-based home automation system. If you want the system to react on its own, look at smart sensors for home automation.

FAQ

Can I use any IR remote for this project?

Most remotes that use a common protocol will work, but each sends its own codes. Read the codes with the library’s example sketch and put them into your program.

How far does the remote work?

Range depends on the remote, the receiver module and the lighting in the room. Expect it to work in a normal room when the remote points roughly at the receiver, and not through walls or closed doors.

Can it control mains appliances?

Yes, with a relay rated for the load, but mains wiring is dangerous. Prototype with low-voltage loads, use an enclosure, and get the final wiring checked by someone qualified.

See also
MOBILE ACCELEROMETER CONTROLLED VEHICLE

Can I add more appliances?

Yes. Add more output pins, one relay channel per appliance, and add a new else if line with a button code for each.

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