What does the 500 Hertz pulse-width modulation mean?

I'm using an Arduino pulse-width modulation output to act as a throttle signal to a golf-cart motor controller. As I understand it, the output looks like 500 Hertz, and may cause an audible buzz in the motor operation.

But I can't figure out what the 500 Hertz really means. Does the Arduino go through the 256-bit cycle 500 times a second?

No, a PWM signal always has a fixed frequency but a variable duty cycle. In the Ardunio the duty cycle can be varied from 0-100% = 0-255 count. A value of 128 would give you a 500hz signal with equal 1 millisecond high and low durations.

Lefty

It means that the output is a square wave with a frequency of 500 Hz, which means from one rising edge to the next rising edge is 20 milliseconds. The width of the on-time (how long it is high, 5V) is controlled by the value you pass to analogWrite().


The Aussie Shield: breakout all 28 pins to quick-connect terminals

is 20 milliseconds.

You sure of that? ;)

2 milliseconds!

A dust mite fell on my 0 key and depressed it at the most inopportune time.

-- The Quick Shield: breakout all 28 pins to quick-connect terminals

That all makes sense. Thanks for clarifying how that works. I did not know it was just one big signal clumped together.

Would there be any advantage to doing one's own pulse-width modulation using software, and doing a more sophisticated algorithm? Like for half voltage (in other words, for the value 128) doing every other bit high? Then perhaps one could use a low-pass filter with a higher threshold to smooth out the signal.

A dust mite fell on my 0 key and depressed it at the most inopportune time.

I usually put that stuff down to extreme Brownian motion events.

Would there be any advantage to doing one's own pulse-width modulation using software

No, probably not. Lowering the frequency by the method you describe would most likely make the noise more audible.

Daanii: That all makes sense. Thanks for clarifying how that works. I did not know it was just one big signal clumped together.

Would there be any advantage to doing one's own pulse-width modulation using software, and doing a more sophisticated algorithm? Like for half voltage (in other words, for the value 128) doing every other bit high? Then perhaps one could use a low-pass filter with a higher threshold to smooth out the signal.

Not sure at all what you mean. However the existing arduino PWM output, if passed through an external low pass filter (cap/resistor) will result in a smooth true analog output voltage ranging from 0-5vdc from a PWM value of 0-255.

Lefty

What I meant was this.

Say you wanted an analog value of 128. That's a duty cycle of 50%. Currently the Arduino pulse-width modulation does that by doing HIGH 128 times followed by LOW 128 times. That's a frequency of 500 Hertz. If you did it instead by doing HIGH LOW 128 times, that's a frequency, I think, of 64 kHz. That's above the human hearing range. Also, that higher frequency is easier to filter, so you lose less of your 0 to 5 Volts range.

That might help in my application. But mostly I'm just curious.

Daanii: What I meant was this.

Say you wanted an analog value of 128. That's a duty cycle of 50%. Currently the Arduino pulse-width modulation does that by doing HIGH 128 times followed by LOW 128 times. That's a frequency of 500 Hertz. If you did it instead by doing HIGH LOW 128 times, that's a frequency, I think, of 64 kHz. That's above the human hearing range. Also, that higher frequency is easier to filter, so you lose less of your 0 to 5 Volts range.

That might help in my application. But mostly I'm just curious.

Still not clear. A PWM output always results in just one low to high and one high to low transition regardless of the duty cycle value, it's just the location in the 2 millisecond 'window' for the single transistion that varies with the duty cycle value. Note that there are two 'corner cases' of value 0 and value 255 where the signal is a steady low or high for the whole 2 millisecond period.

Lefty

I don't know if this helps here, but it clarifies the idea of PWM. http://en.wikipedia.org/wiki/Pulse-width_modulation

Cheers, Kari

Daanii, I think you are confused as to what the number range 0 to 255 represents. The PWM frequency of 500HZ is the cycle frequency between logic values 0 and 1 that the arduino is outputing, where logic0=0volts and logic1=5volts. This frequency is constant irrespective of what the duty cycle is. Duty cycle may be considered as the percentage that the cycle is at logical value 1. The arduino uses the number range 0 to 255 to represent the duty cycle where 0= no output, 127= 50% at 0 and 50% at 1, 255=100% at 1 Duty cycle may be calculated as (numerical value*100%)/255 Trust this helps clarify jack

Thanks for the clarifications. I understand the Arduino pulse-width modulation better now.

My preference would be to have less granularity -- maybe 25 steps instead of 255 -- and a higher frequency than the Arduino hardware pulse-width modulation provides. Now understanding this better, I'll see if I can figure out a way to do that.

Currently the Arduino pulse-width modulation does that by doing HIGH 128 times followed by LOW 128 times. That's a frequency of 500 Hertz.

I'm puzzled as to how that adds up.

