Possibly defective board (random triggering of digital inputs)

Bought this Uno a couple months ago, just now trying it out. At first I tried it with MATLAB using the ArduinoIO package and I was seeing some strange behavior so I decided to test it with a proper sketch. I have the following sketch:

void setup()

void loop()
  if (digitalRead(12))

I have 4 LEDs hooked up to pins 4-7. Each is in series with a 100 ohm resistor since it is using power from the board. Anyway, the problem itself:

Simply touching bare metal to pin 12 triggers the LED strobing; I have changed pins and it exhibits the same behavior. All I can think is that the board is defective. Simply touching metal to a single pin should not trigger anything. It also seems to just randomly trigger...the LEDs will just start and stop on their own. But there is a clear correlation with touching the selected input pin with wire (wire not connected to anything else...)

This perfectly normal. You have left the pin floating and nearby energy is being coupled into the pin.

After: pinMode(12,INPUT); add: digitalWrite(12, HIGH);

that will turn on the internal pull-up resistor.

For videos on what is going on see this: http://www.cmiyc.com/tutorials/arduino-pull-ups/

As you said, that was it completely. Never thought that it would be so sensitive like that.

So, to be clear, I should only see this behavior with things like switches, right? That is to say, I will be using my Uno with an optical sensor. I will make sure and invert the output from the sensor twice so that it's clearly either 0 or 5 volts, but that nearby energy isn't going to make the input fire incorrectly, will it?

Also, does this explain why I was seeing values of approximately 200-300 when reading values from the analog inputs in MATLAB?

Yes regarding analog in. You are making the false assumption (which most people do) that when nothing is connected, you should see 0V. The problem is that the real world is analog, not digital. Unless an input pin is pulled in a direction it is easily influenced.

What is an "optical sensor?"

Well for argument’s sake, let’s just say any sensor that’s supposed to return high or low. If it’s hooked up directly to something that either feeds it around 5 volts or low (<1.5? volts), it’s not going to read high on accident, correct? This gotcha only occurs when used with switches and no contact whatsoever, right?

I was cautious to give a blanket response to "optical sensor" because there are different types of sensors out there. So if you had a specific example in mind, it is better to address that one.

Epyon: This gotcha only occurs when used with switches and no contact whatsoever, right?

Yes, this is true.

However, if a sensor features "open-collector" outputs, these work by effectively leaving their output floating. This allows them to be "pulled-up" to whatever voltage you choose.

An open-collector output would work like a switch with no contact.

The problem is called a "floating" input. Unless you tie it to either +5 or ground through a resistor it is somewhere in the middle. Depends on what signals are in the environment of that pin. Think things like florescent light noise, radio frequency noise (cell phones, wireless devices...) al can put a strong enough random field at an unconnected pin for it to read constantly changing values.

The only time it won't work is when you want that condition to exist, it will then stay at either 0 or 5V...

Thanks everyone for your help/info.