Arduino digital input not working correctly

Hello, setting a digital pin to INPUT on my arduino mega 2560. When I connect one end of a wire to it, and the other end is disconnected, the input still gets triggered.

This is the general gist of my code:

#define west 31

pinMode(west, INPUT);


if ( digitalRead(west) == HIGH )
{ turn on some lights }

The lights shouldn't be changing when I connect one end of a wire. I have read the official description of pinMode, apparently pins are very sensitive, so even noise can trigger them.

I've tried doing this:

#define west 31

pinMode(west, INPUT_PULLUP);


if ( digitalRead(west) == LOW )
{ turn on some lights }

However when I pass 10mA into the pin, the lights do not change.

Am I doing something wrong?

Jeequs:
... digital pin to INPUT ... arduino mega 2560. When I connect one end of a wire to it, and the other end is disconnected, the input still gets triggered.

OK, I think that you're saying that you're connecting a wire to pin 31, and leaving the other end of the wire not connected to anything. You're asking why you might read a HIGH on pin 31.

If I have your question right, I would guess that pin 31, configured as an input, is floating, and has no particular logical state. It will read HIGH or LOW depending on the characteristics of the electric field that the pin, and the wire that's connected to it, are immersed in. See a discussion here, under the heading, "Feeding."

However when I pass 10mA into the pin, the lights do not change.

Does that mean that you're sourcing 10 milliamps into the pin? That would mean that you're applying a positive voltage to a relatively high impedance. My guess is that almost the entire 10 milliamps flows from the pin through its protection diode into VCC. To push 10 milliamps into an input pin, the voltage has to be higher than VCC - certainly high enough to be read as a HIGH. Your code is looking for a LOW to turn on the lights. You're applying a signal that doesn't pass the test, so the sketch doesn't execute the statements in the block.

Maybe a schematic, and a complete, minimal sketch that illustrates the problem, would help.

I've tried doing this:

Well, that doesn't compile, does it?

Jeequs:
However when I pass 10mA into the pin, the lights do not change.

Normally you supply voltage into a data input pin, not amps. How did you determine you passed 10mA into the pin?

If you set a pullup resister, then to change the input, you need to pull the pin to a low voltage (0 - 1.5v) to make it change.

tmd3:
OK, I think that you're saying that you're connecting a wire to pin 31, and leaving the other end of the wire not connected to anything. You're asking why you might read a HIGH on pin 31.

If I have your question right, I would guess that pin 31, configured as an input, is floating, and has no particular logical state. It will read HIGH or LOW depending on the characteristics of the electric field that the pin, and the wire that's connected to it, are immersed in. See a discussion here, under the heading, "Feeding."
Does that mean that you're sourcing 10 milliamps into the pin? That would mean that you're applying a positive voltage to a relatively high impedance. My guess is that almost the entire 10 milliamps flows from the pin through its protection diode into VCC. To push 10 milliamps into an input pin, the voltage has to be higher than VCC - certainly high enough to be read as a HIGH. Your code is looking for a LOW to turn on the lights. You're applying a signal that doesn't pass the test, so the sketch doesn't execute the statements in the block.

Maybe a schematic, and a complete, minimal sketch that illustrates the problem, would help.

Thank you for the reply tm3. I'm working off a project booklet actually, and this particular project simulates two traffic lights. Here is the illustration they give:

http://s14.postimg.org/7v3zwrfq9/schematic.png

