[SOLVED] IRRemote RGB LED PWM at pin 11 breaks

Hello, I've got a strange problem with IRRemote on pin 11. Basically, I want to change the brightness with a remote on a RGB LED, but everytime I try to use it on pin 11, it just stops working. Every other color works, just the one at pin 11 doesn't.
EDIT: Using IRremote v3.3.0
Here's the code:

#include <IRremote.h>

int red = 11;
int green = 10;
int blue = 9;

int IR = 2;

int colorR;
int brightness = 255;

IRrecv irRecriver(IR);
decode_results answer;

void LedOff() {
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  }
void LedOn() {
  digitalWrite(red, HIGH);
  digitalWrite(green, HIGH);
  digitalWrite(blue, HIGH);
  }

void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(IR, INPUT);

  irRecriver.enableIRIn();
}

void loop() {
  if(irRecriver.decode(&answer)){
    Serial.println(answer.value, HEX);
    irRecriver.resume();

    switch(answer.value){
      case 0xFF30CF: //1
       digitalWrite(red, HIGH);
       colorR = 11;
       break;
      case 0xFF18E7: //2
       digitalWrite(green, HIGH);
       colorR = 10;
       break;
      case 0xFF7A85: //3
       digitalWrite(blue, HIGH);
       colorR = 9;
       break;
      case 0xFF10EF: //4
       digitalWrite(red, LOW);
       colorR = 0;
       break;
      case 0xFF38C7: //5
       digitalWrite(green, LOW);
       colorR = 0;
       break;
      case 0xFF5AA5: //6
       digitalWrite(blue, LOW);
       colorR = 0;
       break;
      case 0xFFE01F: //-
       LedOff();
       brightness = brightness - 50;
       if (brightness < 0) {
        brightness = 0;
       }
       analogWrite(colorR, brightness);
       break;
      case 0xFFA857: //+
       LedOff();
       brightness = brightness + 50;
       if (brightness > 254) {
        brightness = 255;
       }
       analogWrite(colorR, brightness);
       break;
    }  
 }
 delay(100);
}

PS: Sorry for bad English

Hello
take a look here to retrieve more information about IR business.

The IRremote library was recently upgraded and will not run older (pre version 3.0) code. You can delete the newer library from your scketchbook\libraries folder and install an older version of the library or modify the code to run on the newer version. Older versions of the library are available through the IDE library manager. Instructions on how to modify older code to work with the new library can be found here.

You would have seen a warning if you had had compiler warnings enabled in IDE Files, Preferences. serial monitor open when starting the code.

Thanks for the reply
I've already tried downgrading and it didn't work.
I'll try to upgrade the code to the new version, and see if it works
EDIT: Yes, I do have some errors when the sketch compiles

Hi, so I don't know if I upgraded the code wrong, but it says
"IrReceiver' does not name a type"

Here's the full error:

Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"





















2:16:1: error: 'IrReceiver' does not name a type

 IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

 ^~~~~~~~~~

C:\Users\Administrator\Documents\Arduino\projects\15 hodina\2\2.ino: In function 'void setup()':

2:36:3: error: 'irRecriver' was not declared in this scope

   irRecriver.enableIRIn();

   ^~~~~~~~~~

C:\Users\Administrator\Documents\Arduino\projects\15 hodina\2\2.ino:36:3: note: suggested alternative: 'IrReceiver'

   irRecriver.enableIRIn();

   ^~~~~~~~~~

   IrReceiver

C:\Users\Administrator\Documents\Arduino\projects\15 hodina\2\2.ino: In function 'void loop()':

