Maker.io main logo

Use Infrared Light to Communicate with Your Arduino

2017-12-05 | By All About Circuits

License: See Original Project Arduino

Courtesy of All About Circuits

Using an infrared light sensor enables your Arduino to receive and decode signals from a TV remote control.

It is common to find TV remote controls that use infrared (IR) light to send encoded messages to the television. IR light wavelength is normally between 930 and 950 nm, which makes it invisible to humans. When a button is pressed on a TV remote control, the controller modulates the IR signal with a pulse-width modulated (PWM) signal that is unique to that particular button. An IR receiver in the TV picks up this unique signal, demodulating the signal and figuring out which button was pressed. In this project, we do something similar with an Arduino by equipping it with an IR receiver.

An IR receiver has three terminals: two terminals power the device and the third terminal is for the sensor output. The receiver will demodulate the IR signal and output the demodulated PWM signal. We will connect the output terminal to Arduino pin 3. Then, the Arduino analyzes the PWM signal to figure out which button was pressed.

Experiment 1

For our first experiment, we use a TV remote control to turn two LEDs on and off.

Hardware Required

Wiring Diagram

Follow the figure below to connect the components. Connect the infrared receiver to pin 3 and the LEDs to pins 4 and 5 through 330-ohm current-limiting resistors.

Follow the figure below to connect the components.

Code for Experiment 1

We used the IRremote.h Arduino library in this experiment. Prior to writing this code, we determined that the IR receiver output would be equal to 2049 when the remote control button 1 was pressed and 2050 when button 2 was pressed. Pressing button 1 turns on the LEDs. Pressing button 2 turns them off.

Copy Code
                  #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
}
}

Download Code - ZIP

Experiment 2

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

Hardware Required

Wiring Diagram

Follow the same wiring diagram as in Experiment #1.

Code for Experiment #2

For this experiment, we will again use buttons 1 and 2 while adding in the remote control's power button. When the power button of the remote control is pressed, the output of the IR sensor when the remote control is 2060. Each time you press button 1, LED 1 turns on. Each time button you press button 2, LED 2 turns one. Finally, each time you press the power button, both LEDs will turn off.

Copy Code
                  #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
}
}

Download Code - ZIP

Videos

 

 

 

arduino-infrared scheme-it

arduino-infrared scheme-it-bom

Scheme-it Link

TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum