Exenifix
Exenifix

Reputation: 91

Arduino breaking both IR input and PWM output when used together

I made wrote this fairly simple code which sets output on LED pin to some analog number

#include <Arduino.h>
#include <IRremote.hpp>

#define IR_RECEIVE_PIN 7
#define LED_PIN 11


uint16_t getCommand() {
    const IRData data = IrReceiver.decodedIRData;
    return data.flags == 1 ? 0 : data.command;
}

void setup() {
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, LOW);

    Serial.begin(9600);
    Serial.println(F("Running TestIRBlink"));
    IrReceiver.begin(IR_RECEIVE_PIN);
}

void loop() {
    if (!IrReceiver.decode()) {
        return;
    }
    const uint16_t cmd = getCommand();
    if (cmd != 0) {
        Serial.println(cmd);
    }
    if (cmd == 7) {
        analogWrite(LED_PIN, 120);
    } else if (cmd == 9) {
        analogWrite(LED_PIN, 230);
    }
    IrReceiver.resume();
}

IR receiver is able to receive all commands until I use 7 or 9. After that, LED does not light up, but IR stops recognizing commands as well. I have no idea why this happens.

Tested LED with PWM separately from IR receiver, it works. Also nothing breaks when using digitalWrite(). I assume PWM and receiver conflict in some way, but I couldn't find out how for 3 days now.

Upvotes: 0

Views: 189

Answers (1)

Exenifix
Exenifix

Reputation: 91

Resolved this by myself now. Turns out, IRremote uses Timer2, which made pins 3 and 11 unsuitable for PWM operations. Switching LED pin to any other PWM pin resolves the issue.

Upvotes: 0

Related Questions