[SOLVED]Arduino Leonardo/Yùn 38 kHz carrier

Hi guys,

i'm trying to generate a 38 kHz carrier signal to modulate an IR led. I succesfully tried with an Arduino Uno with the following code:

 #define pwmPin 11


 // toggle on compare, clk/1
 TCCR2A = _BV(WGM21) | _BV(COM2A0);
 TCCR2B = _BV(CS20);
 // 38kHz carrier/timer
 pinMode(pwmPin, OUTPUT);

Obviously it doesn't work on my Arduino Yùn (which has the same Leonardo uC Atmega32u4). How should i change the code? I have never used this kind of notation and i can't understand the meaning of each command (i took a look at the datasheet but it's very hard to decode for me).

Thank you!

The Leonardo/Yún/Atmega32u4 does not have a Timer2 which you are using in your, code.

I suggest you check out how it is done in the IRremote library.

Search for the use of Timer4 in the file linked above - which outputs the signal on pin 10 by default .

You could also look into the TimerOne library.

Thank you AnalysIR, i find in the library the Atmega32u4 part. I made a very simple substitution of the parameters name like this:

  #define pwmPin 10  

  TCCR4A = _BV(WGM40) | _BV(COM4A1);
  TCCR4B = _BV(CS40);
  // 38kHz carrier/timer
  pinMode(pwmPin, OUTPUT);

I'm going to try this code tonight, stay tuned!

Problem solved!

I followed another way to obtain what i need. You can find the solution at the third post in this topic: http://forum.arduino.cc/index.php?topic=247926.0


For the benefit of others, it might be useful to post the actual solution that worked for you (i.e. code).

For the benefit of others, it might be useful to post the actual solution that worked for you (i.e. code).

Of course!

1 - Download and install this version of PWM library: link PWM Leonardo

2 - If you want some examples, you can find them inside this older version of the library (copy only the examples!) : older version

3 - If you want to produce a continuous 38 kHz carrier (pwm @ 50% duty cycle) use this code in the setup() :

  #define pwmPin 10  
  #define frequency 38000

  void setup(){

      //initialize all timers except for 0, to save time keeping functions

      //sets the frequency for the specified pin
      bool success = SetPinFrequencySafe(pwmPin, frequency);

      pwmWrite(pwmPin, 128);