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.
asked Sep 28, 2015 at 20:40
-
Sound a lot like an IR carrier wave.Gerben– Gerben2015年09月29日 13:04:01 +00:00Commented Sep 29, 2015 at 13:04
3 Answers 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
}
answered Sep 29, 2015 at 7:43
-
Just to add; this code works for pin 10, and pin 10 alone.Gerben– Gerben2015年09月29日 13:03:40 +00:00Commented 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– user135352015年12月19日 11:46:17 +00:00Commented 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.2015年12月21日 06:03:14 +00:00Commented Dec 21, 2015 at 6:03
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
answered Sep 29, 2015 at 6:57
-
Could you elaborate a bit? This is basically a link-only answer which would not be helpful if the link went down.2015年09月29日 07:38:40 +00:00Commented 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– user135352015年09月29日 11:58:38 +00:00Commented Sep 29, 2015 at 11:58
-
@user13535 You now say 25k-60kHz, but your question says 38k-43kHz. Which is it?CharlieHanson– CharlieHanson2015年09月29日 19:15:14 +00:00Commented Sep 29, 2015 at 19:15
-
The 25k-60k Hz. I change the range.user13535– user135352015年09月30日 10:16:32 +00:00Commented Sep 30, 2015 at 10:16
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;
}
answered Sep 28, 2015 at 21:19
lang-cpp