Installing the IRremote library
This step is important idf you have others IR libraries such as IR Robot which might cause conflict.
This first part of the connection without the LEDs is just to test the functionality of the IR receiver. We shall use this basic connection to get the Hex values from the remote control.
Enter the following code on the Arduino IDE.
#include <IRremote.h>
int recvPin = 11;
IRrecv irrecv(recvPin);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
delay(100);
}
Open Serial Monitor on the Arduino IDE
In this project you want to control 3 LEDs. Choose 6 buttons for the following tasks:
Press, f the button number 1 on the remote control. You should see a code on the serial monitor. Press the same button several times to make sure you have the right code for that button. Ignore any codes with FFFFFFFF .
Convert the Hex values to Decimal. https://www.binaryhexconverter.com/hex-to-decimal-converter
Modify the code we used before to obtain the Hex values from the remote control.
/*
* -Using IR receive to control a LED
* Library by Ken S.
*/
#include <IRremote.h> // add IR remote library
int recvPin = 11; // set the pin to receive the signal on
int blueLed = 10;
int greenLed = 9;
int yellowLed = 8;
IRrecv irrecv(recvPin); // defone the pin as a recv pin
decode_results results;
void setup() {
Serial.begin(9600); // starts the serial monitor
irrecv.enableIRIn(); // starts the receiver
pinMode(blueLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
}
void loop() {
//decodes the infrared input
if (irrecv.decode(&results)) {
long int decCode = results.value;
Serial.println(results.value);
//switch case to use the selected remote control button
switch (results.value) {
case 2160019894: //when button 1 is pressed
digitalWrite(blueLed, HIGH);
break;
case 2160030094: //when button 4 is pressed
digitalWrite(blueLed, LOW);
break;
case 2160052534: //when button 2 is pressed
digitalWrite(greenLed, HIGH);
break;
case 2160062734: //when button 5 is pressed
digitalWrite(greenLed, LOW);
break;
case 2160014284: //when button 3 is pressed
digitalWrite(yellowLed, HIGH);
break;
case 2160006124: //when button 6 is pressed
digitalWrite(yellowLed, LOW);
break;
}
irrecv.resume(); // continue to the next value
}
delay(10);
}