In this quick tutorial we are going to learn how to interface IR receiver with Arduino. By using an IR remote control with Arduino, you can gain control over many functions for your projects. Just integrate it into your code, and you’ll have the benefits of an infrared transmitter and receiver.
Table of Contents
Required Components:
Product Name | Quantity | ||
---|---|---|---|
Arduino NANO | 1 | https://amzn.to/3jVNZON | https://amzn.to/3KpUQry |
KY-022 IR receiver sensor | 1 | https://amzn.to/3Vx2rMp | https://amzn.to/3RDxGnV |
5V power supply (USB or External). | 1 | https://amzn.to/3s1a8g3 | https://amzn.to/364yInH |
Few Connecting Wires | https://amzn.to/3H2BV4e | https://amzn.to/3J0WVu2 |
What is an infrared (IR) receiver?
An infrared receiver is used to receive infrared signals such as those used in IR remote controls . On the First layer it has a red filter which does not allow other frequencies to pass into it which could interfere with the receiver.Then a photosensitive diode is placed to capture infrared light and a band-pass filter that separates the 38 kHz frequency, in addition some other electronic circuits that subsequently recover the information.
VS 1838B Infrared Receiver Module
One of the widely used IR receivers is the VS 1838B, as depicted in the image above. Despite its unassuming appearance, this module hosts complex electronics to make sense of the incoming infrared signals. Its datasheet, represented in the diagram below, provides a deeper insight into the intricacies of this tiny yet powerful device.
How Does an Infrared Remote and IR Receiver Work?
The range of an infrared remote control typically extends to about 10 meters in a direct line of sight.
To create an IR remote with Arduino, we can utilize any IR remote control we have at home or you can buy a mini remote online. These remotes contain an internal electronic circuit with a processor and an LED (Light Emitting Diode) that emits modulated infrared signals.
Each button or key on the remote sends a different code, modulated according to the chosen protocol.
In our IR remote with Arduino project, we need to capture this modulation using an IR receiver. For this purpose, we’ll use the KY-022 infrared sensor receiver module. One advantage of this module is that the printed circuit board clearly labels its terminals, making it easy to connect to an Arduino without any confusion.
First with an Arduino program, we can obtain a unique hexadecimal code for each button we wish to use. In our case, we’ll use the 1 and 2 keys.
Next, in a second program, we’ll enable our remote to turn the built-in LED on pin 13 of our Arduino Nano on and off.
Interfacing IR infrared receiver to the Arduino?
The connection is very simple, we just have to be careful to respect the power terminals VCC (positive, 5V), GND (negative) and the signal output of the module. Connect all the required components as shown in the below circuit diagram.
Circuit Diagram:
As you can see from the above circuit diagram KY-022 IR receiver is connected to Arduino NANO. The GND pin and VCC pin of IR receiver module are connected to GND and VIN pins of Arduino respectively. The signal pin of the IR receiver is connected to D13 pin of Arduino as it is the pin for Built in LED pin of Arduino. That’s it for connections lets move to the next part.
Program Code
After connecting all the components, connect Arduino to the PC where Arduino IDE is installed. And install the required libraries given below.
Required libraries:
- IR Remote Library – Download link
First we will get the HEX codes of the remote buttons with help of an example code below.
#include <IRremote.h> int IR = 11; void setup() { Serial.begin(9600); IrReceiver.begin(IR, DISABLE_LED_FEEDBACK); } void loop() { if (IrReceiver.decode()) { Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); IrReceiver.resume(); } delay (100); }
Copy the below code and upload it to Arduino. After uploading open serial monitor at baud rate 9600. Now press the buttons on the remote, In our case lets use only 1 and 2 number buttons. when you press 1 button the serial monitor prints the HEX code of 1 button, note down and do the same for 2 button.
For my remote control, key 1 has hex code FF00BF00 and key 2 has hex code FE01BF00.
Now lets write an another program which can control the built in LED of Arduino. Pressing 1 should turn on the LED and pressing 2 button should turn Off the LED.
#include <IRremote.h> #define Button_1 0xFF00BF00 #define Button_2 0xFE01BF00 int IR = 11; int LED = 13; void setup() { Serial.begin(9600); IrReceiver.begin(IR, DISABLE_LED_FEEDBACK); pinMode(LED, OUTPUT); } void loop() { if (IrReceiver.decode()) { Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); if (IrReceiver.decodedIRData.decodedRawData == Button_1) digitalWrite(LED, HIGH); if (IrReceiver.decodedIRData.decodedRawData == Button_2) digitalWrite(LED, LOW); IrReceiver.resume(); } delay (100); }
After uploading the code when you press button 1 the built in LED will turn on and when you press button 2 on remote the LED will turn OFF.
Code Explanation
Sure, here’s a concise explanation of the code without code boxes:
- Include the IRremote library: This library handles IR remote decoding.
- Define button codes:
#define Button_1 0xFF00BF00
#define Button_2 0xFE01BF00
These are the hexadecimal codes for two buttons on the IR remote. - Set up pin assignments:
int IR = 11;
int LED = 13;
IR
is the pin for the IR receiver, andLED
is the pin for the LED. - Setup function: Initializes serial communication at 9600 baud, sets up the IR receiver on pin 11, and configures the LED pin as an output.
- Loop function:
- Checks if an IR signal is received.
- Prints the received IR code to the serial monitor in hexadecimal format.
- Turns the LED on if the received code matches
Button_1
. - Turns the LED off if the received code matches
Button_2
. - Resumes the IR receiver to prepare for the next signal.
- Waits for 100 milliseconds before repeating the loop.