Can anyone concisely explain PWM in terms of frequency (Hz), Duty Cycle, and the Arduino control values of 0-255?
I am trying to understand how to drive a device at 60Hz using the values of 0-255. I understand Arduino has a default PWM freq at 490Hz. Would I need to change the frequency and then use the values of 0-255. How do I ensure the signal I am sending stays at 60Hz or more (the exception being when the device is off) if I am trying to provide the lowest range of PWM. In other words, what does a value 1 at 60Hz mean? 60x/s?
I have looked at many basic tutorials, but no one seems to explain how Hz, Duty Cycles, and the Arduino values of 0-255 correlate.
I have looked at many basic tutorials, but no one seems to explain how Hz, Duty Cycles, and the Arduino values of 0-255 correlate.
Value 0-255 isn't related to other parameters. 255 simple max 8-bit value, as most of arduino Timers 8-bitters. You may set your own 16 or 24 bits PWM using 16-bit Timer (usially one per uCPU) or combining 16 + 8 = 24 two timers, but w/o analogWrite - need to use direct port manipulation of hardware registers. Put more details on your project, may be other solution to increase a resolution or not to use PWM at all.
I'm not 100% sure of this, but I think the frequency is the basic pulse rate. In other words, you get a rising edge at a rate of 1/f (every 16.7mS @ 60Hz) With a resolution of 255 and a PWM value of 1, the pulse width is 1/255th of the period. (And, the duty cycle is 1/255.)
what does a value 1 at 60Hz mean?
At time 0, you get a rising edge.
0.0655 milliseconds later (1/255 x 16.7) the pulse is "done" and you get a falling edge.
16.7mS after the 1st rising edge, you get another rising edge and the cycle repeats.
With a duty cycle of 50%, (PWM value of 127 or 128) you have a square wave (high half the time, and low half the time). The frequency is still 60Hz, so you still get a rising edge every 16.7mS, but the pulse is high for 8.33mS and low for 8.33 mS. (With a resolution of 255, you can't get an exact 50% duty cycle.)
I understand Arduino has a default PWM freq at 490Hz.
I believe that's kHz.
**[EDIT] ** Nevermind... Thanks Mike.
How do I ensure the signal I am sending stays at 60Hz or more
The duty cycle is controlled in 255 steps from the value you give the arduino's analogWrite function. So a value of 128 will represent a 50% duty cycle.
This is independent of frequency.
Due to the way the hardware works you can only have this wide a range at set frequencies. While you can change the PWM frequency to anything the range at which the duty cycle can be varied over is narrower than the 255 steps you have at special "spot" frequencies.
I am trying to understand how to drive a device at 60Hz using the values of 0-255
Basically you can't with the way the arduino's hardware timer based PWM works.
I understand Arduino has a default PWM freq at 490Hz.
I believe that's kHz.
Everyone, thanks for the replies. Some one, very obviously, replied that 490hz is above 60hz, thank you very much for your amazing ability of perception and math logic in determining 490>60...you are truly a genius!
The specific question was to relate set frequency (hz) to duty cycles and to the Arduino value of 0-255.
Grumpy Mike, you seemed to understand the what I was getting at, perhaps you can elaborate. If I want to run a device using PWM at 60Hz, do I need to change the default values of my Arduino board to 60Hz in order to do so? What will a value of 1 (in terms of 0-255) mean at 60Hz? It will be on 1/60 of each second?
I know this topic is old, but in case anyone comes across it via search, I made a video about this topic. Skip to 9:39 for the bit about arduino PWM: LED Dimming: PWM vs Current - YouTube
rcamp:
Everyone, thanks for the replies. Some one, very obviously, replied that 490hz is above 60hz, thank you very much for your amazing ability of perception and math logic in determining 490>60...you are truly a genius!
The specific question was to relate set frequency (hz) to duty cycles and to the Arduino value of 0-255.
Grumpy Mike, you seemed to understand the what I was getting at, perhaps you can elaborate. If I want to run a device using PWM at 60Hz, do I need to change the default values of my Arduino board to 60Hz in order to do so? What will a value of 1 (in terms of 0-255) mean at 60Hz? It will be on 1/60 of each second?
If you succeed at changing the PWM frequency from 490Hz to 60Hz, a digital duty cycle value of "1" will produce an "on" pulse that is 1/256 times 1/60 of a second long = 1/15360 second.