It's fairly easy to code reading a switch in most languages. Refer to the following circuit as we go through the code:

Though this tutorial has examples of using a switch on an analog pin, all the examples in the tutorial can use either analog or digital pins.

Microsoft MakeCode

MakeCode has reading switches basically baked in. You can read the pins that are available as inputs.

Below is a loop that will read the switch on A1 on a Circuit Playground Express. This works with any MakeCode compatible board on any pin that can act like a digital input pin (which includes analog pins for modern microcontrollers) which has pullup resistors.

The rainbow effect will be on the NeoPixels on a Circuit Playground Express if activated, otherwise the lights will be off.

Setting pull up resistors

on start is the code that sets up our board when everything starts. The set pull pin A1 to up is the magic we hinted at earlier. Many modern microcontrollers have the ability to set a resistor pull up (to Vcc) or pull down (to ground) built in. The set pull pin A1 to up statement (in the red PINS group of blocks) enables an internal resistor between the pin (A1 for my circuit) and Vcc. No external resistor was needed here which saves us on parts and wiring!

Checking Switch Connection

The forever loop reads the pin A1 digitally. A1 will always be high due to the pull up unless the switch is activated when it will read low. For the if statement, we need to put a not statement in front of the read to activate the statement on a digital low (when the switch grounds the pin). If the program reads false (which is low), then not false is true and the program shows an animation on the boards LEDs. Your code can have your board do any action you want.

With Pull-Up resistors, you'll always have to check if the pin is connected to ground (LOW, off or False) to determine when the switch has been pressed - which is backwards from what we normally think! Watch out for this switcheroo.

CircuitPython

Download: file
from digitalio import DigitalInOut, Direction, Pull
import board

# button
button_1 = DigitalInOut(board.A1)
button_1.direction = Direction.INPUT
button_1.pull = Pull.UP

while True:
    if not button_1.value:
        print("Button activated")

CircuitPython has a library that defines the pins on CircuitPython compatible boards called boards. When you import board you can use the defined objects. Analog pins are board.A0, board.A1, etc. and digital pins board.D4, board.D5, etc.

The digitalio library allows the definition of pins for INPUT or OUTPUT and to have a Pull.UP, Pull.DOWN if you want it.

As the button is pulled up, it will return HIGH when not pushed. You want to look for a LOW which indicates the button is activated to do something, here printing to the console.

See the CircuitPython Basics Tutorial for more on using switches.

Arduino

You'll see a good number of Arduino examples on the web. You might be a bit confused there appear to be different ways to code a read. Here is the simple method corresponding to the examples above.

Download: file
void setup() {
  pinMode(A1, INPUT_PULLUP);  // set our pin to an input with a pullup resistor
}

void loop() {
  // read the state of the pushbutton value - if not activated low then loop
  if( ! digitalRead(A1) ) {
     // do something like blink pin 13 LED, or whatever
  }
}

Noisy Switching: Bounce and Debounce

The simple examples above may be all you need - to turn a light on or some other long-lived action. Go for it!

But if you are relying on a switch activation for something timed very fast (less than 10 milliseconds), you may find that the mechanical switch may make contact on and off for a few microseconds before settling down to the state you want. This is called switch bounce, see the next page for more.

This guide was first published on Aug 15, 2018. It was last updated on Aug 15, 2018.
This page (Code Your Micro) was last updated on Sep 21, 2020.