Arduino Pin Voltage Detection Problems

I was working on my own Arduino project and I was setting up a very simple switch. IE: 5V > switch > Pin; controlling Pin 13 (Built in LED). To start off, I didn't even have a switch, I was manually connecting the wires. The system worked fine, the LED lit up when I combined the wires. The only thing is, when I fidgeted the wire attached to the pin w/ the switch, the built in LED would light up most of the time, almost like there was a problem with that pin, but I connected it with many other pins and all of them have the same effect. My Arduino board is a few years old (Arduino Uno)...maybe that's why. Any help is greatly appreciated!

To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

We also need to see a schematic.

You do have a 10K resistor from the pin to ground don’t you?

I created a very basic test sketch to test out the issue:

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

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

A wire is connected to pin 7, and another to 5V. I am manually simulating the switch by combining the wires. However, even before I combine any wires, just touching the wire connected to pin 7 will trip the program and light the led on pin 13; not consistently on, but it jutters on and off. Obviously, when I combine the wires the led turns on consistently.

I don’t have any resistors set up, I was just following the tutorial on the Arduino website about switches.

Thanks!

(I have attached a Schematic as well)

arduino.png

add a 10k resistor from pin 7 to GND.

Review Mr. Gammon's page here: http://www.gammon.com.au/forum/?id=11955

6a234367cb67a9b0976b7bae71b7600ac1096b87.png

As has been pointed out - you need to get used to the discipline of connecting switches between the pin - 7 in this case - and ground, setting the pinMode to INPUT_PULLUP and reading a LOW in the code when the switch is on.

Just fix your wiring and code to do this and you will be fine.

Okay I actually have some resistors laying around so I'll try it out, thanks for all the help!

hakasapl: Okay I actually have some resistors laying around so I'll try it out, thanks for all the help!

But you do not need them. Just wire and code it properly.

Look at the section that talks about "Internal pull-up resistor" in the link posted in reply 4.

|500x463 .