What's wrong with this pot-pwm code?

Hi,
I am testing the pot controlled pwm output, only the duty cycle changed when vary the potcyc, the frequency doesn't change when very the potfre, why?
Thanks
Adam

const int pwmPin = 3; // assigns pin 12 to variable pwm

const int potcyc = A0; // assigns analog pot input A0 to vary the cyc of pwm
const int potfre = A1; // assigns analog pot input A1 to vary the cfrequency of pwm
int c1 = 0;   // declares variable c1
int c2 = 0;   // declares variable c2

void setup()  // setup loop
{
  pinMode(pwmPin, OUTPUT);
  pinMode(potcyc, INPUT);
  pinMode(potfre, INPUT);
}

void loop()
{
  c2 = analogRead(potcyc);
  c1 = 1024 - c2;      // subtracts c2 from 1000 ans saves the result in c1
  digitalWrite(pwmPin, HIGH);
  delayMicroseconds(c1);
  digitalWrite(pwmPin, LOW);
  delayMicroseconds(c2);

  int value = analogRead(potfre);
  value = map(value, 0, 1023, 0, 255);
  analogWrite(pwmPin, value);
  delayMicroseconds(40);

}

analogWrite() simply changes the duty-cycle. there's no need to change the frequency.

Thanks.
Is that possible to change the frequency by a pot?

yes. do a google search

You are trying to do software PWM and hardware PWM on the same pin. Hardware PWM won't let you change pitch so you should try all-software PWM like this:

const int pwmPin = 3; // assigns pin 12 to variable pwm

const int potcyc = A0; // assigns analog pot input A0 to vary the cyc of pwm
const int potfre = A1; // assigns analog pot input A1 to vary the cfrequency of pwm


void setup()  // setup loop
{
  pinMode(pwmPin, OUTPUT);
  // Don't use pinMode() with analogRead()
}

void loop()
{
  float dutyCycleVal = analogRead(potcyc) / 1024.0;
  int delayVal = map(analogRead(potfre), 0, 1024, 2000, 0); // delay in 

  digitalWrite(pwmPin, HIGH);
  delayMicroseconds(delayVal * dutyCycleVal); // The ON part of the cycle
  digitalWrite(pwmPin, LOW);
  delayMicroseconds(delayVal * (1.0 - dutyCycleVal));  // The OFF part of the cycle
}
1 Like

Why in earth would you want to do this?
I suspect you don’t understand how PWM works.

The simplest way to change the speed is to use the prescaller values, that gives corse control of frequency. For fine control of frequency you also need to change the threshold count register inside the timers that generate the PWM, but that means you can not have full control over the duty cycle and hence the speed of your motor.

A simple way to change the speed of PWM with a pot is to use the Tone library, if that is what you want to do.

i had a need of 1Hz blinking LED.
i found this:

void setup() {
    DDRB|= 2 ; // Set digital pin 9 (PB1) to an output ;
    TCCR1A = _BV(COM1A1) | _BV(WGM11); // Enable the PWM output OC1A on digital pins 9
    TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12); // Set fast PWM and prescaler of 256 on timer 1
    ICR1 = 62499; // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
    OCR1A = 625; // Set the duty-cycle to 10%: 62499 / 10 = 6249
}

i think you can change Registers according to analogRead value

why are you using PWM to blink an LED?

set and forget. at no cost of CPU time

Thanks.
I am not use it for speed control.
I am use it to control a capacitor ON/OFF.

Thank you.
I'll test that out.

Thank you.
Is the hardware PWM's frequency fixed or variable? mine measured 433.233 Hz

The Arduino Paltform provides analogWrite(arg1, arg2) function to create known frequency PWM signals at the following DPins (Fig-1) of Arduino UNO. arg1 of the function refers to the DPin and arg2 refers to the duty cycle.

Figure-1:
pwm328x

And what does that mean? A capacitor can not be on or off?

I have already explained this in reply #6. It is variable but in coarse steps.

Hi,
Can you please tell us your application?
What are you trying to do/control with your project?

The more we know about your project, the better information we can give to help you.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hi,
Sorry the words, it is used in a capacitor circuit, used mosfet as switch, and I like to use a pwm control the mosfet's ON/OFF.
Thanks

Hi,

Can you post a circuit diagram please?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

What Freq. are you trying to get up to? What are you running this on?

-jim lee

Thanks.
It is spot welder circuit as the picture.
yuanlitu - Copy

Thanks.
The planned F : 1K - 5KHz.