Fork me on GitHub

Project Notes

#728 Switch Inputs

Reviewing the main methods for reading switch inputs with an Arduino.

Build

Here’s a quick demo..

clip

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 to INPUT mode
    • a resistor R1 is used to pull the pin HIGH by default
    • push-button S1 pulls the pin LOW when pressed
  • pin pinA_led is used drive an LED to indicate the current button state read from pin pinA
    • pin pinA_led is set to OUTPUT mode
    • it drives and LED with current-limiting resistor
    • output is set HIGH or LOW to match the currently read state of pin pinA

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 to INPUT mode
    • a resistor R3 is used to pull the pin LOW by default
    • push-button S2 pulls the pin HIGH when pressed
  • pin pinB_led is used drive an LED to indicate the current button state read from pin pinB
    • pin pinB_led is set to OUTPUT mode
    • it drives and LED with current-limiting resistor
    • output is set HIGH or LOW to match the currently read state of pin pinB

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 to INPUT_PULLUP mode
    • push-button S3 pulls the pin LOW when pressed
  • pin pinC_led is used drive an LED to indicate the current button state read from pin pinC
    • pin pinC_led is set to OUTPUT mode
    • it drives and LED with current-limiting resistor
    • output is set HIGH or LOW to match the currently read state of pin pinC

Circuit Design

The design is available in SwitchInputs.fzz.

bb

schematic

bb_build

Credits and References

About LEAP#728 Arduino

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

Project Source on GitHub Return to the LEAP Catalog
About LEAP

LEAP is my personal collection of electronics projects - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting.

Projects are often inspired by things found wild on the net, or ideas from the many great electronics podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know or send a pull-request.

NOTE: For a while I included various scale modelling projects here too, but I've now split them off into a new repository: check out LittleModelArt if you are looking for these projects.

Project Gallery view the projects as an image gallery Notebook reference materials and other notes Follow the Blog follow projects and notes as they are published in your favourite feed reader