Go Down

Topic: 100KHZ PWM with 41.66% duty cycle (Read 3108 times) previous topic - next topic


I tried a code to obtain 100KHZ PWM output with 41.66% duty cycle by reading an analog value at A0 which is compared with a reference 5V value for obtaining PWM value.

Can some one help me with the code? Any help is very much appreciated.

here is my code.

 int Pwmpin=9;//Pwm Output Pin
  int Fchange= A0;//Frequency change through Potentiometer
  void setup()
  pinMode(Pwmpin, OUTPUT);//Pwm pin as Output
  TCCR1A=_BV(COM1A1)|_BV(COM1B1);//Non-inverted Mode
  TCCR1B=_BV(WGM13)|_BV(CS11);//Prescalar 8

  void loop(){
  float freq=0;
  float count=10000,countt=0,Pinput=0;

 ICR1=count;//variable Frequency
 freq= int(16000000/countt);
 Serial.print("Pwm Freq =");
 Pinput=analogRead(A0);//Read input value
 Serial.print("adc value is:");
 Serial.print("pwm value is:");


Aug 23, 2017, 08:54 pm Last Edit: Aug 23, 2017, 08:57 pm by _pepe_

Your question is not clear.

"The value of the PWM" is usually refering to the duty cycle, but your code suggests it is the frequency.

Moreover, you have already specified a frequency (100 kHz) and a duty cycle (41.66 %) for the signal.

So what really is "the value of the PWM" that the analog input is supposed to modify ? What parameters are fixed, or variable ?

Go Up