#include <IRremote.h>
IRrecv irReceiver(9); // Create an IR receiver object on pin 11
decode_results results;
void setup()
{
Serial.begin(9600);
irReceiver.enableIRIn(); // Start the IR receiver
}
void loop()
{
if (irReceiver.decode(&results))
{ // Check if there's an IR signal
Serial.println(results.value); // Print the value of the received signal
irReceiver.resume(); // Prepare to receive the next signal
}
}
I could not get your code to run, however I have already tried an equivalent to get the codes in the first instance. This seems to have the same behaviour, mostly works but will miss random instances.
#include <IRremote.h>
#define IR_RECEIVE_PIN 6
long ircode = 0;
void setup()
{
Serial.begin(4800);
IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);
}
void loop()
{
if (IrReceiver.decode())
{ // Check if there's an IR signal
ircode = (IrReceiver.decodedIRData.decodedRawData);
Serial.println(ircode); // Print the value of the received signal
}
IrReceiver.resume(); // Prepare to receive the next signal
}
Distance doesn't matter. On the scope I can see a perfect input stream to the Nano. Just sometimes it doesn't see it.
Have used the same library with ESP32 without issue so I'm really questioning if there is some quirk with the Nano, and especially Ali Express variants.
IRremote 4.x on ATmega328P uses Timer2. If any part of your project or libraries (Servo, tone, PWM on pins 3/11, etc.) use Timer2, decoding becomes unreliable.
You are using digitalWrite(11). If pin 11 ever had PWM enabled before, that uses Timer2. Normally digitalWrite() disables PWM, but clones sometimes fail to fully reset Timer2 states.