I have problem. I want to generate frequency 165kHz using my Arduino Mega. But, when I check the freq using oscialloscope the freq generate by arduino is not up to my desired freq. The freq generate 4550Hz. Do you guys have the same problem with me? Here is my coding. Sorry because I am beginner in this field.
// PWM f= 165kHz +-10% duty cycle
int LEDpin = 5;
int level = 25.5;
void setup() {
pinMode (LEDpin,OUTPUT);// put your setup code here, to run once:
}
void loop() {
analogWrite (LEDpin,level);
delay (1000);
analogWrite (LEDpin,0);
delay (1000);// put your main code here, to run repeatedly:
Your code does not show you doing anything to set the frequency, so it's using the default frequency, which is much lower than 165 khz.
If you need 165 khz, you need to set prescaler to 1, and set the timer to count up to 99ish if it's a 16mhz board (eg, TOP of 99). See the datasheet for details on configuring the timers manually.
Pin 5 is output from timer 3. The default pwm frequency from that timer is 490Hz and your sketch does not appear to change the timer from its default settings.