How to turn off the pin that was kick started by analogWrite ?

This is from the official Arduino Reference:
"You do not need to call pinMode() to set the pin as an output before calling analogWrite()."

When I first read this, I was like, ok, no big deal, pinMode is just one line of code.

But then in my mind I have a problem:

Now to turn a digital output pin on and off is easy.

pinMode(Pin_Number, OUTPUT);
digitalWrite(Pin_Number, HIGH);

to turn it off, all you need is:
digitalWrite(Pin_Number, LOW);

That makes sense.

But what about PWM analog output ?
If I straight away do:
analogWrite(Pin_Number, 128);

How do I turn it off ?
Do I do:
digitalWrite(Pin_Number, LOW); ?

This doesn't make any sense since I have not call pinMode in the first place because analogWrite doesn't require the pinMode initializer.

So should I set pinMode for pins that I want to analogWrite to just so that I can use digitalWrite(Pin_Number, LOW) later ?

That's rather illogical.

I can also do analogWrite(Pin_Number,0) but that is just stupid.

Please help.

I can also do analogWrite(Pin_Number,0) but that is just stupid.

Why this is stupid??
i think this is the only way..

It's stupid because the PWM timer is being wasted as it is being use to regulate the pulsation of 0 volts at 490 hz.
It's a waste of the internal oscillator.

I want to turn that pin off period.

It's stupid because the PWM timer is being wasted as it is being use to regulate the pulsation of 0 volts at 490 hz.

Have you looked at the source code? Just maybe the code is smarter than you.

It's a waste of the internal oscillator.

No it's not because it isn't doing anything or taking up any resource.

"You do not need to call pinMode() to set the pin as an output before calling analogWrite()."

There is nothing to say you can't do it. In fact what happens is a pin's direction (input or output) is set by a bit in a data direction register. Part of the analogWrite() function sets that bit, in other words it in effect does a pinMode() for you, so once you have used analogWrite() you can use digitalWrite() as well.

Grumpy_Mike:
what happens is a pin's direction (input or output) is set by a bit in a data direction register. Part of the analogWrite() function sets that bit, in other words it in effect does a pinMode() for you, so once you have used analogWrite() you can use digitalWrite() as well.

I see....I See!

So, the only reason, one don't have to set pinMode is because analogWrite does it for you automatically....

I doesn't suppose I should ask why it doesn't do that automatically when one calls digitalWrite(Pin_Number, HIGH), you would think the computer would know that "ofcourse you want it to be an output since you set it to high".

Regardless of that inconsistency, that means I can call digitalWrite(Pin_Number, LOW) after a analogWrite(Pin_Number, 128) then since analogWrite already set pinMode to OUTPUT.

Thanks :smiley:

Still this internal favoritism is irregular and inconsistent.
A programming language should not behave like that, either they should all need the pinMode initializer, or they should have the ability to auto pinMode when a digitalWrite(XXX, OUTPUT) or analogWrite(XXX,255) is called.

Look at the other use of digitalWrite if you're wondering why digitalWrite needs a pinMode

A programming language should not behave like that, either they should all need the pinMode initializer,

You are wrong.
Are you saying that the analogWrite should refuse to work correctly if it's pin has not initialised?

As AWOL says there are other uses of digitalWrite() that are needed when the pin is an input.

There is no need to be outraged anyway, there isn't a computer language written without inconsistency or lack of orthogonality, I suspect you are just peeved that you made a mistake.

You are right :slight_smile: