#728 Switch Inputs
Reviewing the main methods for reading switch inputs with an Arduino.

Here’s a quick demo..
Notes
Reading the on/off state of a switch is perhaps the most basic Arduino GPIO input task.
It is very simple from a programming perspective. Two library functions are required:
- pinMode - used to set the appropriate mode for the selected GPIO pin
- digitalRead - used to read the binary switch state (LOW, HIGH) from the selected GPIO pin
From an electrical perspective, there are some choices to be made.
The project below is a simple demonstration of the main approaches. The code is available in SwitchInputs.ino.
Push-button: Input goes LOW when pressed
This is covered in “5.1 Using a Switch” from the Arduino Cookbook
This approach uses an external resistor to pull the input pin HIGH by default. Pressing the button will pull the input LOW.
Basic approach:
- pin
pinAis used to read the button state- pin
pinAis set toINPUTmode - a resistor R1 is used to pull the pin
HIGHby default - push-button S1 pulls the pin
LOWwhen pressed
- pin
- pin
pinA_ledis used drive an LED to indicate the current button state read from pinpinA- pin
pinA_ledis set toOUTPUTmode - it drives and LED with current-limiting resistor
- output is set
HIGHorLOWto match the currently read state of pinpinA
- pin
Push-button: Input goes HIGH when pressed
This approach alters the configuration of the external resistor to pull the input pin LOW by default. Pressing the button will pull the input HIGH.
Basic approach:
- pin
pinBis used to read the button state- pin
pinBis set toINPUTmode - a resistor R3 is used to pull the pin
LOWby default - push-button S2 pulls the pin
HIGHwhen pressed
- pin
- pin
pinB_ledis used drive an LED to indicate the current button state read from pinpinB- pin
pinB_ledis set toOUTPUTmode - it drives and LED with current-limiting resistor
- output is set
HIGHorLOWto match the currently read state of pinpinB
- pin
Push-button: Input goes HIGH when pressed, Without External Resistors
This is covered in “5.2 Using a Switch Without External Resistors” from the Arduino Cookbook
The external pull-up resistor for the switch input can be dispensed with by using the internal pull-up resistors that are part of the GPIO port design.
Pull-ups are enabled by using the INPUT_PULLUP pin mode.
Note: in earlier versions of the Arduino libraries, this mode was not available. Pull-ups were enabled by writing HIGH to the port after setting INPUT mode e.g. as follows:
pinMode(inputPin, INPUT);
digitalWrite(inputPin, HIGH);
Basic approach:
- pin
pinCis used to read the button state- pin
pinCis set toINPUT_PULLUPmode - push-button S3 pulls the pin
LOWwhen pressed
- pin
- pin
pinC_ledis used drive an LED to indicate the current button state read from pinpinC- pin
pinC_ledis set toOUTPUTmode - it drives and LED with current-limiting resistor
- output is set
HIGHorLOWto match the currently read state of pinpinC
- pin
Circuit Design
The design is available in SwitchInputs.fzz.



