PWM output question

Hi everyone,

New to forum and to Arduino, so sorry if this sounds obvious, and sorry again if this is in the wrong place.

I am curious about the PWM outputs on the Arduino. Is there a limitation to the switching speed on the non-PWM outputs? Or is it just a coding convenience to be able to write "analogWrite()".

Wouldn't this code do basically the same thing?

digitalWrite(LED, HIGH);
delayMicroseconds(TIME);
digitalWrite(LED, LOW);
delayMicroseconds(TIME);

I guess the only difference that i can see is that maybe analogWrite does not actually hold the program?

Thanks in advance!

Wouldn't this code do basically the same thing?

digitalWrite(LED, HIGH);
delayMicroseconds(TIME);
digitalWrite(LED, LOW);
delayMicroseconds(TIME);

Almost.
This would be similar to analogWrite(LED, 200):

while (true) {
digitalWrite(LED, HIGH);
delayMicroseconds(200*4);
digitalWrite(LED, LOW);
delayMicroseconds((255-200)*4);
}

So you are correct, the major different is, once set analogWrite runs forever, and doesn't take any time for the processor.
PWM is done by specific hardware inside the ATmega chip on the Arduino.

This is much more than a coding convenience. The microcontroller can have 6 independent PWM signals (more on a Mega) without using any software processing time.

HTH
GB

Great thanks!

So there is no limitation to the switching speed of the non-PWM pins? (except where it says that 3us is the lowest reliable speed of delayMicroseconds()?)

Thanks again!

Similar to analogWrite(LED,128);

So there is no limitation to the switching speed of the non-PWM pins?

It is limited by the speed that the processor can execute an instruction to switch a pin. That is 2 clock cycles, so 8MHz on an Arduino.
If you had the right instructions it could switch a pin on, then off in 4 cycles, so oscillate at 4MHz.

The minimum timing of delayMicroseconds is set by the number of instructions it takes to do a few things, not by the speed a pin can be set or cleared.

If you do pin manipulation 'by hand', in assembler, you could probably get around 2MHz for some pins (i.e. estimate 7 cycles of 16MHz).

If I wanted a repetitive square wave, though, I'd use the timers, which are behind the PWM signals. They are more flexible than just PWM.

HTH
GB

Thats what i was looking for!

Thanks everyone for the quick responses!!