Arduino IR remote control of LEDs and Relays.

Arduino IR remote control of LEDs and Relays.
An IR remote control enables us to control devices from a distance through wireless communication. Home appliances like radios and TV sets can be a pain if you have to always get up from your seat to adjust the volume, change channels or even to turn on and off!
In this tutorial I will show you how IR transmitters and receivers work and give some example applications of IR remote control.

Why use Infrared light?

Infrared light is actually part of the light spectrum and is similar to visible light. However infrared light cannot be detected by the human eye because it has a wave length of about 900nm which is above the visible light spectrum. Infrared literally means “below red”.
IR light is safe to work with and even has no effect on the eyes or skin. This is one of the main reasons why this type of light is preferred for remote control purposes since we can use it without seeing it! Another reason is that IR LEDs are very easy to make therefore making the remote control devices generally cheap.

How does an IR remote control work?

An IR remote contains an IR LED which produces pulses of infrared light in order to send signals to another device with an IR receiver for decoding the signals. An IR receiver contains a photodiode and an amplifier for converting the IR light signals to electrical signals.
ir remote and tpso382 ir receiver
The remote has different buttons whereby each button produces a different pulse when pressed. When you press a key on your remote, the transmitting IR LED will blink very quickly for a fraction of a second, transmitting encoded data in form of a pulse. This pulse can then be decoded uniquely by the IR receiver so that a specific action can be taken for example decreasing the volume of your radio.
The IR remote control and receiver modules are usually available in many Arduino starter kits but in case you don’t have one you can buy using the links below:
KOOBOOK 2Sets HX1838 NEC Infrared IR Wireless Remote Control Sensor Module IR Receiver Module DIY Kit for arduino
ELEGOO 4 Channel DC 5V Relay Module with Optocoupler Compatible with Arduino UNO R3 MEGA 2560 1280 DSP ARM PIC AVR STM32 Raspberry Pi
ARDUINO UNO R3 [A000066]
Product
HX1838 NEC IR remote Control module
ELEGOO 4 Channel DC 5V Relay module
ARDUINO UNO R3 [A000066]
Price
$6.99
$7.99
$22.40
KOOBOOK 2Sets HX1838 NEC Infrared IR Wireless Remote Control Sensor Module IR Receiver Module DIY Kit for arduino
Product
HX1838 NEC IR remote Control module
Price
$6.99
Buy New
ELEGOO 4 Channel DC 5V Relay Module with Optocoupler Compatible with Arduino UNO R3 MEGA 2560 1280 DSP ARM PIC AVR STM32 Raspberry Pi
Product
ELEGOO 4 Channel DC 5V Relay module
Price
$7.99
Buy New
ARDUINO UNO R3 [A000066]
Product
ARDUINO UNO R3 [A000066]
Price
$22.40
Buy New

Last update on 2021-04-14 / Affiliate links / Images from Amazon Product Advertising API

Disclosure: These are affiliate link. I earn a commission as an Amazon Associate for qualifying purchases. I would appreciate your support in this way.

Modulation of IR signals.

A major challenge that we could face when using an IR remote control is that there are many other sources of infrared light. Almost everything that emits heat also emits infrared light therefore the signals from a remote can be interfered by anything from sunlight, indoor light bulbs and even our own bodies. Therefore we have to take some measures to ensure that the IR signal gets to the receiver without errors.
To solve this problem, the light pulses from the IR LEDs have to be modulated just like analog radio modulates a carrier wave to send a signal. Modulation involves making the transmitting IR LED blink with a particular frequency. The IR receiver will then be tuned to that frequency in order to ignore the noise signals from other infrared sources.
The diagram below shows a summary of how the IR remote control system works. The remote contains an encoder which converts a binary signal into a modulated electrical signal. This electrical signal is sent to the transmitting LED. The transmitting LED converts the modulated electrical signal into a modulated IR light signal. The IR receiver then demodulates the IR light signal and converts it back to binary before passing on the information to a microcontroller for example Arduino.
modulation of ir remote signals
Most IR remotes use a modulation frequency of 38kHz although other frequencies can also be used. There are very few other sources that have the regularity of a 38kHz signal, therefore an IR transmitter sending data at that frequency would easily stand out.
IR receivers are designed to look out for this modulated infrared light and to ignore the rest hence filtering out all other noise signals saturating the room. Infrared light covers a broad range of the spectrum, from 700 Nm to 1 mm. Most IR LED’s used in remote controls operate at 850 Nm to 940 Nm range so most receivers are tuned to receive light within this range. This is another way that they prevent interference from sunlight and other infrared sources.
Another thing to keep in mind is that infrared is light and therefore it requires line-of-sight visibility for the best possible operation and can still be reflected by items such as glass and walls.

