PWM question

Hi, I don't have an oscilloscope, so I was wondering, for those pins that 'are suitable for PWM', are there capacitors used on those pins to smooth out the output? And if so, if I turn on and off those pins quickly, using digitalWrite() will they not result in a square wave?

Thanks,

Adrian

are there capacitors used on those pins to smooth out the output?

No. If you want them you have to add them externally. http://www.thebox.myzen.co.uk/Tutorial/PWM.html

Either way you will get a square wave.

@GM: So those pins "Suitable for PWM" are just that they have built in code in the AVR library to handle it then?

@NG: I don't think so, the link given by GM shows what I was thinking of, using a filter to largely smooth out the the highly jagged form of the square wave.

Thanks all

So those pins "Suitable for PWM" are just that they have built in code in the AVR library to handle it then?

No it is that those pins are the ones connected in the hardware of the chip. There is nothing you can do to change this.

Grumpy_Mike:

So those pins "Suitable for PWM" are just that they have built in code in the AVR library to handle it then?

No it is that those pins are the ones connected in the hardware of the chip. There is nothing you can do to change this.

So, suitable for PWM just means that they contain code that is not accessible/modifiable and are built in? No AVR lib code required? Interesting...

that they contain code that is not accessible/modifiable and are built in?

It is not code it is hard wires. These are attached to the timers, two pins for each timer. See the 328 data sheet for the exact details.

adrian_h:
@NG: I don’t think so, the link given by GM shows what I was thinking of, using a filter to largely smooth out the the highly jagged form of the square wave.

What do you mean, “I don’t think so”? You think they are not a square wave?

This code (from my page about timers):

#include <TimerHelpers.h>

const byte timer0Input = 4;
const byte timer0OutputA = 6;
const byte timer0OutputB = 5;
  
void setup() {
   pinMode (timer0OutputA, OUTPUT); 
   pinMode (timer0OutputB, OUTPUT); 
   TIMSK0 = 0;  // no interrupts
   Timer0::setMode (5, Timer0::PRESCALE_1, Timer0::CLEAR_A_ON_COMPARE | Timer0::CLEAR_B_ON_COMPARE);
   OCR0A = 200;    // number of counts for a cycle
   OCR0B = 150;    // duty cycle within OCR0A
}  // end of setup

void loop() {}

Gives this output (a square wave):

There is nothing there about turning output on, waiting, turning it off again. It is simply setting parameters for the hardware timer, which outputs to specific pins.

@GM: So those pins “Suitable for PWM” are just that they have built in code in the AVR library to handle it then?

It’s not the library, it’s the hardware.

You would write your own which turned on and off output to any pin, but the code would have to sit there doing that, and nothing else. You might do it with timer interrupts, but if you are going to use timers anyway, you may as well let them do the outputting.

What I meant NG was that one can insert a filter circuit and possibly produce a more analogue output.