• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Manual PWM?

HR19

Member
Ok, so I have an Arduino Uno. It has 490Hz PWM on some pins and 980Hz PWM on some other pins. I've seen libraries allowing for PWM over 32kHz. The chip operates at 16MHz. Here's my question: why can't I manually program a function for this for reasonably high frequency? There's a delayMicroseconds function, so why can't I have a PWM signal reasonably close to that with a simple function, of extremely high resolution? I understand that there would be a few clock cycles, potentially a few for each line. Why do I need a complex library with timing clocks instead of a few lines of code?
 

Pommie

Well-Known Member
Most Helpful Member
The Atmel uC used on arduino boards is very capable of doing much faster PWM and the ATMEGA328 has 6 PWM modules. There are some libraries that allow MUCH faster PWM on the Arduino.

Mike.
 

HR19

Member
The Atmel uC used on arduino boards is very capable of doing much faster PWM and the ATMEGA328 has 6 PWM modules. There are some libraries that allow MUCH faster PWM on the Arduino.

Mike.
Yes, I mentioned that I've seen some of those libraries. But why do I have to use a complex library? The ones I've seen mention having to use some complex code in the background about the timing clocks and other hardware. Why can't I just manually program a little function to make the pin HIGH and LOW and wait a few milliseconds based on desired duty cycle?
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Yes, I mentioned that I've seen some of those libraries. But why do I have to use a complex library? The ones I've seen mention having to use some complex code in the background about the timing clocks and other hardware. Why can't I just manually program a little function to make the pin HIGH and LOW and wait a few milliseconds based on desired duty cycle?
You're continually wanting low jitter - that's what the hardware is for - doing it manually will use up lot's of processor cycles and cause jitter.
 

Pommie

Well-Known Member
Most Helpful Member
The Arduino core code does exactly what you describe which is why it's so slow. The libraries setup the hardware to do it.

Mike.
 

HR19

Member
You're continually wanting low jitter - that's what the hardware is for - doing it manually will use up lot's of processor cycles and cause jitter.
Ok, that's what I was curious about. I get how it uses up processor cycles (presumably would be less efficient as a result, although I doubt is enough to really to me). But how would it cause jitter? I wish I had an oscilloscope so I could just test it and see what I get.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Ok, that's what I was curious about. I get how it uses up processor cycles (presumably would be less efficient as a result, although I doubt is enough to really to me). But how would it cause jitter? I wish I had an oscilloscope so I could just test it and see what I get.
It causes jitter because other things are happening, such as interrupts - and any time spent in an ISR adds to the PWM time.

As in your other thread, now you've mentioned you aren't doing any filtering at all, that explains all your 'problems' - adding suitable filtering should make a HUGE difference (as you're wanting DC, not PWM), and may well do all you need.
 

Latest threads

EE World Online Articles

Loading
Top