Troubleshooting a faulty IR remote control. (Detecting infrared light)

In case there is a problem with a project involving an IR remote control, it may be hard to determine whether the problem is with the transmitter or receiver. This because our human eyes cannot see infrared light. However a mobile phone camera can!
To check if a remote control is working or not, just aim the IR LED of the remote at the lens of your mobile phone camera while viewing the screen. For a working remote, when any button is pressed, you will see the flashing IR LED on the phone screen. If no flashing is observed when pressing any button, the remote control may be faulty.

Decoding an IR Remote using Arduino.

The first part of this project involves decoding of the IR remote. This is done in order to know the control codes of your remote control because every button of the remote control generates a unique hexadecimal code that is modulated and sent to the IR receiver.
If you have the datasheet of a specific remote control then you can be able to get the list of the codes corresponding to the various buttons. However in absence of a datasheet then we can use a simple Arduino program to read and display the codes of most common remote controls on the serial monitor.
This is done by first connecting the IR receiver alone with the Arduino board as shown in the shematic below. There are different types of receivers but all of them have three pins. In my case I am using the TSOP382 IR receiver and the pins are connected to the Arduino as follows;
Pin 1 to Vout (pin 11 on Arduino)
Pin 2 to GND
Pin 3 to Vcc (+5v from Arduino)
connecting the ir receiver with arduino

Code for decoding the remote.

The important library to use for the working of the ir remote with arduino is the IRremote .h library. Make sure you include this library in the Arduino IDE before writing any code for ir remote control.
Download the IRremote.h library
#include <IRremote.h>

int IRpin = 11;
IRrecv irrecv(IRpin);
decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, DEC); // Print the Serial 'results.value'
irrecv.resume(); // Receive the next value
}

}
First we create an object called irrecv where we specify the pin where the IR receiver is connected.
Then we create an object called results , from the decode_results class, which will be used by the irrecv object to share the decoded information with our application
To start the IR receiver, we call the IRrecv member function enableIRIn()
The function irrecv.decode will return true if a code is received and the program will execute the code in the if statement.
The received code is stored in results.value. In my case the code is going to be stored as a decimal value.
At the end we call irrecv.resume() to reset the receiver and prepare it to receive the next code.
The above code is then uploaded to the board. Point the remote control to the receiver. Press the buttons and the respective codes will appear on the serial monitor.
decoding the remote
From the photo above, the serial monitor indicates the codes for the remote buttons in decimal values. The long values are neglected and only the short ones considered. For example the code for button 1 from above is 16724175.
After decoding the remote we can now be able to use it in a number of applications. For example in the control of the lighting of LEDs using Arduino. The setup is as shown below.

LED Control using IR remote with Arduino.

ir remote arduino schematic

Code for controlling LEDs using IR remote.

#include <IRremote.h>

int RECV_PIN = 11; // the pin where you connect the output pin of sensor
int led1 = 2;
int led2 = 4;
int led3 = 7;
int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1 16724175 // code received from button no. 1
#define code2 16718055 // code received from button no. 2
#define code3 16743045 // code received from button no. 3

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600); // you can ommit this line
irrecv.enableIRIn(); // Start the receiver
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}

void loop() {
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(led1, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(led1, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
}
break;
case code2:
if(itsONled[2] == 1) {
digitalWrite(led2, LOW);
itsONled[2] = 0;
} else {
digitalWrite(led2, HIGH);
itsONled[2] = 1;
}
break;
case code3:
if(itsONled[3] == 1) {
digitalWrite(led3, LOW);
itsONled[3] = 0;
} else {
digitalWrite(led3, HIGH);
itsONled[3] = 1;
}
break;
}
Serial.println(value); // you can ommit this line
irrecv.resume(); // Receive the next value
}
}
When the code above is uploaded to the Arduino board and the remote control is pointed towards the setup, the first led lights when button 1 is pressed, the second lights when button 2 is pressed and the third lights on pressing button 3. When button 1 is pressed again, the first led goes off and the result is the same for the other buttons and their corresponding leds.

