LED flickering by itself... hmm, why ?

Hi guys,
Finally got my arduino and began playing with it. :slight_smile:

Here's my small program. Basically if pin 0 ( output ) is connected with pin 2 ( input ), the LED at pin 13 should be on, else it should be off.

void setup() {                
  pinMode(13, OUTPUT); 
  pinMode(0, OUTPUT);
  pinMode(2, INPUT);
}

void loop() {
  
  digitalWrite(0, HIGH);
  
  if ( digitalRead(2) == HIGH )
  {
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }

}

When 0-2 are not connected, but I place my finger close to arduino or just plug a wire into pin 2 , the LED starts flickering. :astonished: any ideas why it could be happening?

Thanks!

Sounds like a floating input.

Sounds like a floating input.

And is case that makes you curious... this is not abnormal behavior. The presense of your finger is enough influence to cause the pin to change state. The is normally kept under control by holding the pin closer to one of the power rails (+5) or (GND) using a resistor... it doesn't have to be exact... but 10K ohms should be enough.

The human body has a number of electrical characteristics... including the ability to conduct AC currents (act like a lossy dielectric) which allows you to transmit noisy signals from your environment like a big antenna to your circuit. Those touch on/touch off lamp controls that have been around for years take advantage of this behavior.

Yes! Thanks, connected pin 2 to ground through a 220 ohm resistor - works great now! :slight_smile:

Pin 0 or 1 shouldn't be used anyway as it's used for Serial connection.

Yes! Thanks, connected pin 2 to ground through a 220 ohm resistor - works great now!

You'd be much better off with a resistor >1K

you dont need to use any extra resistor
you can use the internal pull up resistor

like that

void setup() {
pinMode(13, OUTPUT);
pinMode(0, OUTPUT);
pinMode(2, INPUT);
digitalWrite(2, HIGH);
}

the internal pull up i think it is 2K

you dont need to use any extra resistor
you can use the internal pull up resistor

Well you can but I normally find that simply confuses new users initially. Best to understand they need one with a physical resistor first.

the internal pull up i think it is 2K

Why "think" when the truth is out there?
You're an order of magnitude out; 20 - 50K is what the datasheet says.