Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

Using IR Sensors and Remotes with Arduino

Contents

Introduction

In this tutorial, we will use an IR sensor with the Arduino to be able to control it using a remote control unit.

What is IR?

IR stands for “Infrared”.

Electromagnetic waves that have wavelengths in the range of 1mm to 700nm and frequencies between 300GHz to 430THz are known as “IR”. IR has a slightly higher wavelength than visible light and cannot be seen through the naked eye.

It is very popular for wireless communication. Many of us have a television or an air conditioner in our homes, and remotes with IR are used to control these devices.

How Does IR Work, and Why is It Used?

How IR works in remote control units will be discussed in another post where I will be teaching you about cloning remote signals. Today, we will just be focusing on achieving a specific task wherein our IR sensor receives a specific signal from a remote control unit.

In short, the IR transmitter in remotes sends a specific series of pulses for every button pushed. The receiver receives and decodes the signals and makes a specific action.

IR is widely used because it is secure and directional. It can be transmitted and received from a long distance at a very fast speed.

Schematic Diagram

Construct the circuit as follows.

Now, go to this link and download it as a Zip file: https://github.com/z3t0/Arduino-IRremote

Now, open the Arduino IDE.

Navigate to Sketch > Include Library > Add .ZIP library

Now, select the ZIP file you downloaded previously.

The Code

Now, write the following code in your IDE & upload it:

				
					#include <IRremote.hpp>

#define IR_RECEIVE_PIN 2

void setup()
{
  Serial.begin(115200);
  Serial.println("Beginning IR Receiver");
  IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK); // Start the receiver
}

void loop() {
  if (IrReceiver.decode()) {
      
      IrReceiver.printIRResultShort(&Serial);
      
      IrReceiver.resume(); // Enable receiving of the next value
        
  }
  
}
				
			

After uploading the code, open your serial monitor. Take any random remote control unit and point it at your IR sensor. Keep tapping different buttons a few times.

You should notice that a specific value is showing up on your Serial Monitor every time you tap the button on your remote.

Here, I am using the remote of my Sony television, and every time I tap the power button, I get this value.

Now, I want to light up the built-in LED with my remote whenever the UP button (code 0x74) is tapped. How would I do that? It could be done with a simple if statement.

Now upload the following code:

				
					#include <IRremote.hpp>

#define IR_RECEIVE_PIN 2

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  Serial.println("Beginning IR Receiver");
  IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK); // Start the receiver
}

void loop() {
  if (IrReceiver.decode()) {
      
      IrReceiver.printIRResultShort(&Serial);
      
      IrReceiver.resume(); // Enable receiving of the next value
        /*
         * Finally, check the received data and perform actions according to the received command
         */
        if (IrReceiver.decodedIRData.command == 0x74) {
            // do something
            digitalWrite(LED_BUILTIN, HIGH);
            
        } 
  }
  
}
				
			

After uploading the code, point your remote at the sensor and tap the UP button.

You should see the built-in LED of your Arduino turned on.

Congratulations! You have just learned how to interpret any IR signal and use them for a purpose.

Next, you should be able to write a code on your own to turn the LED off by pressing the down button of your remote control.

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top