Project

Remote Viewing: Communicating with an Arduino via Infrared Light

June 14, 2015 by Tim Youngblood

An infrared (IR) light sensor allows the use of an Arduino to receive and decode signals from a TV remote control.

An infrared (IR) light sensor allows the use of an Arduino to receive and decode signals from a TV remote control.

Most TV remote controls use infrared light to send encoded messages to the TV set. The wavelength of this IR light is normally between 930 and 950 nm, making it invisible to humans. When you press one of the buttons on a TV remote control, the control will modulate the IR signal with a pulse-width modulated (PWM) signal unique to that button. This signal is picked up by an IR receiver in the television, which demodulates the signal and figures out which button was pressed. We can do something similar with an Arduino, if we equip it with an IR receiver.

 

The IR receiver has three terminals; two are for powering the device while the remaining one is for the sensor output. The receiver demodulates the IR signal and outputs the demodulated PWM signal. The output terminal is connected to Arduino pin 3. The Arduino then figures out which button was pressed by analyzing the PWM signal.

 

Experiment 1

 

In this experiment, we will use a TV remote control to turn on and off two LEDs.

 

Hardware Required

  • 1x TV remote controller
  • 1 x IR receiver
  • 1 x Arduino Mega2560
  • 2 x LED
  • 2 x 330 ohm resistor
  • 1 x breadboard
  • jumper wires

 

Wiring Diagram

Connect the components as shown in the figure below. The infrared receiver connects to pin 3. The LEDs are connected to pins 4 and 5 through 330-ohm current-limiting resistors.

Circuit Diagram

 

Code for Experiment 1

For this experiment, we will use the IRremote.h Arduino library. Before writing this code, we determined that the IR receiver output would be equal to 2049 if the remote control 1 button was pressed and 2050 if button 2 was pressed. Pressing button 1 turns on the LEDs. Pressing button 2 turns them off.


#include "IRremote.h"
 
int rec = 3; // the pin where you connect the output pin of TSOP4838
int led_1 = 4;
int led_2 = 5;

#define code1  2049 // code received from button 1
#define code2  2050 // code received from button 2


 
IRrecv ir_rec(rec);
 
decode_results result;
 
void setup()
{
  Serial.begin(9600);   
  ir_rec.enableIRIn();  
  pinMode(led_1, OUTPUT);
  pinMode(led_2, OUTPUT);
 
}
 
void loop() {
  if (ir_rec.decode(&result)) {
    unsigned int val = result.value;
    switch(val) {
       case code1:
          digitalWrite(led_1,HIGH);
          digitalWrite(led_2,HIGH);
          break;
       case code2:
          digitalWrite(led_2,LOW);
          digitalWrite(led_1,LOW);
          break;
       
    } 
      
    Serial.println(val); 
    ir_rec.resume();                   // Receive the next value
  }
}

Infred_Light.zip

Experiment 2

In this experiment, we will turn on and off different LEDs using the TV remote control.

 

Hardware Required

  • 1 x TV remote control
  • 1 x IR receiver
  • 1 x Arduino Mega2560
  • 2 x LED
  • 2 x 330 ohm resistor
  • 1 x breadboard
  • jumper wires

 

Wiring Diagram

The wiring diagram is exactly the same as Experiment #1

 

Code for Experiment #2

In this experiment, we will use the remote control's power button in addition to buttons 1 and 2. The output of the IR sensor when the remote control power button is pressed is 2060. Pressing button 1 turns on LED 1, pressing button 2 turns on LED 2, pressing the power button turns them both off.

 


#include "IRremote.h"
 
int rec = 3; // the pin where you connect the output pin of TSOP4838
int led_1 = 4;
int led_2 = 5;

#define code1  2049 // code received from button 1
#define code2  2050 // code received from button 2
#define code3  2060 // code received from power button



 
IRrecv ir_rec(rec);
 
decode_results result;
 
void setup()
{
  Serial.begin(9600);   
  ir_rec.enableIRIn();  
  pinMode(led_1, OUTPUT);
  pinMode(led_2, OUTPUT);
 
}
 
void loop() {
  if (ir_rec.decode(&result)) {
    unsigned int val = result.value;
    switch(val) {
       case code1:
          digitalWrite(led_1,HIGH);// TURNS LED_1 ON WHEN BUTTON 1 IS PRESSED
          digitalWrite(led_2,LOW);
          break;
       case code2:
          digitalWrite(led_2,HIGH);// TURNS LED_2 ON WHEN BUTTON 2 IS PRESSED
          digitalWrite(led_1,LOW);
          break;
       case code3:
          digitalWrite(led_2,LOW);// TURNS  BOTH LEDS OFF WHEN POWER BUTTON IS PRESSED
          digitalWrite(led_1,LOW);
          break;
       
       
    } 
      
    Serial.println(val); 
    ir_rec.resume();                   // Receive the next value
  }
}

ir_communication_(exp2).ino.zip

Videos

Give this project a try for yourself! Get the BOM.