An infrared sensor is an electronic instrument that measures infrared light radiating from objects in its field of view. The most common application is the PIR motion sensor.
material required
Component name | Quantity |
Arduino | 1 |
IR receiver module | 1 |
LED | 2 |
220ohm Resistor | 2 |
TV Remote | 1 |
Jumper wires | as per requirement |
Breadboard | 1 |
IR modules/IR reciver
The IR receiver module work on 37.9KHz and receiving range 18m. Whenever the sensor exposes to IR radiation and that radiation falls in working range the module gives an output.
Ir sensor has 3 output legs VCC, GND, and Signal. This module work on 5v.
Connection of Arduino and IR sensor
5V of IR sensor will connect to 5v of Arduino. GND of IR will connect to GND of Arduino. The signal of the IR sensor will connect to Arduino pin no. 7.
Code
To work with the IR sensor IRremote library is required. Go to add library and add IRremote library. Copy the following code and upload it to Arduino and get a list of HEX Codes for all the button/keys on your remote.
#include <IRremote.h> //including infrared remote header file
int RECV_PIN = 7; // the pin where you connect the output pin of IR sensor
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(&results))// Returns 0 if no data ready, 1 if data ready.
{
int readResults = results.value;// Results of decoding are stored in result.value
Serial.println(" ");
Serial.print("Code: ");
Serial.println(results.value); //prints the value a a button press
Serial.println(" ");
irrecv.resume(); // Restart the ISR state machine and Receive the next value
}
}
Each button on the remote will give you different hex codes. Use these hex codes for different applications.
Connection of Arduino, IR sensor and LED
Code
#include <IRremote.h>
int RECV_PIN =7;
int bluePin = 9;
int yellowPin = 10;
int redPin = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop(){
if (irrecv.decode(&results)){
int value = results.value;
Serial.println(value);
switch(value){
case 12495: //Keypad button "1"
//set color red
analogWrite(redPin, HIGH);
analogWrite(yellowPin,LOW);
analogWrite(bluePin, LOW);
}
switch(value){
case -7177: //Keypad button "2"
//set color yellow
analogWrite(redPin, LOW);
analogWrite(yellowPin,HIGH);
analogWrite(bluePin, LOW);
}
switch(value){
case 539: //Keypad button "3"
//set color blue
analogWrite(redPin, LOW);
analogWrite(yellowPin,LOW);
analogWrite(bluePin, HIGH);
}
irrecv.resume();
}
}