# How make pwm frequency 50hz( pin num : 3, 5, 6, 9, 10)

help...I want to make pwm frequency 50hz( pin num : 3, 5, 6, 9, 10)

//

i need pwm output 50hz.

but i cant understand

so i need help..

please answer my question

50Hz is pretty slow. Use blink without delay

``````/* sketch to output 20KHz signal

run on regular Arduino
*/
byte pulseOut = 2;
unsigned long currentMicros;
unsigned long nextMicros;
unsigned long duration = 25UL; // flip every 50uS = 20KHz pulse
// increase for slower pulses
unsigned long elapsedMicros;

void setup(){

pinMode (pulseOut, OUTPUT);
digitalWrite(pulseOut, LOW);

}
void loop(){
currentMicros = micros();
elapsedMicros = currentMicros - nextMicros;
if (elapsedMicros >= duration){
nextMicros = nextMicros + duration;
PIND = PIND | 0b00000100; // toggle D2 by writing to Input register
}
}
``````

There's enough info in that sheet to do what you need. Im' pretty new to this, but the salient things seem to be:

• It's specific to arduino mega. I don't know how the different board differ (the whole point of the libraries is to hide that stuff from the programmer)
• The pmw register steps at the clock frequency divided by a power of 2
• the register steps from 0-255, but you can clip that
• you can set a value at which the modulation turns on and off

Now, if your chip is 16mz, that's 0x4e200 chip cycles per output cycle (16000000/50). let's divide this by 1024 to get 0x138. Now, this is greater than 255, whic is a problem. But the PWM thingy can be set to scan up and then down (phase-correct mode). So divide 0x138 by 2 to get 0x9c. Yay!

• set the cycle divider to 1024
• set the PWM to phase-correct mode
• use 0x9C (or possibly 0x9C-1, depending on how it works) as the upper bound
• use "Toggle OCnA on Compare Match" to give you a fixed 50% duty cycle.

You will have to fiddle a bit to get it right, but by my reading of the document you linked to, this will get you in the ballpark.

help...I want to make pwm frequency 50hz( pin num : 3, 5, 6, 9, 10)

You could do it in hardware on a Mega with Timers 2,3, and 4 but you need a close read of the data sheet to set them up. If you are on a UNO, pins 5 and 6 are on Timer 0 which is the system millis() timer, and I wouldn't recommend changing its prescaler unless you are sure of what you are doing and can sort out any unintended consequences.

I would recommend using Crossroad's approach of toggling the pins using a software timer with micros() rather than the hardware timers.