#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
pinA
is used to read the button state- pin
pinA
is set toINPUT
mode - a resistor R1 is used to pull the pin
HIGH
by default - push-button S1 pulls the pin
LOW
when pressed
- pin
- pin
pinA_led
is used drive an LED to indicate the current button state read from pinpinA
- pin
pinA_led
is set toOUTPUT
mode - it drives and LED with current-limiting resistor
- output is set
HIGH
orLOW
to 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
pinB
is used to read the button state- pin
pinB
is set toINPUT
mode - a resistor R3 is used to pull the pin
LOW
by default - push-button S2 pulls the pin
HIGH
when pressed
- pin
- pin
pinB_led
is used drive an LED to indicate the current button state read from pinpinB
- pin
pinB_led
is set toOUTPUT
mode - it drives and LED with current-limiting resistor
- output is set
HIGH
orLOW
to 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
pinC
is used to read the button state- pin
pinC
is set toINPUT_PULLUP
mode - push-button S3 pulls the pin
LOW
when pressed
- pin
- pin
pinC_led
is used drive an LED to indicate the current button state read from pinpinC
- pin
pinC_led
is set toOUTPUT
mode - it drives and LED with current-limiting resistor
- output is set
HIGH
orLOW
to match the currently read state of pinpinC
- pin
Circuit Design
The design is available in SwitchInputs.fzz.