2:40:6: error: 'irRecriver' was not declared in this scope

   if(irRecriver.decode(IrReceiver.decodedIRData)){

      ^~~~~~~~~~

C:\Users\Administrator\Documents\Arduino\projects\15 hodina\2\2.ino:40:6: note: suggested alternative: 'IrReceiver'

   if(irRecriver.decode(IrReceiver.decodedIRData)){

      ^~~~~~~~~~

      IrReceiver

2:41:49: error: no matching function for call to 'HardwareSerial::println(IRData&, int)'

     Serial.println(IrReceiver.decodedIRData, HEX);

                                                 ^

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:26:0,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:233,

                 from sketch\2.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:77:12: note: candidate: size_t Print::println(const __FlashStringHelper*)

     size_t println(const __FlashStringHelper *);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:77:12: note:   candidate expects 1 argument, 2 provided

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:78:12: note: candidate: size_t Print::println(const String&)

     size_t println(const String &s);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:78:12: note:   candidate expects 1 argument, 2 provided

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:79:12: note: candidate: size_t Print::println(const char*)

     size_t println(const char[]);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:79:12: note:   candidate expects 1 argument, 2 provided

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:80:12: note: candidate: size_t Print::println(char)

     size_t println(char);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:80:12: note:   candidate expects 1 argument, 2 provided

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:81:12: note: candidate: size_t Print::println(unsigned char, int)

     size_t println(unsigned char, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:81:12: note:   no known conversion for argument 1 from 'IRData' to 'unsigned char'

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:82:12: note: candidate: size_t Print::println(int, int)

     size_t println(int, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:82:12: note:   no known conversion for argument 1 from 'IRData' to 'int'

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:83:12: note: candidate: size_t Print::println(unsigned int, int)

     size_t println(unsigned int, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:83:12: note:   no known conversion for argument 1 from 'IRData' to 'unsigned int'

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:84:12: note: candidate: size_t Print::println(long int, int)

     size_t println(long, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:84:12: note:   no known conversion for argument 1 from 'IRData' to 'long int'

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:85:12: note: candidate: size_t Print::println(long unsigned int, int)

     size_t println(unsigned long, int = DEC);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:85:12: note:   no known conversion for argument 1 from 'IRData' to 'long unsigned int'

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:86:12: note: candidate: size_t Print::println(double, int)

     size_t println(double, int = 2);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:86:12: note:   no known conversion for argument 1 from 'IRData' to 'double'

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:87:12: note: candidate: size_t Print::println(const Printable&)

     size_t println(const Printable&);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:87:12: note:   candidate expects 1 argument, 2 provided

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:88:12: note: candidate: size_t Print::println()

     size_t println(void);

            ^~~~~~~

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:88:12: note:   candidate expects 0 arguments, 2 provided

2:44:36: error: switch quantity not an integer

     switch(IrReceiver.decodedIRData){

                                    ^

exit status 1

'IrReceiver' does not name a type

Yes, that looks like you do not have the library installed properly. Did you delete the IRremote library folder from your sketchbook\libraries folder? Then install the version desired? This page shows how to use the IDE library manager.

What Arduino are you using?

Your code compiles fine with no warnings or errors with version 2.80 of the library and seems to work. The program sees the remote and I can control the LEDs with my remote (codes modified to fit my remote).

Hi, I've tried reinstalling the library but I still have the same error, I guess I edited the code wrong. I'm using Arduino UNO.
Yes, I know that it compiles fine with the 2.80 version, but when you change the brightness on the pin 11 (on the RGB led (red for me)), the code stops working entirely, and in the serial console, it starts printing different numbers.
The brightness glitch was the only thing that was wrong, I think I didn't quite explain it in the first post.

I have wired up the circuit and using the posted code (modified with my key codes) I am seeing what you say. And just with the red LED (pin 11). Don't yet know why, though.

My suspicion is that the problem occurs, because the PWM for pin 11 uses timer 2 which is the same timer used by the library
<IRremote.h>.
I'm searching the web, to confirm about the pin 11 PWM,
and there is a conflict, but I haven't found confirmation yet.

RV mineirin

PS:

Timer output Arduino output Chip pin Pin namePin name
OC0A		6		   12             PD6      TIMER 0
OC0B		5		   11             PD5      TIMER 0
OC1A		9		   15             PB1      TIMER 1
OC1B		10		   16             PB2      TIMER 1
OC2A		11		   17             PB3      TIMER 2
OC2B		3           5             PD3      TIMER 2


https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
1 Like

That is a good idea. It happens when you have the LED on pin 11 and use the - button. From then on the IRremote returns seemingly random garbage if any thing.

I changed 11 to 6 (6 is on timer0) it all the places and now the code behaves the same for all the LEDs. Good catch @ ruilviana.

OP, I had to go through and find all the magic numbers. You name the pins so why don't you use the pin names in the code?

Hi, I didn't understand your answer when I first saw it, so you're saying that if I reconnect the pin (on the board) from pin 11 to pin 6 it would work?
Also, when you said that you had to find the pins colors, well it's on top of the code

Yes they are named at the top of the code, but thereafter you use the pin numbers instead of the names. That makes it so that to change the pin associated with red, one must go through the code and change all occurrences of 11 to 6. If you use the name, red in the code instead of the pin number, then you only have to change 11 to 6 in one place at the top of the code.

  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

 colorR = 11;
colorR = 10;
colorR = 9;

Examples of you using pin numbers instead of names.

Pin 6 is on a different timer (timer0) than the timer that IRremote library uses (timer2).

Hi, so I just reconnected the pin from 11 to 6, and it works.
Thank you so much and ruilaviana for solving my problem.
I'm marking this post as [SOLVED]

@ruilaviana should get the credit for solving this. I did not even think of the timer conflict.

1 Like

Ok, the "Solution" flag is on ruilavianas post.

2 Likes

@groundFungus and @sucharek

thank you so much

RV mineirin

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.