Here is the code they provide (I've changed the pin numbers for my own appropriation)

#define westButton 31
#define eastButton 33
#define westRed 50
#define westYellow 48
#define westGreen 46
#define eastRed 40
#define eastYellow 38
#define eastGreen 36

#define yellowBlinkTime 500 // 0.5 seconds for yellow light blink

boolean trafficWest = true; // west = true, east = false
int flowTime = 1000; // amount of time to let traffic flow
int changeDelay = 2000; // amount of time between color changes

void setup()
{
// setup digital I/O pins
pinMode(westButton, INPUT);
pinMode(eastButton, INPUT);
pinMode(westRed, OUTPUT);
pinMode(westYellow, OUTPUT);
pinMode(westGreen, OUTPUT);
pinMode(eastRed, OUTPUT);
pinMode(eastYellow, OUTPUT);
pinMode(eastGreen, OUTPUT);
// set initial state for lights - west side is green first
digitalWrite(westRed, LOW);
digitalWrite(westYellow, LOW);
digitalWrite(westGreen, HIGH);
digitalWrite(eastRed, HIGH);
digitalWrite(eastYellow, LOW);
digitalWrite(eastGreen, LOW);
}

void loop()
{
if ( digitalRead(westButton) == HIGH ) // request west>east traffic flow
{
if ( trafficWest != true )
// only continue if traffic flowing in the opposite (east) direction
{
trafficWest = true; // change traffic flow flag to west>east
delay(flowTime); // give time for traffic to flow
digitalWrite(eastGreen, LOW); // change east-facing lights from green
// to yellow to red
digitalWrite(eastYellow, HIGH);
delay(changeDelay);
digitalWrite(eastYellow, LOW);
digitalWrite(eastRed, HIGH);
delay(changeDelay);
for ( int a = 0; a < 5; a++ ) // blink yellow light
{
digitalWrite(westYellow, LOW);
delay(yellowBlinkTime);
digitalWrite(westYellow, HIGH);
delay(yellowBlinkTime);
}
digitalWrite(westYellow, LOW);
digitalWrite(westRed, LOW); // change west-facing lights from red to green
digitalWrite(westGreen, HIGH);
}
}

if ( digitalRead(eastButton) == HIGH ) // request east>west traffic flow
{
if ( trafficWest == true )
// only continue if traffic flow is in the opposite (west) direction
{
trafficWest = false; // change traffic flow flag to east>west
delay(flowTime); // give time for traffic to flow
digitalWrite(westGreen, LOW);
// change west lights from green to yellow to red
digitalWrite(westYellow, HIGH);
delay(changeDelay);
digitalWrite(westYellow, LOW);
digitalWrite(westRed, HIGH);
delay(changeDelay);
for ( int a = 0 ; a < 5 ; a++ ) // blink yellow light
{
digitalWrite(eastYellow, LOW);
delay(yellowBlinkTime);
digitalWrite(eastYellow, HIGH);
delay(yellowBlinkTime);
}
digitalWrite(eastYellow, LOW);
digitalWrite(eastRed, LOW); // change east-facing lights from red to green
digitalWrite(eastGreen, HIGH);
}
}
}

I've connected my components as the schematic outlines. However, the "traffic lights" go through their cycle without me pressing either switch.

According to the link you have provided, I think I just need to connect the 10k resistors to a ground.

With regards to using INPUT_PULLUP, yes, I've just realised my error. I quickly passed my eyes over the official description, seeing the words "inverts the behaviour", but did not see that I require a ground for the signalling.

"I've connected my components as the schematic outlines. However, the "traffic lights" go through their cycle without me pressing either switch. "

Wouldn't you expect traffic lights to go through their cycle Until you press a button? What are the buttons intended to simulate in a traffic light system?

What do you want to happen when you press a button?

Initially the "westgreen" led is lit, and nothing else should happen at all. The only time a change should occur is when the button representing the opposite side, is pressed.

If this is to simulate an intersection, I would think the lights would run as normal, and the buttons would be for a pedestrian wanting to cross. I am just guessing of course.

Have you changed the buttons to ground yet. How does that work for you?

I think the schematic is missing a ground on the pulldown resistors for the switches. See the attached picture. Note that my theory is based on a cursory reading of the schematic, and on a cursory look at the code. I'd welcome other opinions.

Question:
When S2 is open, what is input to D3 ?
When S2 is closed, what is input to D3 ?

Is this a class project?
You are to determine what's wrong with the hardware, and the software?
If so, we are happy to help, but not do the project.

I've had a chance to add that extra resistor, it's working fine now.

To jack wp, no, this is not a class project. I'm working on my own, off. I followed the schematic, when it wasn't working I assumed something was wrong with the hardware/software and not the schematic provided.

Thanks everyone for the responses. I know now not to put full credence to schematics even if they are "official".