2

Im new here. Anyone that could give a code or sample that would generate I wanted to use a frequency in range of 35k-43kHz. Please help me.

1
  • Sound a lot like an IR carrier wave.
    – Gerben
    Commented Sep 29, 2015 at 13:04

3 Answers 3

3

The code below generates 38 kHz and modulates its duty cycle.

// Example of modulating a 38 kHz frequency duty cycle by reading a potentiometer
// Author: Nick Gammon
// Date: 24 September 2012

const byte POTENTIOMETER = A0;
const byte LED = 10;  // Timer 1 "B" output: OC1B

// Clock frequency divided by 38 kHz frequency desired
const long timer1_OCR1A_Setting = F_CPU / 38000L;

void setup() 
 {
  pinMode (LED, OUTPUT);

  // set up Timer 1 - gives us 38.005 kHz 
  // Fast PWM top at OCR1A
  TCCR1A = bit (WGM10) | bit (WGM11) | bit (COM1B1); // fast PWM, clear OC1B on compare
  TCCR1B = bit (WGM12) | bit (WGM13) | bit (CS10);   // fast PWM, no prescaler
  OCR1A =  timer1_OCR1A_Setting - 1;                 // zero relative  
  }  // end of setup

void loop()
  {
  // alter Timer 1 duty cycle in accordance with pot reading
  OCR1B = (((long) (analogRead (POTENTIOMETER) + 1) * timer1_OCR1A_Setting) / 1024L) - 1;

  // do other stuff here
  }
3
  • Just to add; this code works for pin 10, and pin 10 alone.
    – Gerben
    Commented Sep 29, 2015 at 13:03
  • I connected the output to the ultrasonic transmitter but its not working. here is the transmitter specification that i used (farnell.com/datasheets/1498178.pdf) and the circuit diagram (google.com.ph/…)
    – user13535
    Commented Dec 19, 2015 at 11:46
  • Not working? How do you know? Are you getting 38 kHz out of the Arduino or not? You would need an oscilloscope to check.
    – Nick Gammon
    Commented Dec 21, 2015 at 6:03
1

use the 'tone' function: arduino tone function

example:

//tone(pinNumber, FrequencyInHertz);
tone(6, 35000); //generate a 35kHz tone on pin 6
delay(10);
tone(6, 43000); //generate a 43kHz tone on pin 6
4
  • Could you elaborate a bit? This is basically a link-only answer which would not be helpful if the link went down.
    – Nick Gammon
    Commented Sep 29, 2015 at 7:38
  • I wanted to generate a set of high frequency at the range of 25k-60k Hz using the arduino uno. Please help me. What function do i use? I'm a newbie on coding and if anyone would share some code it would be a great help. Im very thankful.
    – user13535
    Commented Sep 29, 2015 at 11:58
  • @user13535 You now say 25k-60kHz, but your question says 38k-43kHz. Which is it? Commented Sep 29, 2015 at 19:15
  • The 25k-60k Hz. I change the range.
    – user13535
    Commented Sep 30, 2015 at 10:16
-1

This is meant to be a comment, but I can't put (pseudo) code in a comment.

A 40 kHz square wave? Simple:

for(;;){
   wait ( 1 / 2 * 40.000 );
   make pin high;
   wait ( 1 / 2 * 40.000 );
   make pin low;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.