Change PWM Arduino nano Frequency

Hello all,

Here is the thing:
I use the code below to switich between two different cases in the If statement, based on the value I am sending thru the serial.

The issue is that, when I set the PWMs to 30Hz and 60Hz the serial is freezing for a few seconds - works quite slow.

If I use the default PWM settings, no issues (or I cannot notice them). Everything works fine then!

Do you see any issues with the code or is ir normal to wait such amount of time in case PWMs Freq are set to 30 or 60hz?

Thanks!

void setup() {
  Serial.begin(115200);

  pinMode(measurePin, INPUT);

  pinMode(SwitchZCD1, OUTPUT);
  pinMode(SwitchZCD2, OUTPUT);
  pinMode(SwitchZCD3, OUTPUT);
  pinMode(SwitchIGBT, OUTPUT);

  //Pins D5 and D6 - 7.8 kHz
// TCCR0B = 0b00000010;  // x8
  //TCCR0A = 0b00000011;  // fast pwm
//  TCCR0B =TCCR0B & B11111000 | B00000101;//60HZ
// TCCR0A = 0b00000011;  // fast pwm
TCCR0B = TCCR0B & B11111000 | B00000101;    // set timer 0 divisor to  1024 for PWM frequency of    61.04 Hz
TCCR1B = TCCR1B & B11111000 | B00000101;    // set timer 1 divisor to  1024 for PWM frequency of    30.64 Hz
 TCCR2B = TCCR2B & B11111000 | B00000111;    // set timer 2 divisor to  1024 for PWM frequency of    30.64 Hz
  analogWrite(SwitchZCD1, 0);
  analogWrite(SwitchZCD2, 0);
  analogWrite(SwitchZCD3, 0);
  analogWrite(SwitchIGBT, 0);
}

void loop() {


  if (Serial.available() > 0) { // Четене от серийния порт и задаване на нова стойност на границата за регулиране във Volt.
    // read the incoming byte:
    String red = Serial.readString();
    Urefmin = red.toInt();
  }

if(Urefmin == 1){
  pwm_IGBT =22;
      analogWrite(SwitchZCD1, 150);
      analogWrite(SwitchZCD2, 0);
      analogWrite(SwitchZCD3, 0);
      analogWrite(SwitchIGBT, pwm_IGBT);
}else {
    pwm_IGBT =0;
      analogWrite(SwitchZCD1, 0);
      analogWrite(SwitchZCD2, 0);
      analogWrite(SwitchZCD3, 0);
      analogWrite(SwitchIGBT, pwm_IGBT);
}
  //Serial.print("Uin =");
  //  Serial.print("PWM =");
  Serial.println(pwm_IGBT);
  //  Serial.println("  ");

  //delay(5);

}

Have you checked which timer is used for the serial I/O?

What, after all is said and done, are you trying to do here? Explain what you're creating in HUMAN language. Not in code.

No?!
Could the solution be that I am slowing down the timer, that is also being used for serial monitor?

I am checking........

Thanks!

Whell, I am trying to create low frequency for the PWM of one of the digital pins, witn hardwarware PWMs. It should be 30Hz or 60Hz, depending on other factors.

But, in this simple code, I want to send 1 or 0 from the serial, and depending on that, to see how these switchings are affecting my schematic.

Firstly I want send 1 and then in the first IF statement I wll be getting some switches driven by the PWMs set.

and that is all, just toggling between two configurations of switches.

BUT, the problem is that when I decrease the frequency of the PWM, then the Serial monitor gets extremely slow, and the reactiong of the entire system!

@maxpower1919
How many 30Hz PWM signals do you need? In your code I see a four signal - is it means that you need only them?
If so, do not use PWM on Nano pins 5 & 6 - these pins controlled by Timer0 which affected the Serial and millis function

How about an adjustable square wave generator?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.