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.

migrated from electronics.stackexchange.com Sep 28 '15 at 21:35

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • Sound a lot like an IR carrier wave. – Gerben Sep 29 '15 at 13:04
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
  }
  • Just to add; this code works for pin 10, and pin 10 alone. – Gerben Sep 29 '15 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 Dec 19 '15 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 Dec 21 '15 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
  • Could you elaborate a bit? This is basically a link-only answer which would not be helpful if the link went down. – Nick Gammon Sep 29 '15 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 Sep 29 '15 at 11:58
  • @user13535 You now say 25k-60kHz, but your question says 38k-43kHz. Which is it? – CharlieHanson Sep 29 '15 at 19:15
  • The 25k-60k Hz. I change the range. – user13535 Sep 30 '15 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, privacy policy and cookie policy

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