Why setup isnt needed in this PWM example.

// Dim an LED using a PWM pin
int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10

void setup()
{
  // no setup needed
}

void loop()
{
   for (int i=0; i <= 255; i++){
      analogWrite(PWMpin, i);
      delay(10);
   } 
}

isnt the PWMpin supposed to setup as output pin: pinMode (PWMpin, OUTPUT); ?
Or what am i missing..?

isnt the PWMpin supposed to setup as output pin: pinMode (PWMpin, OUTPUT); ?

You can only call analogWrite() on certain pins. The fact that you have called it implies that the pin is supposed to be an output pin. Therefore, the analogWrite() function makes sure that it is. Therefore, you do not need to.

But can i use those pins for digitalRead() ?

But can i use those pins for digitalRead() ?

Certainly. You'll want to set them as INPUT then, using pinMode() in setup().

The digitalWrite() function does different things for INPUT pins vs. OUTPUT pins, so you must explicitly define the direction. The analogWrite() function does not (it only understands OUTPUT), so it is able to set the mode for you.

Okay, i undestand what happens if i introduce digitalWrite(pin, HIGH) for an output pin.
But what happens if i have the pin set as an INPUT and try to digitalwrite in it?

But what happens if i have the pin set as an INPUT and try to digitalwrite in it?

You'll turn the internal pullup resistor on or off.

PaulS:

But what happens if i have the pin set as an INPUT and try to digitalwrite in it?

You'll turn the internal pullup resistor on or off.

Ah okay.. i need to take a look on that. Thank you for the excellent answers!

houpsi:

PaulS:

But what happens if i have the pin set as an INPUT and try to digitalwrite in it?

You'll turn the internal pullup resistor on or off.

Ah okay.. i need to take a look on that. Thank you for the excellent answers!

Okay, so i get it that if i have a pin as an input pin and its pullup resistor is set on with digitalWrite(pin, HIGH), it is driven to 5 volts, right.
But why would i want that to happen. I have thought that input pins voltage is always 0 unless there is something to be "sensed" or read.

But why would i want that to happen.

One reason is that it makes connecting switches much easier. Turn on the pullup resistor, and the pin reads HIGH/5V. Connect the switch to the pin and ground. Then, when you press the switch, the pin is connected to ground, so it reads LOW/0V. Release the switch, and the pin is pulled back up to 5V.

PaulS:

But why would i want that to happen.

One reason is that it makes connecting switches much easier. Turn on the pullup resistor, and the pin reads HIGH/5V. Connect the switch to the pin and ground. Then, when you press the switch, the pin is connected to ground, so it reads LOW/0V. Release the switch, and the pin is pulled back up to 5V.

I was about to say that doesnt that short circuit the pin to ground (and therefore fry the chip). But theres the pullup resistor so no?

EDIT: frying the chip

The internal pullup resistor is ~20K ohms. It can be connected to ground without concern.

CrossRoads:
The internal pullup resistor is ~20K ohms. It can be connected to ground without concern.

Yeah, thats what i thought. Thanks!