Using an External Pull-up Resistor
You want to run a long wire from the Raspberry Pi to the switch, but you are getting some false readings on the input pin.
The internal pull-up resistors are quite weak (about 40kΩ). So, if you run a long lead to the switch or operate in an electrically noisy environment, you may get false triggerings on the digital input. You can overcome this by turning off the internal pull-up and pulldown resistors and using an external pull-up resistor.
Figure 11-8 shows the use of an external pull-up resistor.
To test out this hardware, you can use the program switch.py; see Recipe 11.1.
Discussion
The lower the resistance of the resistor, the longer the range of your switch. However, when you press the button, a current flows from 3.3V through the resistor to ground. A 100Ω resistor will draw a current of 3.3V/100Ω = 33 mA. This is within the safe limit for the 3.3V supply of 50 mA, so don’t use a lower value than this.
In almost all cases, a 1kΩ resistor will provide a long range with no problems.
See Also
For the basics of connecting a button, see Recipe 11.1.