top of page
Writer's pictureRAJ SHARMA

20Khz with Adjustable Duty Cycle Arduino Code

#include <TimerOne.h>

void setup() {

pinMode(9, OUTPUT); // Set pin 9 as an output

Timer1.initialize(50); // Initialize Timer1 with a 50 us period (20 kHz)

Timer1.pwm(9, 512); // Set PWM duty cycle to 50% on pin 9

}

void loop() {

// Do nothing, the signal is generated by Timer1

}




In this code, we use the Timer1 library to initialize Timer1 with a 50 us period (20 kHz) and set the PWM duty cycle to 50% on pin 9.

Note that both of these approaches require some knowledge of the Arduino's internal timers and PWM capabilities. Also, make sure to use a suitable resistor and capacitor to filter the signal if you're using it to drive a load, as the Arduino's output is not suitable for driving high-current loads directly.



0 views0 comments

Recent Posts

See All

Comments


bottom of page