Anyway, as retrolefty said, the important thing is that if you pass this pulsing 500 Hz signal through a capacitor, the capacitor effectively "smooths out" the pulses into something you won't hear. Or I think he said that. I admit the last time I played with motors and PWM I burnt my finger. It got a bit hot.

Daanii: Thanks for the clarifications. I understand the Arduino pulse-width modulation better now.

My preference would be to have less granularity -- maybe 25 steps instead of 255 -- and a higher frequency than the Arduino hardware pulse-width modulation provides. Now understanding this better, I'll see if I can figure out a way to do that.

If you search the forum, you will find instructions how to change PWM frequency. There's a chance that you configure wrong timer, and serial communication and internal timing would fail, but that is something I need to go through myself too.

And of course, you can always do that 0-255 with 25 step increments, no problem, then the actual change in the speed of the motor, or what ever, shows more visibly.

You say you understand now the Arduino Pulse-width modulation? It is actually global thing not just Arduino. ;)

Cheers, Kari

[quote author=Nick Gammon link=topic=54258.msg390588#msg390588 date=1299477632] Anyway, as retrolefty said, the important thing is that if you pass this pulsing 500 Hz signal through a capacitor, the capacitor effectively "smooths out" the pulses into something you won't hear. [/quote]

I could use a resistor-capacitor low-pass filter. But with high enough resistance and capacitance to fliter a 500 Hertz signal, I will see a pretty significant voltage drop at the top end. The voltage will never get to 5 Volts even with a 100% duty cycle.

An inductor-capacitor filter would help with that. But that kind of filter has its own problems.

Daanii: [quote author=Nick Gammon link=topic=54258.msg390588#msg390588 date=1299477632] Anyway, as retrolefty said, the important thing is that if you pass this pulsing 500 Hz signal through a capacitor, the capacitor effectively "smooths out" the pulses into something you won't hear.

I could use a resistor-capacitor low-pass filter. But with high enough resistance and capacitance to fliter a 500 Hertz signal, I will see a pretty significant voltage drop at the top end. The voltage will never get to 5 Volts even with a 100% duty cycle.

An inductor-capacitor filter would help with that. But that kind of filter has its own problems.

[/quote]

Not true, as long as the impedence the filter is wired to is high enough you will be able to see 5 volts at the cap. Most wire the low pass to a unity gain opamp which will buffer the low pass to what the now a true analog voltage is wired to.

Lefty

GaryP: You say you understand now the Arduino Pulse-width modulation? It is actually global thing not just Arduino.

Yes, I know. I was just wondering why the Arduino has a low frequency (500 Hertz) combined with low granularity (0 - 255) in its pulse-width modulation. And why it has "on-off" modulation rather than "distributed" modulation. (See www.datadog.com/pwm_tutorial.pdf)

But I understand that better now. And I'm not complaining. The Arduino has limits. It can't do everything well. I'm just trying to see how I can do my application better. The comments to my post have helped me do that. Thanks.

retrolefty: Not true, as long as the impedence the filter is wired to is high enough you will be able to see 5 volts at the cap. Most wire the low pass to a unity gain opamp which will buffer the low pass to what the now a true analog voltage is wired to.

Lefty

You're right. I'm sure I could get to where I want to go. But what I'm thinking now will work best is a digital-to-analog converter, not an Arduino doing pulse-width modulation. Anyone know of any good DACs out there?

Daanii:

GaryP: You say you understand now the Arduino Pulse-width modulation? It is actually global thing not just Arduino.

Yes, I know. I was just wondering why the Arduino has a low frequency (500 Hertz) combined with low granularity (0 - 255) in its pulse-width modulation. And why it has "on-off" modulation rather than "distributed" modulation. (See www.datadog.com/pwm_tutorial.pdf)

But I understand that better now. And I'm not complaining. The Arduino has limits. It can't do everything well. I'm just trying to see how I can do my application better. The comments to my post have helped me do that. Thanks.

analogWrite() is just a function the arduino team added to the standard arduino library and/or core functions. They made various design decision based on what they thought would be useful and easy to use. There is nothing in the Arduino platform that prevents one from writing their own pwm functions that run at whatever frequency and resolution capabilities the AVR chip provides. An example is digitalRead() function. It uses a variable for the pin number which can be useful and the pin mapping lets the arduino pin numbers stay consitance even if the chips I/O ports and pins are different from say a 328 to 1280 chip. But there is nothing to prevent someone from using direct port access I/O to gain I/O speed at the expensive of giving up the 'abstrated pin numbering' and portability that the arduino I/O method allows.

I guess what I'm saying is anything you don't like about a predefined arduino function in the arduino platform can be 'corrected' to your satisfaction by writing your own function and not utilizing the arduino function in question. Your limits then are only those hardware limitations of the microprocessor.