Code for generating 40khz pulse wave arduino uno

Hello everybody

I am working on a project and would like some help. I am looking for Arduino uno code to generate a 40 KHZ pulse for 8 cycles and delay of 50 ms to drive my ultrasonic transmitter. At the moment I am only using the function generator in labs to achieve this.

thanks

So that would be 8 cycles on and 2000 cycles off would it?

This code pulses pin 3 for 8 cycles before turning it off.

/* Code to pulse pin 3 with a modulated signal
* Can be used to drive an IR LED to keep a TSOP IR reciever happy
* This allows you to use a modulated reciever and a continious beam detector
* By Mike Cook Nov 2011 - Released under the Open Source licence
*/
 volatile byte pulse = 0;

ISR(TIMER2_COMPB_vect){  // Interrupt service routine to pulse the modulated pin 3
    pulse++;
  if(pulse >= 8) { // change number for number of modulation cycles in a pulse
    pulse =0;
    TCCR2A ^= _BV(COM2B1); // toggle pin 3 enable, turning the pin on and off
  }
}

void setIrModOutput(){  // sets pin 3 going at the IR modulation rate
  pinMode(3, OUTPUT);
  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Just enable output on Pin 3 and disable it on Pin 11
  TCCR2B = _BV(WGM22) | _BV(CS22);
  OCR2A = 51; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 62 = 32 KHz
  OCR2B = 26;  // deines the duty cycle - Half the OCR2A value for 50%
  TCCR2B = TCCR2B & 0b00111000 | 0x2; // select a prescale value of 8:1 of the system clock
}

void setup(){
  setIrModOutput();
  TIMSK2 = _BV(OCIE2B); // Output Compare Match B Interrupt Enable
}

void loop(){
// do something here
}

I think it is simple enough to modify this to carry on counting another 2000 cycles before turning it back on again.

Thanks for the reply, I tried to upload and run the program but is showing me like error message (stray error)

but is showing me like error message (stray error)

Please paste the error message. But it sounds like you have not copied it correctly and have an unprintable character.

Click on select, then copy, then go into a new file on the IDE, highlight it all and paste the code in.

The IRremote library contains such code for various controllers and timers.

hi, linolo, did you get this figured out?
am I correct if this line changes the cycle?

TCCR2B = TCCR2B & 0b00111000 | 0x2; // select a prescale value of 8:1 of the system clock

thanks

It changes the prescaler to 1:8. Fine adjustment happens with OCR2A, which contains the number of (prescaled) clock ticks per PWM cycle.

hi, thanks doctor.

if I change OCR2A, wouldn't I need to change the prescaler, since the frequency will change?

Both the prescaler and the compare value affect the frequency. Typically the prescaler is used for raw timing, the OCR for fine tuning the frequency. The difference becomes more obvious with the duty cycle, that can range from 0 (0%) to OCR2A (100%).

well, I modified the code above to be like this, I feel like it's working. thanks.

it's based on code I got from this link generating 8 40KHz pulses - Arduino Due - Arduino Forum

volatile byte pulse = 0;

void setIrModOutput(){  // sets pin 3 going at the IR modulation rate
  pinMode(3, OUTPUT);
  //fungsi penghasil frek 40khz
  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Just enable output on Pin 3 and disable it on Pin 11
  TCCR2B = _BV(WGM22) | _BV(CS22); //64 prescaler ?
  OCR2A = 0; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 
  OCR2B = 0;  // defines the duty cycle - Half the OCR2A value for 50%
  //end of fungsi penghasil frek 40khz
  TCCR2B =   TCCR2B & 0b00111000 | 0x2;   ;// select a prescale value of 8:1 of the system clock  ganti and jadi xor
}

void setup(){
 setIrModOutput();
 pinMode(7,INPUT);
 pinMode(13, OUTPUT);
 
}

void loop(){
TIMSK2 = _BV(OCIE2B);
delay(10);
}


ISR(TIMER2_COMPB_vect) {
    if (pulse == 0)
    {
      pulse++;
      OCR2B = 1050;  
      OCR2A = 2100;
    }
    else if (pulse < 8)                    // Have we counted up to 8?
    {
      pulse++;                        // Increment the counter and continue     
    }
    else
    {
     pulse = 0;                      // Reset the counter
     OCR2B = 0;             // Set the PWM duty cycle to 0%     
     TIMSK2= 0;
     OCR2A = 0; 
    }
  }