Pwm Frequency change

Hello
I am using pwm pins for driving laser diodes, for that I need to operate them on same frequency.i.e 490 Hz. How can I set timer 0 to this frequency value? I saw some prescaler values which are providing me values of 980 Hz for value 3, and 244 Hz for value 4. But I want to operate it on 490 Hz. How can I achieve this value on Timer 0 pins on Arduino mega 2560?
Thanks :slight_smile:

I don't know of a simple solution. You can use one timer to generate the frequency (using tone(pin,490)?), which already affects PWM generation on pins 3 and 11, but perhaps not in the intended way. Eventually route that frequency to another timer to create the PWM signal of the intended duty cycle. Or use "phase (and frequency) correct" PWM modes, with the compare registers set to the appropriate values.

Modifying the Timer 0 frequency will affect the timing of many other functions, including millis(). You better use one of the other timers.

I want to use all the timers in arduino mega2560 and set all of them at 490 Hz. but i am unable to set timer 0, 3, 4 to this value but timer 1 2 and 5 can be set to these values

Here is a good reference about pwm pins and values on the mega

Everything except timer0(pins 4,13) should be at 490 hz by default.

Are you certain that timers 3 and 4 are not at this value?

Changing timer 0 to 490 Hz is not so easy as changing a prescaler. You will need to change the mode from fast pwm to phase correct like the other timers. You will change millis() and micros(). If you absolutely need pwm at 490 on pins 4 and 13 you are leaving the standard arduino environment, and should refer to the data sheet.

I am also unable to achieve it through prescaler values on timer 3 and 4, I am getting value of 980Hz when I use pre scaler value 4 I get 250Hz I don't know how to achieve 490Hz on all. By default the value on timer 3 and 4 is also 980Hz and I have checked it on two arduino mega boards both showing the same..
Can any one tell me how to get this value of frequency?

The arudino environment sets up the timer defaults, and its easy enough to take a look at how the ide sets up the pwm output(prescalers, mode) on all the pins of the Mega. This will tell you how to get them all the same.

Here's some code which I put together for reading the relevant setup registers(TCCRnA,TCCRnB) of the three timers in the ATmega328. You should be able to extend it for the six timers and the pins of the mega. What do you see?

void setup() {

byte a;
byte b;
  
  //enable arduino ide pwm all pins
  analogWrite(6,127);//outputA timer0
  analogWrite(5,127);//outputB timer0
  analogWrite(9,127);//outputA timer1
  analogWrite(10,127);//outputB timer1
  analogWrite (11,127);//outputA timer2
  analogWrite(3,127);//outputB timer2
  
  
  Serial.begin(9600);
  Serial.print("Register");
  Serial.print("\t");
  Serial.println("Bit Values");
  Serial.println();
 
  a = TCCR0A;
  Serial.print("TCCR0A");
  printRegister(a);
  
  b = TCCR0B;
  Serial.print("TCCR0B");
  printRegister(b);
  
  a = TCCR1A;
  Serial.print("TCCR1A");
  printRegister(a);
  
  b = TCCR1B;
  Serial.print("TCCR1B");
  printRegister(b);
  
  a = TCCR2A;
  Serial.print("TCCR2A");
  printRegister(a);
  
  b = TCCR2B;
  Serial.print("TCCR2B");
  printRegister(b);
  
  
  }

void loop(){}

void printRegister(byte v)
{
  Serial.print("\t");
  Serial.print("\t");
  for (int i = 7; i > -1; i-- )
    {
      Serial.print((v >> i) & 0X01);//shift and select first bit
    }
    
    Serial.println();
}