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.
3 Answers
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– Gerben09/29/2015 13:03:40Commented 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– user1353512/19/2015 11:46:17Commented 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.12/21/2015 06:03:14Commented 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
-
Could you elaborate a bit? This is basically a link-only answer which would not be helpful if the link went down.09/29/2015 07:38:40Commented 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– user1353509/29/2015 11:58:38Commented Sep 29, 2015 at 11:58
-
@user13535 You now say 25k-60kHz, but your question says 38k-43kHz. Which is it?CharlieHanson– CharlieHanson09/29/2015 19:15:14Commented Sep 29, 2015 at 19:15
-
The 25k-60k Hz. I change the range.user13535– user1353509/30/2015 10:16:32Commented 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;
}