This article covers:
- 1: Introduction to IR Sensor
- 2: IR Sensor Pinout
- 3: Interfacing IR Sensor with Arduino
- 3.1: Schematic
- 3.2: Installing the Required Library
- 4: Decoding an IR Remote Buttons
- 4.1: Code
- 4.2: Output
- 5: Controlling LED Using IR Remote
- 5.1: Schematic
- 5.2: Code
- 5.3: Output
- Conclusion
1: Introduction to IR Sensor
An IR receiver is a device that can detect and receive infrared (IR) signals. It is typically used in IR remote control systems to receive commands from an IR remote controller or other IR transmitter. The IR receiver consists of a photodiode or other IR sensitive component that detects the IR signals and a demodulator circuit that decodes the signals.
The output from the IR receiver is usually a digital signal that can be processed by a microcontroller or other electronic device. IR receivers are commonly used in a wide range of consumer electronics, including televisions, DVD players, air conditioners, and other home appliances, as well as in industrial and automotive applications.
It’s worth noting that IR receiver sensors can be susceptible to interference from other infrared sources, such as sunlight or other IR devices. To avoid this, it’s recommended to point the IR receiver sensor directly at the IR remote control, and to keep the sensor away from other infrared sources.
2: IR Sensor Pinout
The IR receiver sensor is a small module that typically has three pins:
- VCC
- GND
- OUT
The VCC pin is connected to a power supply, such as the 5V pin on the Arduino, to provide power to the sensor. The OUT pin is connected to a digital input pin on the Arduino and GND pin is connected to the ground pin on the Arduino:
3: Interfacing IR Sensor with Arduino
To use the IR receiver sensor with an Arduino Uno, a library called IRremote is required. This library can be downloaded from the Arduino website and allows the Arduino to decode the IR signals received by the sensor. Once the library is installed, it can be included in the sketch (program) that is uploaded to the Arduino.
Once the IR receiver sensor is connected and the sketch is uploaded, the Arduino can begin to receive and interpret signals from an IR remote control. The IR signals can be decoded using a function provided by the IRremote library, and the decoded signals can then be used to control various devices.
For example, a specific IR signal can be used to turn a device on or off, or to control the brightness or temperature.
3.1: Schematic
Here is a basic schematic for connecting an IR sensor to an Arduino:
IR sensor | Arduino |
---|---|
VCC (power) | 5V |
GND (ground) | GND |
OUT (output) | D8 |
It’s important to note that different IR sensors may have different pinouts and voltages, so it’s important to check the datasheet for the specific sensor you’re using.
3.2: Installing the Required Library
There are several IR remote libraries available for Arduino. You can use any of these libraries as per your requirement and IR remote protocol you are using.
We will install the IRremote library by ArminJo.
4: Decoding an IR Remote Buttons
There are multiple IR remotes available such as for televisions, air conditioners, and home automation systems. By decoding an IR remote we can design a universal remote for all these appliances.
To decode an IR remote, upload the below given code and press the buttons on the IR remote then observe the serial monitor of the Arduino IDE. The example sketch will print the IR signal in HEX format and the protocol used.
Extract the key codes of the IR remote buttons you want to use in your project. Using these key codes and the IRremote library functions we can control the desired actions when the corresponding buttons are pressed on the IR remote.
4.1: Code
Upload the code given below through the Arduino IDE on Arduino Uno:
IRrecv IR(8); /*D8 Arduino Pin defined*/
void setup() {
IR.enableIRIn(); /*IR Communication Enabled*/
Serial.begin(9600); /*Serial Baud Rate defined*/
}
void loop() {
if (IR.decode()) { /*IR Remote library function to decode remote */
Serial.println(IR.decodedIRData.decodedRawData, HEX); /*Print HEX value*/
delay (1000);
IR.resume (); } /*Wait for next input*/
}
Code started by including an IR remote library. After that we defined the Arduino digital pin at which the IR signal will be read. Next in the setup() part we initialize the IR communication and baud rate is defined.
In loop() part of code we decode the received IR signal in HEX format which is then printed on serial monitor.
4.2: Output
After uploading the code, we pressed three buttons Red, Green and Blue on the IR remote.
Following HEX code is obtained for the red, green and blue button on the IDE serial monitor.
IR Remote Button | HEX Code |
---|---|
RED Button | 0xFB04EF00 |
GREEN Button | 0xFA05EF00 |
BLUE Button | 0xF906EF00 |
5: Controlling LED Using IR Remote
To control a device with the IR remote, you will need to use the decoded HEX signals in your sketch. For example, you can use an if-else statement to check the decoded signal and turn on or off a specific device. You can also use the decoded signals to control the brightness or temperature of a device.
5.1: Schematic
The given image explains the connection of three different colored LEDs with Arduino and IR receiver.
LED & IR Sensor | Arduino Pin |
---|---|
RED | D5 |
GREEN | D6 |
BLUE | D7 |
IR Sensor OUT | D8 |
5.2: Code
Following is the Arduino code for IR remote control LEDs. The code can be uploaded to the Arduino Uno board by opening the Arduino IDE.
IRrecv IR(8); /*IR Pin defined*/
int blue=7; /*Blue LED at PIN D7*/
int green=6; /*Green LED at PIN D6*/
int red=5; /*Red LED at PIN D5*/
bool Red_State=1; /*RED LED State*/
bool Green_State=1; /*Green LED State*/
bool Blue_State=1; /*Blue LED State*/
void setup() {
IR.enableIRIn(); /*IR Communication enables*/
pinMode(blue, OUTPUT); /*Blue LED pin set as output*/
pinMode(green, OUTPUT); /*Green LED pin set as output*/
pinMode(red, OUTPUT); /*RED LED Pin Set as Output*/
Serial.begin(9600);
}
void loop() {
if (IR.decode()){ /*Decode IR Signal in HEX format*/
Serial.println (IR.decodedIRData.decodedRawData, HEX);
/*Check for IR input*/
/*Red LED code*/
if (IR.decodedIRData.decodedRawData == 0xFB04EF00 && Red_State == 1) {
digitalWrite(red, HIGH);
Serial.println("RED LED ON");
Red_State = 0;
}
else if(IR.decodedIRData.decodedRawData == 0xFB04EF00 && Red_State == 0)
{
digitalWrite(red, LOW);
Serial.println("RED LED OFF");
Red_State = 1;
}
/*Check for IR output*/
if (IR.decodedIRData.decodedRawData == 0xFA05EF00 && Green_State == 1) {
/*Green LED Code*/
digitalWrite(green, HIGH);
Serial.println("GREEN LED ON");
Green_State = 0;
}
else if(IR.decodedIRData.decodedRawData == 0xFA05EF00 && Green_State == 0)
{
digitalWrite(green, LOW);
Serial.println("GREEN LED OFF");
Green_State = 1;
}
/*Check for IR code*/
if (IR.decodedIRData.decodedRawData == 0xF906EF00 && Blue_State == 1) {
/*Blue LED Code*/
digitalWrite(blue, HIGH);
Serial.println("BLUE LED ON");
Blue_State = 0;
}
else if(IR.decodedIRData.decodedRawData == 0xF906EF00 && Blue_State == 0)
{
digitalWrite(blue, LOW);
Serial.println("BLUE LED OFF");
Blue_State = 1;
}
IR.resume ();
}
}
Code started by including an IR remote library. After that we defined the Arduino digital pin at which the IR signal will be read. Next three pins for LEDs are defined. All three LEDs have separate states so that we can use the toggle function inside the code using the AND condition.
In the setup() part we initialize the IR communication and baud rate is defined. Along with that all three LED pins are set as output using pinMode() function.
In loop() part of code if-else condition is separately used for all three LEDs. All three LEDs are controlled separately using the HEX code.
IR Remote Button | HEX Code |
---|---|
RED Button | 0xFB04EF00 |
GREEN Button | 0xFA05EF00 |
BLUE Button | 0xF906EF00 |
Note: Remember these are the HEX code for the remote one we are using. Your remote may have a different HEX code. So, replace the code with HEX code you got in the serial monitor.
5.3: Output
After uploading code to the Arduino board press any of the three-button respective LEDs will glow. If we press the red button the red LED will glow and vice versa:
Similarly, we can also glow these LEDs simultaneously. To turn OFF the LED simply press the button again as we have used the toggle condition in Arduino code.
Using the same method, we can control AC appliances by replacing the LEDs with a Relay switch.
Conclusion
In summary, decoding an IR remote control with an Arduino microcontroller is a simple and cost-effective way to control various devices. By connecting an IR receiver to the Arduino, uploading a sketch, and decoding the IR signals, you can easily control devices such as televisions, air conditioners, and home automation systems.