INPUT troubles

hello to all. i want to start by saying that i am a bigginner in electronics and i just bought my first arduino. i started by doing various tutorials found on the web and i got stuck.

the code in the arduino is:

/*
 * Switch test program
 */

int switchPin = 2;              // Switch connected to digital pin 2

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  pinMode(switchPin, INPUT);    // sets the digital pin as input to read switch
}


void loop()                     // run over and over again
{
  Serial.print("Read switch input: ");
  Serial.println(digitalRead(switchPin));    // Read the pin and display the value
  delay(100);
}

when i view the serial monitor on my computer, it changes the "Read switch input:" from 1 to 0 without me even touching the board! (no wires connected, just an arduino connected via usb cable)

output from serial monitor.

Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 1 Read switch input: 1 Read switch input: 1 Read switch input: 1 Read switch input: 1 Read switch input: 1 Read switch input: 1 Read switch input: 1 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0 Read switch input: 0

the arduino is an Duemilanove and it's placed on a rubber glove (i didn't have any rubber pads to put it on) cause the first thing that came into my mind was static electricity.

is my arduino faulty or i am doing something wrong?

Looks like you don't have a pull-up or pull-down resistor attached.

http://en.wikipedia.org/wiki/Pull-down_resistor

The term used for this situation is "floating pin". :)

In case the wikipedia page is just gibberish to you (been there :P), a pull up resistor is basically a strong resistor attached between the side of the switch the pin reads from and the Arduino's Ground. That way, if the switch is unpressed, The digital pin will read ground through the pull up resistor, but if it is pressed, it will read 5v coming through the switch (assuming the other side is hooked into 5V).

This helps to not read the random variations you get with an unconnected pin.

EDIT: Others below me also pointed out that you can rewire a little and use the internal resistors. That will work too, and will involve fewer components :).

is my arduino faulty or i am doing something wrong?

What you have is called a "floating" input pin condition. You are most likely wiring one side of the switch to +5vdc and the other to the input pin. Without a external pull-down resistor wired from the pin to ground the input does not see a valid voltage when the switch is not pressed, but rather floating and just seeing noise voltages.

The easiest solution is to change the logic a little and wire the other end of the switch to ground instead of +5vdc. Then in your sketch you can activate the internal pull-up for that input pin. So you will read a logic one or HIGH when the switch is not pressed and a logic 0 or LOW when you do press the switch.

 //  Switch test program


int switchPin = 2;              // Switch connected to digital pin 2

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  pinMode(switchPin, INPUT);    // sets the digital pin as input to read switch
  digitalWrite(switchPin, HIGH);  // Turn on the internal pull-up resistor
}


void loop()                     // run over and over again
{
  Serial.print("Read switch input: ");
  Serial.println(digitalRead(switchPin));    // Read the pin and display the value
  delay(100);
}

Hope that helps.

Lefty

it changes the "Read switch input:" from 1 to 0 without me even touching the board! (no wires connected

It will do your inputs are floating. Either use a pull up resistor or enable the internal ones with a digitalWrite(pin,HIGH) to your input pins.

EDIT A triple post is that a record?

thanks for your answers, now it works fine :)