Problem with the pinMode command

Hi there.I am new in the arduino world and since i am still learning how to program it,I have faced a problem that I was not able to solve. First of all, I have to mention that I am using arduino Uno, although i don't think that the problem has to do with a specific model of arduino.So my question is this;I can't understant in which cases I must set the pins that I am using as an INPUT and when as an OUTPUT in the pinMode command!!! :~ I hope that I didn't started a new topic unnecessary and please excuse me for the mistakes I may have done since English isn't my mother tongue...

orestman: Hi there.I am new in the arduino world and since i am still learning how to program it,I have faced a problem that I was not able to solve. First of all, I have to mention that I am using arduino Uno, although i don't think that the problem has to do with a specific model of arduino.So my question is this;I can't understant in which cases I must set the pins that I am using as an INPUT and when as an OUTPUT in the pinMode command!!! :~ I hope that I didn't started a new topic unnecessary and please excuse me for the mistakes I may have done since English isn't my mother tongue...

All I/O pins are set as input by default so you don't really need to set them as such, although it is considered a good practice to do so. You must set any digital pins to output mode if you wish to have the pins drive any external circuitry or device.

Lefty

Thank you very much for the quik reponse!!!So just to make it sure. It isn't neccessary to set something in the void setup as an input or an output(for example a led), and it won't hurt my arduino board, right??? So I can write this code for example;

int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

like this;

int led = 13;

void setup(){
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

and it will be the shame???i do not think so.I don't have to write anything to the void setup???just right it and leave it empty?

Use the first sketch with setup() setting the pin as o/p. The LED needs to have a series resistor with it, 220R for 5 volt operation.

There is an LED on the UNO board connected to pin 13. This LED is intended to be a "hello world" indicator that your first sketch works. I suspect the pin 13 is set as output in arduino core. I suspect both your programs will work the same but why not giving them a try.

Atmega328 Datasheet: The I/O ports of the AVR are immediately reset to their initial state when a reset source goes active. This does not require any clock source to be running

All pins are set to high impedance with pullups disabled during reset, so they are all inputs when your program starts running.

The reason the second works is that when you write an input pin high, you turn on an internal pullup resistor which will source some current to the LED. If you are using a rev3 Arduino, the onboard LED is driven by an Op-Amp which will amplify the current from the pullup and you will see the LED turn on brightly. On some boards the LED never turns off until you set the pin to be an output and Low, this is because the op-amp input is left floating when the pin is an input.

Specifically

It isn't neccessary to set something in the void setup as an input or an output(for example a led), and it won't hurt my arduino board, right???

Is wrong.

You always need to set pins to be outputs if you are using them as outputs. Yes it won't hurt your arduino but then it won't work either.

Yes it won't hurt

I like that.

Congrats on the new prince.

Grumpy_Mike: Specifically

It isn't neccessary to set something in the void setup as an input or an output(for example a led), and it won't hurt my arduino board, right???

Is wrong.

You always need to set pins to be outputs if you are using them as outputs. Yes it won't hurt your arduino but then it won't work either.

I got that but the thing that still can't understand is in wich cases I have to set any divice ,not only a LED ,as an input and when as an output?I mean with what criterion I set something as an INPUT and with what as an OUPUT???? I hope I didn't get annoying but I can't figure it out!!!!!

An OUTPUT sends a signal to an external device, such as an LED, that can respond to the signal that the Arduino sends

An INPUT reads a signal from an external device, such as a switch, so that the Arduino can take action based on what it reads

Think about it.
Can you output a signal to a switch to make it do something ? No.
Can you input a signal from an whether an LED to determine whether it is on or off ? N (or at least not normally)

What devices confuse you as to whether they they need to be connected to pins defined as input or output ?

Can you input a signal from an whether an LED to determine whether it is on or off ?

Can you what?

Let me try that again, this time in English instead of gibberish.

Can you input a signal from an LED to determine whether it is on or off ? No (or at least not normally)

UKHeliBob: An OUTPUT sends a signal to an external device, such as an LED, that can respond to the signal that the Arduino sends

An INPUT reads a signal from an external device, such as a switch, so that the Arduino can take action based on what it reads

Think about it. Can you output a signal to a switch to make it do something ? No. Can you input a signal from an whether an LED to determine whether it is on or off ? N (or at least not normally)

What devices confuse you as to whether they they need to be connected to pins defined as input or output ?

There isn't any specific device that confuse me but the whole logic of the pinMode command.But thanks to you I understood that!!!Thank you very much.Oh and something else :). If I want to put a potentiometer or a servo to my circuit,the potentiometer has to be defined as an INPUT and the servo has to be defined as an OUTPUT right???

If I want to put a potentiometer or a servo to my circuit,the potentiometer has to be defined as an INPUT and the servo has to be defined as an OUTPUT right???

No and no.

Potentiometers are connected to analog pins, which are INPUT only, by definition.

Servos are controlled by a library which manages the pin state.

No and no.

Potentiometers are connected to analog pins, which are INPUT only, by definition.

Servos are controlled by a library which manages the pin state. [/quote]

Yeah I forgot about the servo library.... But for example an LDR is going to be defined as an INPUT right???

But for example an LDR is going to be defined as an INPUT right???

Normally, an LDR is going to be connected as part of a potential divider, and will be connected to an analogue input, which, as has been discussed, is always going to be an input.

But for example an LDR is going to be defined as an INPUT right???

An LDR varies its resistance depending on the amount that falls on it. [u]L[/u]ight [u]D[/u]ependant [u]R[/u]esistor. Thus it is not a digital (on/off) device and would normally be connected to one of the analogue pins which, as has been pointed out, are always INPUT devices and hence do not need to be set up as such using the pinMode() command.

The value output by the LDR is read using analogRead(). You can connect an LDR to a digital pin defined as an input but you will have no control in your program as to the resistance value of the LDR which causes the input to change from HIGH to LOW and vice versa.