Electronic – arduino – How to make a switch on the Arduino

arduinoswitches

I have the Freetronics Eleven, which is 100% compatible with Arduino Uno. Ultimately, I want to make a foot pedal switch that connects to a digital pin on the arduino so that a LOW is read when the pedal is pressed, and a HIGH is read when the pedal is released, or vice versa.

At the moment I'm just trying to test making a switch of the same behavior by sticking wires in the pins and connecting them together. I've tried connecting and disconnecting pin 2 and gnd, pin 2 and 5v, pin 2 and pin 12 etc. with pin 2 set to input.

What is the correct setup to make a basic switch? Which pin do I connect to which to make it so when the pins are connected one reads low instead of high, or vice versa?

EDIT: Thanks for telling me about the pull-up resistor. I have since discovered via google that the arduino pins have built-in pull-up resistors. For others who come along this problem, this is how you enable them:

pinMode(10, INPUT); // set pin 10 for input
digitalWrite(10, HIGH); // set pull-up resistor on pin 10

then to use:

if (digitalRead(10) == LOW) // pin 10 is connected to gnd
    doSomething();
else // pin 10 is not connected to gnd
    doSomethingElse();

Best Answer

What you need is a pull-up resistor.

enter image description here

The Arduino whebsite has a nice tutorial (with software) on using a pushbutton.

We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-up resistor (here 2.2 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 7) which reads the button's state.

enter image description here