|ATMega 168/328 pinout|
|Push button with pull-up resistor to 5V|
Switches without a resistor trick
Like the above example, if you do not want to use a resistor between the switch/push button, you can do this neat feature by utilizing the ATMEGA328 internal 20K pull-up resistor.
The command to activate the internal 20K pull-up resistor is :- digitalWrite(HIGH)
|Arduino switch without resistor|
Using Digital pin 3 as an example :-
pinMode(3,INPUT); // default mode is INPUT
digitalWrite(3, HIGH); // Turn on the internal pull-up resistor, default state is HIGH
state = digitalRead(3); // read the state of the pin
For the programming logic, when the switch is OPEN, the state is HIGH and when the switch is CLOSED or pressed, the state is LOW.
There is another neat Arduino trick to read two switches using only one digital pins at Arduino reference site. It is a combination of the above two examples.