I need help with my Arduino IR sensor and IR remote. I'm actually new to Arduino I just bought my first Arduino kit around 20 October 2022 so I was trying to make a simple thing with my IR things like when I press my remote's button the led with blink but when I saw tutorials on youtube.. it doesn't work I wasted my past few hours doing it so please help me at least make my newbie time with Arduino good
This is the code I just copied from a random article
#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 results.value = results;// 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
}
Confirm the sensor output is going to Arduino pin 7.
Try this:
#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();
} //END of setup()
//********************************************^************************************************
void loop()
{
//**************************************
if (irrecv.decode(&results)) //this checks to see if a code has been received
{
if (results.value != 0xFFFFFFFF)
{
Serial.print("Code: ");
Serial.println(results.value, HEX); //prints the hex value a a button press
}
irrecv.resume(); // Restart the ISR state machine and Receive the next value
}
} //END of loop()
//IR remote ON/OFF example
//********************************************^************************************************
// Version YY/MM/DD Description
// 1.00 21/09/27 Running sketch
//
//
//Install IRremote Version 3.3.0
#include <IRremote.h> // Ver 3.3.0 <-------<<<<<<<
#define DECODE_NEC
unsigned long irMillis;
// s e t u p ( )
//******************************************************************************************
void setup()
{
Serial.begin(115200);
pinMode(13, OUTPUT); //LED feedback
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
//**************************************
const byte IR_RECEIVE_PIN = 2;
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
//IR_RECEIVE_PIN (UNO) default = 2
//IrReceiver.begin(IR_RECEIVE_PIN); //receive on pin 2
//IrReceiver.begin(4); //receive on pin 4
//disableLEDFeedback();
//enableLEDFeedback ();
//use pin 12, defaults to 13, true/false
//setLEDFeedback(12,true);
} //END of setup()
// l o o p ( )
//******************************************************************************************
void loop()
{
//*******************************************************
if (IrReceiver.decode())
{
//Some code lines to try
//
//IrReceiver.printIRResultShort(&Serial);
//IrReceiver.printIRResultRawFormatted(&Serial, true);
//IrReceiver.printIRResultMinimal(&Serial);
//Serial.println(IrReceiver.decodedIRData.command);
//Serial.println(IrReceiver.decodedIRData.address);
//Serial.println(IrReceiver.decodedIRData.protocol);
//Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
IrReceiver.resume(); // Enable receiving of the next value
}
//*******************************************************
//have we received a new IR code form the remote ?
if (IrReceiver.decodedIRData.command != 0)
{
checkIRcode();
}
} //END of loop()
// c h e c k I R c o d e ( )
//******************************************************************************************
void checkIRcode()
{
Serial.println(IrReceiver.decodedIRData.command);
switch (IrReceiver.decodedIRData.command)
{
//********************* key #1
case 22:
{
digitalWrite(12, !digitalRead(12));
}
break;
//********************* key #2
case 25:
{
digitalWrite(11, !digitalRead(11));
}
break;
//********************* key #3
case 13:
{
digitalWrite(10, !digitalRead(10));
}
break;
//********************* key #4
case 12:
{
digitalWrite(9, !digitalRead(9));
}
break;
} //END of switch...case
//needed to block repeat codes
IrReceiver.decodedIRData.command = 0;
} //END of checkIRcode()
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.
uhm excuse me sorry for late reply but when I try that code it says
14:41:49.393 -> The function decode(&results)) is deprecated and may not work as expected! Just use decode() without a parameter and IrReceiver.decodedIRData.
//Install IRremote Version 3.3.0
#include <IRremote.h> // Ver 3.3.0 <-------<<<<<<<
//**************************************
const byte IR_RECEIVE_PIN = 7;
//********************************************^************************************************
void setup()
{
Serial.begin(115200);
IrReceiver.begin(IR_RECEIVE_PIN);
} //END of setup()
//********************************************^************************************************
void loop()
{
//*******************************************************
//have we received a new IR code form the remote ?
if (IrReceiver.decode())
{
if (IrReceiver.decodedIRData.command != 0)
{
Serial.print("Code: ");
Serial.println(IrReceiver.decodedIRData.command);
//needed to block repeat codes
IrReceiver.decodedIRData.command = 0;
}
IrReceiver.resume();
}
//*******************************************************
// other non blocking code goes here
//*******************************************************
} //END of loop()