Can somebody help me to create a 38 khz waveform with timer 2

I would really appreciate your help i need it know and i'm desperate because i don't know what to do

tone(thePin, 38000);

Here is one that uses Timer 1

Maybe research into MsTimer2.h if you need to use Timer 2.

#include <TimerOne.h>
 
void setup() 
{
  // Initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards
  pinMode(13, OUTPUT);    
  
  Timer1.initialize(13); 
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here
}
 
void loop()
{

}
 
void timerIsr()
{
    PINB |= _BV(PINB5);   // Toggle digital pin D13
}

gardner:

tone(thePin, 38000);

Confirmed with my scope:

void setup ()
  {
  tone(3, 38000);
  }  // end of setup

void loop () { }

Boy, that was easy!

Please add to "How to use this forum"

READ THE OP

http://playground.arduino.cc/Main/MsTimer2

READ THE OP

OP was given alternatives in case they were not aware them.
They were directed to research into MsTimer2.h

If the OP doesn't want to read replies they don't have to.

Vaclav:
READ THE OP

What are you getting at? The tone library uses Timer 2.

Besides, this isn't a programming question it is a Project Guidance question.

gardner:
http://arduino.cc/en/Reference/Tone

tone(thePin, 38000);

I'm wondering if the OP didn't mean a38k carrier for (say) an IR signal. Is it possible to use that tone with a message? I suppose it could be sent in bursts.

Without using timers at all*, adafruit have simple code here which has a function pulseIR(long microsecs). It sends a square 38k wave (just digitalWrites of high and low using delayMicroseconds) for a burst of microsecs, thus sending the message using 38k as the carrier.

  • or does delayMicroseconds use a timer under the hood?

I've verified the waveform on a 'scope.

edit:

In this part of the adafruit code:

// This procedure sends a 38KHz pulse to the IRledPin 
// for a certain # of microseconds. We'll use this whenever we need to send codes
void pulseIR(long microsecs) {
  // we'll count down from the number of microseconds we are told to wait
 
  cli();  // this turns off any background interrupts
 
  while (microsecs > 0) {
    // 38 kHz is about 13 microseconds high and 13 microseconds low
   digitalWrite(IRledPin, HIGH);  // this takes about 3 microseconds to happen
   delayMicroseconds(10);         // hang out for 10 microseconds, you can also change this to 9 if its not working
   digitalWrite(IRledPin, LOW);   // this also takes about 3 microseconds
   delayMicroseconds(10);         // hang out for 10 microseconds, you can also change this to 9 if its not working
 
   // so 26 microseconds altogether
   microsecs -= 26;
  }
 
  sei();  // this turns them back on
}

.... I guess the digitalWrites to give the wave could just be replaced with tone(), thusly:

// This procedure sends a 38KHz pulse to the IRledPin 
// for a certain # of microseconds. We'll use this whenever we need to send codes
void pulseIR(long microsecs) {
  // we'll count down from the number of microseconds we are told to wait
 
  cli();  // this turns off any background interrupts
 
  while (microsecs > 0) {
     tone(IRledPin, 38000);
    // 38 kHz is about 13 microseconds high and 13 microseconds low
   // so 26 microseconds altogether
   microsecs -= 26;
  }
 
  sei();  // this turns them back on
}

JimboZA:

  • or does delayMicroseconds use a timer under the hood?

No.

I have stuff about timers and 38 kHz here:

However the original post was so devoid of detail (except that the OP is desperate) that it is hard to know what it is required for.

@electman: The better the question, the better the answer.

JimboZA:
I'm wondering

Me too. My mindreading is not that great.