PWM Code runs on mega2560 but not on Uno

Hi all,

Fairly new to the platform and my first post. Started out my journey with the Mega 2560 but realized it is overkill for some projects. Bought my first UNO and trying to make the below code run on it. Using an IR Remote Transmitter to PWM an LED array and do some other functions. Everything works great on the Mega, but I'm getting no signal out on Pin 11 when loaded on the UNO. Pin 11 on the UNO is functional as I tested it with a much simpler sketch to adjust the PWM value (verified on scope). Any ideas why this code will not run on the UNO ?

//sketch Rev. 03-08-2022 T.Decker

#include "IRremote.h" // Library for IR Receiver

const int outputPin = 11; // 0 - 255 PWM output on Pin 11
const int RECV_PIN = 10; // Signal Pin of IR receiver on Arduino Digital Pin 10
unsigned long key_value = 0 ;
int pwmValue;
int delayPeriod = 10; // delay in mS between PWM update during LED ramp up or down functions

/-----( Declare objects )-----/
IRrecv irrecv(RECV_PIN); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'

/-----( Function )-----/
//void translateIR() // takes action based on IR code received

void setup()
{
pinMode(outputPin, OUTPUT);
// TCCR1B = TCCR1B & B11111000 | B00000100; // Changes PWM output frequency of pin 11 to 122.55 Hz (Mega2560 only)
// TCCR1B = TCCR1B & B11111000 | B00000011; // Default timer setting for pin 10 and 9 (UNO only)
// TCCR2B = TCCR2B & B11111000 | B00000110; // Pin D3 and D11 to change PWM frequency to 122.55 Hz (Uno Only)
// TCCR0B = TCCR0B & B11111000 | B00000100; // for PWM frequency of 244.14 Hz on D5 and D6 (UNO only)

irrecv.enableIRIn(); // Enables the IR receiver
irrecv.blink13(true); // Blinks Arduino LED (on pin 13)
}

void loop()
{
if (irrecv.decode(&results))
{
if (results.value == 0XFFFFFFFF)
results.value = key_value;

    switch(results.value)
    {  
      case 0xFFA25D: //Keypad button "Power" turns LED off"
      pwmValue = 0 ;
      analogWrite(outputPin, pwmValue);
      break; 
      
      case 0xFF52AD: //Keypad button "9" Full brightness
      pwmValue = 255 ;
      analogWrite(outputPin, pwmValue);
      break; 
              
      case 0xFF4AB5: //Keypad button "8"
      pwmValue = 227 ;
      analogWrite(outputPin, pwmValue);
      break;  
             
      case 0xFF42BD: //Keypad button "7"
      pwmValue = 198 ;
      analogWrite(outputPin, pwmValue);
      break;         

      case 0xFF5AA5: //Keypad button "6"
      pwmValue = 170 ;
      analogWrite(outputPin, pwmValue);
      break;  
             
      case 0xFF38C7: //Keypad button "5"
      pwmValue = 143 ;
      analogWrite(outputPin, pwmValue);
      break;         
    
      case 0xFF10EF: //Keypad button "4"
      pwmValue = 80 ;
      analogWrite(outputPin, pwmValue);    
      break;     
    
      case 0xFF7A85: //Keypad button "3"
      pwmValue = 40 ;
      analogWrite(outputPin, pwmValue);
      break;
    
      case 0xFF18E7: //Keypad button "2"
      pwmValue = 20 ;
      analogWrite(outputPin, pwmValue); 
      break;        
  
      case 0xFF30CF: //Keypad button "1"
      pwmValue = 10 ;
      analogWrite(outputPin, pwmValue); 
      break;
    
      case 0xFF6897: //Keypad button "0"  Dimmest setting
      pwmValue = 5 ;
      analogWrite(outputPin, pwmValue);
      break; 

      case 0xFF906F: //Keypad button "up arrow" generates slow ramp up from 0 to 255 pwm
        
          for (int n = 0; n < 256; n ++ )
          {
          pwmValue = n ;
          delay (delayPeriod);
          analogWrite(outputPin, pwmValue);
          delay (delayPeriod);
          }
          delay (50);
          analogWrite(outputPin, 0);  // toggle off to indicate end of Ramp
          delay (50);
          analogWrite (outputPin, pwmValue);  // toggle on back to full brightness
      break; 
      
      case 0xFFE01F: //Keypad button "down arrow" generates slow ramp down from 255 to 0 pwm
        
          for (int n = 255; n > -1; n -- )
          {
          pwmValue = n ;
          delay (delayPeriod);
          analogWrite(outputPin, pwmValue);
          delay (delayPeriod);
          }
         delay (50);
      break; 

      case 0xFFC23D: //Keypad button "Fast Forward" generates 35 fast flashes at 5Hz
        
          for (int n = 0; n < 35 ; n ++ )
          {
          pwmValue = 255 ;
          analogWrite(outputPin, pwmValue);
          delay (100);
          pwmValue = 0 ;
          analogWrite(outputPin, pwmValue);
          delay (100);
          }   
      break; 
      
    }
    key_value = results.value;
    irrecv.resume(); 

}
}

Please use Code Tags </> to present code.

The IRremote library uses a timer that consequently is not available for PWM outputs. You used pin 11 that on the Uno is related to timer 2 (OC2A). Use a different PWM output tied to timer 1 (pin 9,10).

Do you know and read this? Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols? And please start with the examples of the library and not some legacy code :slight_smile:

Thank you DrD, this was helpful. I have it running on a different Timer related to pin 9. I guess I did not realize the timer assignments were different across the various Arduino boards. Lesson learned

The defaults are different, but you can override them with your own choice. On an Uno however the 8 bit T2 is sufficient for IRremote timing, leaving 16 bit T1 for more demanding purposes.

If that's all then you can mark your topic Solved.

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