Go Down

Topic: [SOLVED]Arduino Leonardo/Yùn 38 kHz carrier  (Read 854 times) previous topic - next topic

zeroG

Apr 29, 2016, 03:16 pm Last Edit: May 01, 2016, 08:57 pm by zeroG
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:

Code: [Select]

 #define pwmPin 11

 [...]

 // toggle on compare, clk/1
 TCCR2A = _BV(WGM21) | _BV(COM2A0);
 TCCR2B = _BV(CS20);
 // 38kHz carrier/timer
 OCR2A = (F_CPU/(IR_CLOCK_RATE*2L)-1);
 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!

AnalysIR

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.
https://github.com/z3t0/Arduino-IRremote/blob/master/IRremoteInt.h

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.


zeroG

#2
May 01, 2016, 03:50 pm Last Edit: May 01, 2016, 03:51 pm by zeroG
Thank you AnalysIR, i find in the library the Atmega32u4 part. I made a very simple substitution of the parameters name like this:

Code: [Select]

  #define pwmPin 10 

  TCCR4A = _BV(WGM40) | _BV(COM4A1);
  TCCR4B = _BV(CS40);
  // 38kHz carrier/timer
  OCR4C = (F_CPU/(IR_CLOCK_RATE*2L)-1);
  pinMode(pwmPin, OUTPUT);


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

zeroG

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

Bye!

AnalysIR

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

zeroG

#5
May 02, 2016, 12:22 am Last Edit: May 02, 2016, 12:24 am by zeroG
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() :

Code: [Select]


  #define pwmPin 10  
  #define frequency 38000

  void setup(){

      //initialize all timers except for 0, to save time keeping functions
      InitTimersSafe();

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

      pwmWrite(pwmPin, 128);
  }



Go Up