Controlling a 5V 4 Channel Relay using an IR remote.

The infrared remote can be used in controlling high voltage appliances in homes for example lights. This is possible with the use of relays. In this case am using a 5V 4-channel relay module that am going to connect to an infrared receiver so that I can use Arduino to control the relay module.
If you are not very sure of how to use the relay you can check out my other tutorial on how to interface the 4 channel relay module with Arduino form the link below:
  • How to Interface a 5V 4 Channel Relay Module with Arduino.
  • The connections are going to be made as shown in the schematic below.
    ir remote controlled relay schematic

    Code for controlling relay with IR remote and Arduino.

    #include <IRremote.h>
    int RECV_PIN = 11; // the pin where you connect the output pin of sensor
    int relay1 = 2;
    int relay2 = 3;
    int relay3 = 4;
    int relay4 = 5;
    
    int relayState[] = {0,0,0,0,0}; //the initial state of relays
    
    #define code1 16724175    // code received from button no. 1
    #define code2 16718055    // code received from button no. 2
    #define code3 16743045    // code received from button no. 3
    #define code4 16716015    // code received from button no. 4
    
    IRrecv irrecv(RECV_PIN);
    decode_results results;
    
    void setup() {
    irrecv.enableIRIn(); // Start the receiver  
    pinMode(relay1, OUTPUT);
    pinMode(relay2, OUTPUT);
    pinMode(relay3, OUTPUT);
    pinMode(relay4, OUTPUT);
    }
    
    
    void loop() {
    if (irrecv.decode(&results)) {
    unsigned int value = results.value;
    switch(value) {
    case code1:
    if(relayState[1] == 0) { // if first relay is on then
    digitalWrite(relay1, HIGH); // turn it off when button is pressed
    relayState[1] = 1; // and set its state as off
    } else { // else if first led is off
    digitalWrite(relay1, LOW); // turn it on when the button is pressed
    relayState[1] = 0; // and set its state as on
    }
    break;
    case code2:
    if(relayState[2] == 0) {
    digitalWrite(relay2, HIGH);
    relayState[2] = 1;
    } else {
    digitalWrite(relay2, LOW);
    relayState[2] = 0;
    }
    break;
    case code3:
    if(relayState[3] == 0) {
    digitalWrite(relay3, HIGH);
    relayState[3] = 1;
    } else {
    digitalWrite(relay3, LOW);
    relayState[3] = 0;
    }
    break;
    case code4:
    if(relayState[4] == 0) {
    digitalWrite(relay4, HIGH);
    relayState[4] = 1;
    } else {
    digitalWrite(relay4, LOW);
    relayState[4] = 0;
    }
    break;
    }
    irrecv.resume(); // Receive the next value
    }
    
    }
    
    I have demonstrated the use of IR remote control in some other projects like controlling a robot car and stepper motor. You can check them out for further practice:
  • Remote controlled Car Using Arduino
  • Controlling 28BYJ-48 Stepper Motor with Remote.
  • 6 thoughts on “Arduino IR remote control of LEDs and Relays.

    1. As a beginner it is very difficult to follow your program because of the letter coloring
      does not indicate words, symbols are commands can be chosen arbitrarily.

      QUESTION:—————————\-
      //Decode IR _________\_______________________
      #include //<–why is this include required ?? //
      #include

      int IRpin =11;
      IRrecv irrecv(IRpin);
      decode_results results;

      void setup() {
      Serial.begin(9600);
      irrecv.enableIRIn(); //Dig_11 1838 data output
      }

      void loop() {
      if (irrecv.decode(&results)) { // _&results _& =>address of results
      Serial.println(results.value, DEC); // Ser_MOn printout of IR code
      irrecv.resume(); // next value of _irrecv
      }
      delay(100);
      }

      1. Your question is not very clear maybe you can rephrase it so that i can understand what exactly the problem is. Maybe i can answer that #include<> is used to declare the libraries that are being used in the given program. For example in the above program we need the IRremote.h library.

      1. As far as I can tell this number is used to indicate that the button is being held, it is produced when just after the correct code when the button is pressed slightly too long.

    2. Thanks for the detailed explanation. Everything worked out fine for me by just following the steps. Keep up the good tutorials.

    3. Hii
      Thanks a lot for sharing this awesome project about IR receiver . i am plaining to make IR car using remote control, this will help me alot and wil follow it . thanks once again keep it up with such great idea

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top